Skip to content

Commit

Permalink
Add new method, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tekill committed May 7, 2017
1 parent e387eff commit c4c0f5c
Show file tree
Hide file tree
Showing 67 changed files with 730 additions and 102 deletions.
25 changes: 17 additions & 8 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@ checks:
fix_identation_4spaces: true
fix_doc_comments: true

build:
tests:
override:
-
command: 'vendor/bin/phpunit --coverage-clover=some-file'
coverage:
file: 'some-file'
format: 'clover'
tools:
external_code_coverage:
timeout: 600
runs: 3
php_analyzer: true
php_code_coverage: false
php_code_sniffer:
config:
standard: PSR2
filter:
paths: ['src']
php_loc:
enabled: true
excluded_dirs: [vendor, tests]
php_cpd:
enabled: true
excluded_dirs: [vendor, tests]
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ php:
before_script:
- composer install

script: vendor/bin/phpunit
script: vendor/bin/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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ composer Scripts composer event
[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-author]: https://github.com/Tekill
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
}
],
"require": {
"php": ">=5.6"
"php": ">=5.6",
"symfony/console": "~2.8|~3.0"
},
"require-dev": {
"composer/composer": "1.0.*@dev",
"phpunit/phpunit": "^5.0"
},
"autoload": {
"psr-4": { "LF\\EnvDiff\\": "src/" }
}
},
"bin": [
"env-diff"
]
}
37 changes: 37 additions & 0 deletions env-diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env php
<?php

$autoloadPaths = [
__DIR__ . '/../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/vendor/autoload.php'
];

foreach ($autoloadPaths as $path) {
if (file_exists($path)) {
require_once $path;

$config = [];

$processor = new LF\EnvDiff\Processor(
new \LF\EnvDiff\IO\ConsoleIO(
new \Symfony\Component\Console\Input\ArgvInput(),
new \Symfony\Component\Console\Output\ConsoleOutput()
)
);

$processor->actualizeEnv(LF\EnvDiff\Config::createFormArray($config));
$processor->showDifference(LF\EnvDiff\Config::createFormArray($config));

exit(0);
}
}

fwrite(
STDERR,
'You need to set up the project dependencies using Composer:' . PHP_EOL . PHP_EOL .
' composer install' . PHP_EOL . PHP_EOL .
'You can learn all about Composer on https://getcomposer.org/.' . PHP_EOL
);

exit(1);
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<filter>
<whitelist>
<directory>./</directory>
<directory>./src</directory>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
Expand Down
5 changes: 3 additions & 2 deletions src/Composer/ScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Composer\Script\Event;
use InvalidArgumentException;
use LF\EnvDiff\Config;
use LF\EnvDiff\IO\ComposerIO;
use LF\EnvDiff\Processor;
use RuntimeException;

