Skip to content

Commit 7bedf2f

Browse files
committed
Initial commit
0 parents  commit 7bedf2f

File tree

32 files changed

+686
-0
lines changed

32 files changed

+686
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
composer.lock
3+
phpunit.xml
4+
vendor

.travis.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
language: php
2+
3+
sudo: false
4+
5+
cache:
6+
directories:
7+
- $HOME/.composer/cache/files
8+
9+
matrix:
10+
include:
11+
- php: 5.4
12+
- php: 5.5
13+
- php: 5.6
14+
- php: 7.0
15+
env: DEPENDENCIES=dev
16+
- php: hhvm
17+
18+
before_script:
19+
- if [ "$DEPENDENCIES" = "dev" ]; then perl -pi -e 's/^}$/,"minimum-stability":"dev"}/' composer.json; fi;
20+
- composer update $COMPOSER_FLAGS
21+
22+
script: phpunit --coverage-text --coverage-clover=coverage.clover
23+
24+
after_script:
25+
- wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover coverage.clover

LICENSE.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2017 Lexey Felde
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# EnvDiff
2+
3+
[![Latest Version on Packagist][ico-version]][link-packagist]
4+
[![Software License][ico-license]](LICENSE.md)
5+
[![Build Status][ico-travis]][link-travis]
6+
[![Coverage Status][ico-scrutinizer]][link-scrutinizer]
7+
[![Quality Score][ico-code-quality]][link-code-quality]
8+
[![Downloads][ico-downloads]][link-downloads]
9+
10+
EnvDiff is tool to compare environment keys to find the difference between .env files and actualize them
11+
12+
# Usage
13+
14+
composer Scripts composer event
15+
```$json
16+
"scripts": {
17+
"post-update-cmd": "Lf\\EnvDiff\\Composer\\ScriptHandler::actualizeEnv"
18+
}
19+
```
20+
[ico-version]: https://img.shields.io/packagist/v/tekill/env-diff.svg?style=flat-square
21+
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
22+
[ico-travis]: https://img.shields.io/travis/tekill/env-diff/master.svg?style=flat-square
23+
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/tekill/env-diff.svg?style=flat-square
24+
[ico-hhvm]: https://img.shields.io/hhvm/tekill/env-diff.svg?style=flat-square
25+
[ico-code-quality]: https://img.shields.io/scrutinizer/g/tekill/env-diff.svg?style=flat-square
26+
[ico-downloads]: https://img.shields.io/packagist/dt/tekill/env-diff.svg?style=flat-square
27+
[ico-sensio]: https://insight.sensiolabs.com/projects/1fb8cbab-f611-45b5-8a45-0113e433eab7/big.png
28+
29+
[link-packagist]: https://packagist.org/packages/tekill/env-diff
30+
[link-travis]: https://travis-ci.org/tekill/env-diff
31+
[link-scrutinizer]: https://scrutinizer-ci.com/g/tekill/env-diff/code-structure
32+
[link-code-quality]: https://scrutinizer-ci.com/g/tekill/env-diff
33+
[link-downloads]: https://packagist.org/packages/tekill/env-diff
34+
[link-author]: https://github.com/tekill
35+
[link-contributors]: ../../contributors
36+
[link-sensio]: https://insight.sensiolabs.com/projects/1fb8cbab-f611-45b5-8a45-0113e433eab7

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "tekill/env-diff",
3+
"description": "Check the difference between env files and actualize each other",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Lexey Felde",
9+
"email": "felde.lexey@gmail.com"
10+
}
11+
],
12+
"require": {
13+
"php": ">=5.4"
14+
},
15+
"require-dev": {
16+
"composer/composer": "1.0.*@dev",
17+
"phpunit/phpunit": "~6.0"
18+
},
19+
"autoload": {
20+
"psr-4": { "LF\\EnvDiff\\": "src/" }
21+
},
22+
"minimum-stability": "dev"
23+
}

phpunit.xml.dist

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit colors="true" bootstrap="vendor/autoload.php">
4+
<testsuites>
5+
<testsuite>
6+
<directory suffix="Test.php">./tests/</directory>
7+
</testsuite>
8+
</testsuites>
9+
10+
<filter>
11+
<whitelist>
12+
<directory>./</directory>
13+
<exclude>
14+
<directory>./tests</directory>
15+
<directory>./vendor</directory>
16+
</exclude>
17+
</whitelist>
18+
</filter>
19+
</phpunit>

