Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tekill committed May 3, 2017
0 parents commit 7bedf2f
Show file tree
Hide file tree
Showing 32 changed files with 686 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
composer.lock
phpunit.xml
vendor
25 changes: 25 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
language: php

sudo: false

cache:
directories:
- $HOME/.composer/cache/files

matrix:
include:
- php: 5.4
- php: 5.5
- php: 5.6
- php: 7.0
env: DEPENDENCIES=dev
- php: hhvm

before_script:
- if [ "$DEPENDENCIES" = "dev" ]; then perl -pi -e 's/^}$/,"minimum-stability":"dev"}/' composer.json; fi;
- composer update $COMPOSER_FLAGS

script: phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover coverage.clover
19 changes: 19 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2017 Lexey Felde

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# EnvDiff

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
[![Build Status][ico-travis]][link-travis]
[![Coverage Status][ico-scrutinizer]][link-scrutinizer]
[![Quality Score][ico-code-quality]][link-code-quality]
[![Downloads][ico-downloads]][link-downloads]

EnvDiff is tool to compare environment keys to find the difference between .env files and actualize them

# Usage

composer Scripts composer event
```$json
"scripts": {
"post-update-cmd": "Lf\\EnvDiff\\Composer\\ScriptHandler::actualizeEnv"
}
```
[ico-version]: https://img.shields.io/packagist/v/tekill/env-diff.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/tekill/env-diff/master.svg?style=flat-square
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/tekill/env-diff.svg?style=flat-square
[ico-hhvm]: https://img.shields.io/hhvm/tekill/env-diff.svg?style=flat-square
[ico-code-quality]: https://img.shields.io/scrutinizer/g/tekill/env-diff.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/tekill/env-diff.svg?style=flat-square
[ico-sensio]: https://insight.sensiolabs.com/projects/1fb8cbab-f611-45b5-8a45-0113e433eab7/big.png

[link-packagist]: https://packagist.org/packages/tekill/env-diff
[link-travis]: https://travis-ci.org/tekill/env-diff
[link-scrutinizer]: https://scrutinizer-ci.com/g/tekill/env-diff/code-structure
[link-code-quality]: https://scrutinizer-ci.com/g/tekill/env-diff
[link-downloads]: https://packagist.org/packages/tekill/env-diff
[link-author]: https://github.com/tekill
[link-contributors]: ../../contributors
[link-sensio]: https://insight.sensiolabs.com/projects/1fb8cbab-f611-45b5-8a45-0113e433eab7
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "tekill/env-diff",
"description": "Check the difference between env files and actualize each other",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Lexey Felde",
"email": "felde.lexey@gmail.com"
}
],
"require": {
"php": ">=5.4"
},
"require-dev": {
"composer/composer": "1.0.*@dev",
"phpunit/phpunit": "~6.0"
},
"autoload": {
"psr-4": { "LF\\EnvDiff\\": "src/" }
},
"minimum-stability": "dev"
}
19 changes: 19 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit colors="true" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite>
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
74 changes: 74 additions & 0 deletions src/Composer/ScriptHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace LF\EnvDiff\Composer;

use Composer\Script\Event;
use InvalidArgumentException;
use LF\EnvDiff\Config;
use LF\EnvDiff\Processor;
use RuntimeException;

class ScriptHandler
{
/**
* @param Event $event
*
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public static function actualizeEnv(Event $event)
{
$configs = self::extractConfigs($event);
$processor = new Processor($event->getIO());

foreach ($configs as $config) {
$processor->actualizeEnv(Config::createFormArray($config));
}
}

/**
* @param Event $event
*
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public static function showDifference(Event $event)
{
$configs = self::extractConfigs($event);
$processor = new Processor($event->getIO());

foreach ($configs as $config) {
$processor->showDifference(Config::createFormArray($config));
}
}

/**
* @param Event $event
*
* @return array
*
* @throws InvalidArgumentException
*/
private static function extractConfigs(Event $event)
{
$extras = $event->getComposer()->getPackage()->getExtra();

$configs = isset($extras['lf-diff-env']) ? $extras['lf-diff-env'] : [[]];

if (!is_array($configs)) {
throw new InvalidArgumentException(
'The extra.lf-env-diff setting must be an array or a configuration object'
);
}

foreach ($configs as $config) {
if (!is_array($config)) {
throw new InvalidArgumentException(
'The extra.lf-env-diff setting must be an array of configuration objects'
);
}
}

return $configs;
}
}
64 changes: 64 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace LF\EnvDiff;

class Config
{
/** @var string */
private $dist;

/** @var string */
private $target;

/** @var bool */
private $keepOutdatedEnv;

/**
* @param array $config
*
* @return static
*/
public static function createFormArray(array $config = [])
{
if (empty($config['target'])) {
$config['target'] = '.env';
}
if (empty($config['dist'])) {
$config['dist'] = $config['target'] . '.dist';
}
if (empty($config['keep-outdated'])) {
$config['keep-outdated'] = true;
}

$self = new static();
$self->target = $config['target'];
$self->dist = $config['dist'];
$self->keepOutdatedEnv = (bool) $config['keep-outdated'];

return $self;
}

/**
* @return string
*/
public function getDist()
{
return $this->dist;
}

/**
* @return string
*/
public function getTarget()
{
return $this->target;
}

/**
* @return boolean
*/
public function isKeepOutdatedEnv()
{
return $this->keepOutdatedEnv;
}
}
22 changes: 22 additions & 0 deletions src/Env/Dumper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace LF\EnvDiff\Env;

class Dumper
{
/**
* @param array $envArray
*
* @return string
*/
public static function dump(array $envArray)
{
$dump = '';

foreach ($envArray as $env => $variable) {
$dump .= sprintf('%s=%s%s', $env, $variable, PHP_EOL);
}

return trim($dump);
}
}
42 changes: 42 additions & 0 deletions src/Env/Parser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace LF\EnvDiff\Env;

use InvalidArgumentException;

class Parser
{
/**
* @param string $path
*
* @return array
*
* @throws InvalidArgumentException
*/
public static function parse($path)
{
if (!is_file($path)) {
throw new InvalidArgumentException(sprintf('The file "%s" does not exist', $path));
}

$file = fopen($path, 'r');

$env = [];
while (false === feof($file)) {
$line = trim(fgets($file));
// Ignore empty lines

if (empty($line) === true || $line[0] === '#') {
continue;
}
if (false === strpos($line, '=')) {
throw new InvalidArgumentException(sprintf('Line `%s` is not valid env variable', $line));
}

list($name, $value) = explode('=', $line);
$env[trim($name)] = trim($value);
}

return $env;
}
}
Loading

0 comments on commit 7bedf2f

Please sign in to comment.