Expand All @@ -19,7 +20,7 @@ class ScriptHandler
public static function actualizeEnv(Event $event)
{
$configs = self::extractConfigs($event);
$processor = new Processor($event->getIO());
$processor = new Processor(new ComposerIO($event->getIO()));

foreach ($configs as $config) {
$processor->actualizeEnv(Config::createFormArray($config));
Expand All @@ -35,7 +36,7 @@ public static function actualizeEnv(Event $event)
public static function showDifference(Event $event)
{
$configs = self::extractConfigs($event);
$processor = new Processor($event->getIO());
$processor = new Processor(new ComposerIO($event->getIO()));

foreach ($configs as $config) {
$processor->showDifference(Config::createFormArray($config));
Expand Down
26 changes: 19 additions & 7 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

class Config
{
const DEFAULT_TARGET = '.env';
CONST DEFAULT_DIST = '.env.dist';

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

Expand All @@ -13,6 +16,20 @@ class Config
/** @var bool */
private $keepOutdatedEnv;

/**
* Config constructor.
*
* @param string $dist
* @param string $target
* @param bool $keepOutdatedEnv
*/
public function __construct($dist = self::DEFAULT_DIST, $target = self::DEFAULT_TARGET, $keepOutdatedEnv = true)
{
$this->dist = $dist;
$this->target = $target;
$this->keepOutdatedEnv = $keepOutdatedEnv;
}

/**
* @param array $config
*
Expand All @@ -26,16 +43,11 @@ public static function createFormArray(array $config = [])
if (empty($config['dist'])) {
$config['dist'] = $config['target'] . '.dist';
}
if (empty($config['keep-outdated'])) {
if (!isset($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 new static($config['dist'], $config['target'], (bool) $config['keep-outdated']);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions src/Env.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace LF\EnvDiff;

use InvalidArgumentException;
use LF\EnvDiff\Env\Dumper;
use LF\EnvDiff\Env\Parser;

class Env
{
/**
* @param array $envArray
*
* @return string
*/
public static function dump(array $envArray)
{
return (new Dumper())->dump($envArray);
}

/**
* @param string $path
*
* @return array
*
* @throws InvalidArgumentException
*/
public static function parse($path)
{
return (new Parser())->parse($path);
}
}
2 changes: 1 addition & 1 deletion src/Env/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Dumper
*
* @return string
*/
public static function dump(array $envArray)
public function dump(array $envArray)
{
$dump = '';

Expand Down
29 changes: 22 additions & 7 deletions src/Env/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,45 @@ class Parser
*
* @throws InvalidArgumentException
*/
public static function parse($path)
public function parse($path)
{
if (!is_file($path)) {
throw new InvalidArgumentException(sprintf('The file "%s" does not exist', $path));
}

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

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

if (empty($line) === true || $line[0] === '#') {
if ($this->isCommentOrEmpty($line)) {
continue;
}
if (false === strpos($line, '=')) {
throw new InvalidArgumentException(sprintf('Line `%s` is not valid env variable', $line));
throw new InvalidArgumentException(
sprintf('Parse error at %d line: `%s` is not valid env value', $number, $line)
);
}

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

fclose($file);

return $env;
}

/**
* @param string $line
*
* @return bool
*/
private function isCommentOrEmpty($line)
{
return (mb_strlen($line) === 0 || $line[0] === '#');
}
}
48 changes: 48 additions & 0 deletions src/IO/ComposerIO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace LF\EnvDiff\IO;

use Composer\IO\IOInterface as ComposerIOInterface;

/**
* @codeCoverageIgnore
*/
class ComposerIO implements IOInterface
{
/** @var ComposerIOInterface */
private $io;

/**
* ComposerIO constructor.
*
* @param ComposerIOInterface $io
*/
public function __construct(ComposerIOInterface $io)
{
$this->io = $io;
}

/**
* {@inheritdoc}
*/
public function write($message)
{
$this->io->write($message);
}

/**
* {@inheritdoc}
*/
public function isInteractive()
{
return $this->io->isInteractive();
}

/**
* {@inheritdoc}
*/
public function ask($question, $default = null)
{
return $this->ask($question, $default);
}
}
60 changes: 60 additions & 0 deletions src/IO/ConsoleIO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace LF\EnvDiff\IO;

use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

/**
* @codeCoverageIgnore
*/
class ConsoleIO implements IOInterface
{
/** @var InputInterface */
private $input;

/** @var OutputInterface */
private $output;

/** @var QuestionHelper */
private $questionHelper;

/**
* ConsoleIO constructor.
*
* @param InputInterface $input
* @param OutputInterface $output
*/
public function __construct(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$this->questionHelper = new QuestionHelper();
}

/**
* {@inheritdoc}
*/
public function write($message)
{
$this->output->writeln($message);
}

/**
* {@inheritdoc}
*/
public function isInteractive()
{
$this->input->isInteractive();
}

/**
* {@inheritdoc}
*/
public function ask($question, $default = null)
{
return $this->questionHelper->ask($this->input, $this->output, new Question($question, $default));
}
}
Loading

0 comments on commit c4c0f5c

Please sign in to comment.