Skip to content

Commit

Permalink
Merge pull request #11 from sroze/composer-script-handler-should-use-…
Browse files Browse the repository at this point in the history
…composer-io

Uses Composer's IO for the script handler
  • Loading branch information
sroze authored Feb 6, 2018
2 parents 39c6bf8 + 197dcd2 commit 5222d47
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 15 deletions.
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
],
"require": {
"php": ">=7.0",
"symfony/console": "~4.0",
"symfony/dotenv": "~4.0",
"symfony/process": "~4.0",
"symfony/console": "~3.0 || ~4.0",
"symfony/dotenv": "~3.4 || ~4.0",
"symfony/process": "~3.0 || ~4.0",
"jackiedo/dotenv-editor": "~1.0"
},
"autoload": {
Expand All @@ -36,6 +36,7 @@
],
"require-dev": {
"behat/behat": "^3.4",
"phpspec/phpspec": "^4.3"
"phpspec/phpspec": "^4.3",
"composer/composer": "1.0.*@dev"
}
}
14 changes: 12 additions & 2 deletions src/Companienv/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public function __construct(callable $callable, $name)

$this->callable = $callable;

$this->addOption('dist-file', null, InputOption::VALUE_REQUIRED, 'Name of the file used as reference', '.env.dist');
$this->addOption('file', null, InputOption::VALUE_REQUIRED, 'Name of the file used for the values', '.env');
$this->addOption('dist-file', null, InputOption::VALUE_REQUIRED, 'Name of the file used as reference', Application::defaultDistributionFile());
$this->addOption('file', null, InputOption::VALUE_REQUIRED, 'Name of the file used for the values', Application::defaultFile());
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand Down Expand Up @@ -81,4 +81,14 @@ public static function defaultExtensions()
new AskVariableValues(),
];
}

public static function defaultFile()
{
return '.env';
}

public static function defaultDistributionFile()
{
return '.env.dist';
}
}
31 changes: 31 additions & 0 deletions src/Companienv/Composer/InteractionViaComposer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Companienv\Composer;

use Companienv\IO\Interaction;
use Composer\IO\IOInterface;

class InteractionViaComposer implements Interaction
{
private $io;

public function __construct(IOInterface $io)
{
$this->io = $io;
}

public function askConfirmation(string $question): bool
{
return $this->io->askConfirmation($question);
}

public function ask(string $question, string $default = null): string
{
return $this->io->ask($question, $default);
}

public function writeln($messageOrMessages)
{
return $this->io->write($messageOrMessages);
}
}
24 changes: 15 additions & 9 deletions src/Companienv/Composer/ScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@

namespace Companienv\Composer;

use Symfony\Component\Process\Process;
use Companienv\Application;
use Companienv\Companion;
use Companienv\Extension\Chained;
use Companienv\IO\FileSystem\NativePhpFileSystem;
use Companienv\IO\InputOutputInteraction;
use Composer\Script\Event;

class ScriptHandler
{
public static function run()
public static function run(Event $event)
{
$directory = getcwd();

if (!file_exists($path = implode(DIRECTORY_SEPARATOR, array($directory, 'bin', 'companienv')))) {
if (!file_exists($path = implode(DIRECTORY_SEPARATOR, array($directory, 'vendor', 'bin', 'companienv')))) {
throw new \RuntimeException('Could not find Companienv\'s console');
}
}

(new Process($path, getcwd()))->run();
$companion = new Companion(
new NativePhpFileSystem($directory),
new InteractionViaComposer($event->getIO()),
new Chained(Application::defaultExtensions()),
Application::defaultFile(),
Application::defaultDistributionFile()
);
$companion->fillGaps();
}
}

0 comments on commit 5222d47

Please sign in to comment.