Skip to content
This repository has been archived by the owner on Oct 24, 2018. It is now read-only.

Commit

Permalink
add console command
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Feb 20, 2016
1 parent 35affd3 commit c333c0b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ public function getLongVersion()
{
return parent::getLongVersion().' by <comment>Hannes Van De Vreken & Freek Van der Herten</comment>';
}
}
}
38 changes: 34 additions & 4 deletions src/Console/ConvertCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Spatie\Php7to5\Console;

use Spatie\Php7to5\DirectoryConverter;
use Spatie\Php7to5\Exceptions\InvalidArgument;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ConvertCommand extends Command
Expand All @@ -19,9 +22,16 @@ protected function configure()
'A PHP 7 file or a directory containing PHP 7 files'
)
->addArgument(
'destionation',
'destination',
InputArgument::REQUIRED,
'The file or path where the PHP 5 code should be saved'
)
->addOption(
'alsoCopyNonPhpFiles',
'nonPhp',
InputOption::VALUE_REQUIRED,
'Should non php files be copied over as well',
true
);
}

Expand All @@ -33,12 +43,32 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln("Start converting {$input->getArgument('source')}");
$this->guardAgainstInvalidArguments($input);

$source = $input->getArgument('source');
$destination = $input->getArgument('destination');

$output->writeln("Converting {$source} to {$destination}...");
$output->writeln('');

$output->writeln("All done!");
$converter = new DirectoryConverter($source);

if (!$input->getOption('alsoCopyNonPhpFiles')) {
$converter->doNotCopyNonPhpFiles();
}

$converter->savePhp5FilesTo($destination);

$output->writeln('All Done!');
$output->writeln('');

return 0;
}
}

protected function guardAgainstInvalidArguments(InputInterface $input)
{
if (!is_dir($input->getArgument('source'))) {
throw InvalidArgument::directoryDoesNotExist($input->getArgument('source'));
}
}
}
6 changes: 3 additions & 3 deletions src/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PhpParser\NodeTraverser;
use PhpParser\ParserFactory;
use Spatie\Php7to5\Exceptions\InvalidParameter;
use Spatie\Php7to5\Exceptions\InvalidArgument;

class Converter
{
Expand All @@ -14,12 +14,12 @@ class Converter
/**
* @param string $pathToPhp7Code
*
* @throws \Spatie\Php7to5\Exceptions\InvalidParameter
* @throws \Spatie\Php7to5\Exceptions\InvalidArgument
*/
public function __construct($pathToPhp7Code)
{
if (!file_exists($pathToPhp7Code)) {
throw InvalidParameter::fileDoesNotExist($pathToPhp7Code);
throw InvalidArgument::fileDoesNotExist($pathToPhp7Code);
}

$this->pathToPhp7Code = $pathToPhp7Code;
Expand Down
10 changes: 5 additions & 5 deletions src/DirectoryConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Spatie\Php7to5;

use FilesystemIterator;
use Spatie\Php7to5\Exceptions\InvalidParameter;
use Spatie\Php7to5\Exceptions\InvalidArgument;

class DirectoryConverter
{
Expand All @@ -15,12 +15,12 @@ class DirectoryConverter
*
* @param string $sourceDirectory
*
* @throws \Spatie\Php7to5\Exceptions\InvalidParameter
* @throws \Spatie\Php7to5\Exceptions\InvalidArgument
*/
public function __construct($sourceDirectory)
{
if (!file_exists($sourceDirectory)) {
throw InvalidParameter::directoryDoesNotExist($sourceDirectory);
throw InvalidArgument::directoryDoesNotExist($sourceDirectory);
}

$this->sourceDirectory = $sourceDirectory;
Expand Down Expand Up @@ -49,12 +49,12 @@ public function doNotCopyNonPhpFiles()
/**
* @param string $destinationDirectory
*
* @throws \Spatie\Php7to5\Exceptions\InvalidParameter
* @throws \Spatie\Php7to5\Exceptions\InvalidArgument
*/
public function savePhp5FilesTo($destinationDirectory)
{
if ($destinationDirectory === '') {
throw InvalidParameter::directoryIsRequired();
throw InvalidArgument::directoryIsRequired();
}

$this->copyDirectory($this->sourceDirectory, $destinationDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

use Exception;

class InvalidParameter extends Exception
class InvalidArgument extends Exception
{
/**
* @param string $directoryName
*
* @return \Spatie\Php7to5\Exceptions\InvalidParameter
* @return \Spatie\Php7to5\Exceptions\InvalidArgument
*/
public static function directoryDoesNotExist($directoryName)
{
Expand All @@ -19,15 +19,15 @@ public static function directoryDoesNotExist($directoryName)
/**
* @param string $fileName
*
* @return \Spatie\Php7to5\Exceptions\InvalidParameter
* @return \Spatie\Php7to5\Exceptions\InvalidArgument
*/
public static function fileDoesNotExist($fileName)
{
return new static("File `{$fileName}` does not exist");
}

/**
* @return \Spatie\Php7to5\Exceptions\InvalidParameter
* @return \Spatie\Php7to5\Exceptions\InvalidArgument
*/
public static function directoryIsRequired()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/ConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Spatie\Php7to5\Test;

use Spatie\Php7to5\Converter;
use Spatie\Php7to5\Exceptions\InvalidParameter;
use Spatie\Php7to5\Exceptions\InvalidArgument;

class ConverterTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -60,7 +60,7 @@ public function it_can_replace_spaceship_operators()
/** @test */
public function it_will_throw_an_exception_if_the_source_file_does_not_exist()
{
$this->setExpectedException(InvalidParameter::class);
$this->setExpectedException(InvalidArgument::class);

new Converter('thisFileDoesNotExist.php');
}
Expand Down

0 comments on commit c333c0b

Please sign in to comment.