src/Composer/ScriptHandler.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace LF\EnvDiff\Composer;
4+
5+
use Composer\Script\Event;
6+
use InvalidArgumentException;
7+
use LF\EnvDiff\Config;
8+
use LF\EnvDiff\Processor;
9+
use RuntimeException;
10+
11+
class ScriptHandler
12+
{
13+
/**
14+
* @param Event $event
15+
*
16+
* @throws InvalidArgumentException
17+
* @throws RuntimeException
18+
*/
19+
public static function actualizeEnv(Event $event)
20+
{
21+
$configs = self::extractConfigs($event);
22+
$processor = new Processor($event->getIO());
23+
24+
foreach ($configs as $config) {
25+
$processor->actualizeEnv(Config::createFormArray($config));
26+
}
27+
}
28+
29+
/**
30+
* @param Event $event
31+
*
32+
* @throws InvalidArgumentException
33+
* @throws RuntimeException
34+
*/
35+
public static function showDifference(Event $event)
36+
{
37+
$configs = self::extractConfigs($event);
38+
$processor = new Processor($event->getIO());
39+
40+
foreach ($configs as $config) {
41+
$processor->showDifference(Config::createFormArray($config));
42+
}
43+
}
44+
45+
/**
46+
* @param Event $event
47+
*
48+
* @return array
49+
*
50+
* @throws InvalidArgumentException
51+
*/
52+
private static function extractConfigs(Event $event)
53+
{
54+
$extras = $event->getComposer()->getPackage()->getExtra();
55+
56+
$configs = isset($extras['lf-diff-env']) ? $extras['lf-diff-env'] : [[]];
57+
58+
if (!is_array($configs)) {
59+
throw new InvalidArgumentException(
60+
'The extra.lf-env-diff setting must be an array or a configuration object'
61+
);
62+
}
63+
64+
foreach ($configs as $config) {
65+
if (!is_array($config)) {
66+
throw new InvalidArgumentException(
67+
'The extra.lf-env-diff setting must be an array of configuration objects'
68+
);
69+
}
70+
}
71+
72+
return $configs;
73+
}
74+
}

src/Config.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace LF\EnvDiff;
4+
5+
class Config
6+
{
7+
/** @var string */
8+
private $dist;
9+
10+
/** @var string */
11+
private $target;
12+
13+
/** @var bool */
14+
private $keepOutdatedEnv;
15+
16+
/**
17+
* @param array $config
18+
*
19+
* @return static
20+
*/
21+
public static function createFormArray(array $config = [])
22+
{
23+
if (empty($config['target'])) {
24+
$config['target'] = '.env';
25+
}
26+
if (empty($config['dist'])) {
27+
$config['dist'] = $config['target'] . '.dist';
28+
}
29+
if (empty($config['keep-outdated'])) {
30+
$config['keep-outdated'] = true;
31+
}
32+
33+
$self = new static();
34+
$self->target = $config['target'];
35+
$self->dist = $config['dist'];
36+
$self->keepOutdatedEnv = (bool) $config['keep-outdated'];
37+
38+
return $self;
39+
}
40+
41+
/**
42+
* @return string
43+
*/
44+
public function getDist()
45+
{
46+
return $this->dist;
47+
}
48+
49+
/**
50+
* @return string
51+
*/
52+
public function getTarget()
53+
{
54+
return $this->target;
55+
}
56+
57+
/**
58+
* @return boolean
59+
*/
60+
public function isKeepOutdatedEnv()
61+
{
62+
return $this->keepOutdatedEnv;
63+
}
64+
}

src/Env/Dumper.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace LF\EnvDiff\Env;
4+
5+
class Dumper
6+
{
7+
/**
8+
* @param array $envArray
9+
*
10+
* @return string
11+
*/
12+
public static function dump(array $envArray)
13+
{
14+
$dump = '';
15+
16+
foreach ($envArray as $env => $variable) {
17+
$dump .= sprintf('%s=%s%s', $env, $variable, PHP_EOL);
18+
}
19+
20+
return trim($dump);
21+
}
22+
}

src/Env/Parser.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace LF\EnvDiff\Env;
4+
5+
use InvalidArgumentException;
6+
7+
class Parser
8+
{
9+
/**
10+
* @param string $path
11+
*
12+
* @return array
13+
*
14+
* @throws InvalidArgumentException
15+
*/
16+
public static function parse($path)
17+
{
18+
if (!is_file($path)) {
19+
throw new InvalidArgumentException(sprintf('The file "%s" does not exist', $path));
20+
}
21+
22+
$file = fopen($path, 'r');
23+
24+
$env = [];
25+
while (false === feof($file)) {
26+
$line = trim(fgets($file));
27+
// Ignore empty lines
28+
29+
if (empty($line) === true || $line[0] === '#') {
30+
continue;
31+
}
32+
if (false === strpos($line, '=')) {
33+
throw new InvalidArgumentException(sprintf('Line `%s` is not valid env variable', $line));
34+
}
35+
36+
list($name, $value) = explode('=', $line);
37+
$env[trim($name)] = trim($value);
38+
}
39+
40+
return $env;
41+
}
42+
}

0 commit comments

Comments
 (0)