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

Commit

Permalink
Merge pull request #23 from sergiusd/extension_and_exclude
Browse files Browse the repository at this point in the history
Add options extension and exclude
  • Loading branch information
freekmurze authored Sep 25, 2017
2 parents 0b5c234 + ee66bc1 commit 63398c9
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 18 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ By default the command will only copy over `php`-files. Want to copy over all fi
$ php7to5 convert {$directoryWithPHP7Code} {$destinationWithPHP5Code} --copy-all
```

The command uses an extension for the php - *.php, you can make your set of extensions.

```bash
$ php7to5 convert {$directoryWithPHP7Code} {$destinationWithPHP5Code} --extension=php --extenstion=phtml
```

If necessary, you can exclude directories / files.

```bash
$ php7to5 convert {$directoryWithPHP7Code} {$destinationWithPHP5Code} --exсlude=cache --extenstion=vendor/package
```

## Programmatically convert files

You can convert a single file by running this code:
Expand Down
17 changes: 16 additions & 1 deletion src/Console/ConvertCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ protected function configure()
InputArgument::REQUIRED,
'The file or path where the PHP 5 code should be saved'
)
->addOption(
'extension',
'e',
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
'PHP extensions',
['php']
)
->addOption(
'exclude',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
'Exclude path'
)
->addOption(
'copy-all',
null,
Expand Down Expand Up @@ -85,7 +98,9 @@ protected function convertPHPFilesInDirectory(InputInterface $input, OutputInter
{
$source = $input->getArgument('source');
$destination = $input->getArgument('destination');
$converter = new DirectoryConverter($source);
$extensions = $input->getOption('extension');
$excludes = $input->getOption('exclude');
$converter = new DirectoryConverter($source, $extensions, $excludes);

$this->isDestinationASourceDirectory($source, $destination);
$this->isDestinationDifferentThanSource($source, $destination);
Expand Down
49 changes: 35 additions & 14 deletions src/DirectoryConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,38 @@

namespace Spatie\Php7to5;

use FilesystemIterator;
use Spatie\Php7to5\Exceptions\InvalidParameter;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;

class DirectoryConverter
{
/** @var string */
protected $copyNonPhpFiles = true;
/** @var string[] */
protected $extensions;
/** @var null|string[] */
protected $excludes;
protected $logger;

/**
* DirectoryConverter constructor.
*
* @param string $sourceDirectory
* @param string[] $extensions
* @param string[]|null $excludes
*
* @throws \Spatie\Php7to5\Exceptions\InvalidParameter
*/
public function __construct($sourceDirectory)
public function __construct($sourceDirectory, array $extensions, array $excludes = null)
{
if (!file_exists($sourceDirectory)) {
throw InvalidParameter::directoryDoesNotExist($sourceDirectory);
}

$this->sourceDirectory = $sourceDirectory;
$this->extensions = array_map('mb_strtolower', $extensions);
$this->excludes = $excludes;
}

public function setLogger(OutputInterface $output)
Expand Down Expand Up @@ -87,22 +95,35 @@ protected function copyDirectory($sourceDirectory, $destinationDirectory)
mkdir($destinationDirectory);
}

$items = new FilesystemIterator($sourceDirectory, FilesystemIterator::SKIP_DOTS);
$finder = new Finder();
$finder->in($sourceDirectory);
if (!$this->copyNonPhpFiles) {
foreach ($this->extensions as $extension) {
$finder->name('*.'.$extension);
}
}

foreach ($items as $item) {
$target = $destinationDirectory.'/'.$item->getBasename();
if ($this->excludes) {
foreach ($this->excludes as $exclude) {
$finder->notPath('/^'.preg_quote($exclude, '/').'/');
}
}

if ($item->isDir()) {
$sourceDirectory = $item->getPathname();
foreach ($finder as $item) {
$target = $destinationDirectory.'/'.$item->getRelativePathname();

$this->copyDirectory($sourceDirectory, $target);
} else {
if ($this->isPhpFile($target) || $this->copyNonPhpFiles) {
copy($item->getPathname(), $target);
if ($item->isFile()) {
$isPhpFile = $this->isPhpFile($target);
if ($isPhpFile || $this->copyNonPhpFiles) {
$targetDir = dirname($target);
if ($targetDir && !is_dir($targetDir)) {
mkdir($targetDir, 0755, true);
}
copy($item->getRealPath(), $target);

$this->log($item->getBasename(), $target);
$this->log($item->getRelativePath(), $target);

if (strtolower(pathinfo($target, PATHINFO_EXTENSION)) === 'php') {
if ($isPhpFile) {
$this->convertToPhp5($target);
}
}
Expand All @@ -127,6 +148,6 @@ protected function convertToPhp5($filePath)
*/
protected function isPhpFile($filePath)
{
return strtolower(pathinfo($filePath, PATHINFO_EXTENSION)) === 'php';
return in_array(strtolower(pathinfo($filePath, PATHINFO_EXTENSION)), $this->extensions, true);
}
}
65 changes: 64 additions & 1 deletion tests/ConsoleCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,77 @@ public function it_can_convert_all_php_files_from_a_given_directory()
$this->assertTempFileNotExists([
$destinationDirectory.'/file3.txt',
$destinationDirectory.'/directory1/file3.txt',
$destinationDirectory.'/directory1/file4.phtml',
]);

$this->assertTempFileExists([
$destinationDirectory.'/file1.php',
$destinationDirectory.'/file2.php',
$destinationDirectory.'/directory1/file1.php',
$destinationDirectory.'/directory1/file2.php',
]);
}

/** @test */
public function it_can_convert_all_php_files_from_a_given_directory_with_additional_extension()
{
$destinationDirectory = $this->destinationDirectory;
$command = $this->getCommand($this->sourceDirectory, $destinationDirectory, '--overwrite --extension=php --extension=phtml');
$this->runCommand($command);

$this->assertTempFileNotExists([
$destinationDirectory.'/file3.txt',
$destinationDirectory.'/directory1/file3.txt',
]);

$this->assertTempFileExists([
$destinationDirectory.'/file1.php',
$destinationDirectory.'/file2.php',
$destinationDirectory.'/directory1/file1.php',
$destinationDirectory.'/directory1/file2.php',
$destinationDirectory.'/directory1/file4.phtml',
]);
}

/** @test */
public function it_can_convert_all_php_files_from_a_given_directory_with_exclude_directory()
{
$destinationDirectory = $this->destinationDirectory;
$command = $this->getCommand($this->sourceDirectory, $destinationDirectory, '--overwrite --exclude=directory1');
$this->runCommand($command);

$this->assertTempFileNotExists([
$destinationDirectory.'/file3.txt',
$destinationDirectory.'/directory1/file3.txt',
$destinationDirectory.'/directory1/file1.php',
$destinationDirectory.'/directory1/file2.php',
$destinationDirectory.'/directory1/file4.phtml',
]);

$this->assertTempFileExists([
$destinationDirectory.'/file1.php',
$destinationDirectory.'/file2.php',
]);
}

/** @test */
public function it_can_convert_all_php_files_from_a_given_directory_with_exclude_file()
{
$destinationDirectory = $this->destinationDirectory;
$command = $this->getCommand($this->sourceDirectory, $destinationDirectory, '--overwrite --exclude=directory1/file2.php');
$this->runCommand($command);

$this->assertTempFileNotExists([
$destinationDirectory.'/file3.txt',
$destinationDirectory.'/directory1/file3.txt',
$destinationDirectory.'/directory1/file2.php',
$destinationDirectory.'/directory1/file4.phtml',
]);

$this->assertTempFileExists([
$destinationDirectory.'/file1.php',
$destinationDirectory.'/file2.php',
$destinationDirectory.'/directory1/file1.php',
]);
}

Expand All @@ -72,7 +135,7 @@ public function it_can_convert_all_files_from_a_given_directory_if_option_copy_a
$destinationDirectory.'/directory1/file1.php',
$destinationDirectory.'/directory1/file2.php',
$destinationDirectory.'/directory1/file3.txt',

$destinationDirectory.'/directory1/file4.phtml',
]);
}

Expand Down
80 changes: 78 additions & 2 deletions tests/DirectoryConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function setUp()
/** @test */
public function it_can_copy_and_convert_an_entire_directory()
{
$directoryConverter = new DirectoryConverter($this->getSourceDirectory());
$directoryConverter = new DirectoryConverter($this->getSourceDirectory(), ['php']);

$directoryConverter->savePhp5FilesTo($this->getTempDirectory());

Expand All @@ -36,7 +36,7 @@ public function it_can_copy_and_convert_an_entire_directory()
/** @test */
public function it_can_copy_and_convert_an_entire_directory_filtering_on_php_files()
{
$directoryConverter = new DirectoryConverter($this->getSourceDirectory());
$directoryConverter = new DirectoryConverter($this->getSourceDirectory(), ['php']);

$directoryConverter
->doNotCopyNonPhpFiles()
Expand All @@ -52,6 +52,82 @@ public function it_can_copy_and_convert_an_entire_directory_filtering_on_php_fil
$this->assertTempFileNotExists([
'sourceDirectory/file3.txt',
'sourceDirectory/directory1/file3.txt',
'sourceDirectory/directory1/file4.phtml',
]);

$this->assertAllPhpFilesWereConverted($this->getTempDirectory());
}

/** @test */
public function it_can_copy_and_convert_an_entire_directory_filtering_on_custom_php_files()
{
$directoryConverter = new DirectoryConverter($this->getSourceDirectory(), ['php', 'phtml']);

$directoryConverter
->doNotCopyNonPhpFiles()
->savePhp5FilesTo($this->getTempDirectory());

$this->assertTempFileExists([
'sourceDirectory/file1.php',
'sourceDirectory/file2.php',
'sourceDirectory/directory1/file1.php',
'sourceDirectory/directory1/file2.php',
'sourceDirectory/directory1/file4.phtml',
]);

$this->assertTempFileNotExists([
'sourceDirectory/file3.txt',
'sourceDirectory/directory1/file3.txt',
]);

$this->assertAllPhpFilesWereConverted($this->getTempDirectory());
}

/** @test */
public function it_can_copy_and_convert_an_entire_directory_with_exclude_directory()
{
$directoryConverter = new DirectoryConverter($this->getSourceDirectory(), ['php'], ['sourceDirectory/directory1']);

$directoryConverter
->doNotCopyNonPhpFiles()
->savePhp5FilesTo($this->getTempDirectory());

$this->assertTempFileExists([
'sourceDirectory/file1.php',
'sourceDirectory/file2.php',
]);

$this->assertTempFileNotExists([
'sourceDirectory/file3.txt',
'sourceDirectory/directory1/file1.php',
'sourceDirectory/directory1/file2.php',
'sourceDirectory/directory1/file3.txt',
'sourceDirectory/directory1/file4.phtml',
]);

$this->assertAllPhpFilesWereConverted($this->getTempDirectory());
}

/** @test */
public function it_can_copy_and_convert_an_entire_directory_with_exclude_file()
{
$directoryConverter = new DirectoryConverter($this->getSourceDirectory(), ['php'], ['sourceDirectory/directory1/file1.php']);

$directoryConverter
->doNotCopyNonPhpFiles()
->savePhp5FilesTo($this->getTempDirectory());

$this->assertTempFileExists([
'sourceDirectory/file1.php',
'sourceDirectory/file2.php',
'sourceDirectory/directory1/file2.php',
]);

$this->assertTempFileNotExists([
'sourceDirectory/file3.txt',
'sourceDirectory/directory1/file1.php',
'sourceDirectory/directory1/file3.txt',
'sourceDirectory/directory1/file4.phtml',
]);

$this->assertAllPhpFilesWereConverted($this->getTempDirectory());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><?php echo time() ?></p>

0 comments on commit 63398c9

Please sign in to comment.