From 39975c2533db35f7fcf5b832325a6dc8a716854b Mon Sep 17 00:00:00 2001 From: Michiel Roos Date: Fri, 21 Dec 2018 09:20:13 +0100 Subject: [PATCH] Namespace converter can handle directories --- Readme.md | 6 +- bin/typo3migrate | 2 +- composer.json | 3 +- composer.lock | 51 ++++++++++++++++- .../Command/ConvertFluidNamespacesCommand.php | 57 +++++++++++++------ 5 files changed, 97 insertions(+), 22 deletions(-) diff --git a/Readme.md b/Readme.md index 6c6a425..63e7999 100644 --- a/Readme.md +++ b/Readme.md @@ -45,11 +45,11 @@ Will become: ``` -Command: +Convert a single file: ```bash php ./typo3migrate.phar fluidNsToHtml ~/tmp/Template.html ``` -Or in a loop: +Or all files in a folder: ```bash -for f in `find ./tmp/Templates/ -name '*.html'`; do ./bin/typo3migrate fluidNsToHtml $f; done +php ./typo3migrate.phar fluidNsToHtml ~/tmp/Templates/ ``` \ No newline at end of file diff --git a/bin/typo3migrate b/bin/typo3migrate index 1d5604a..4fd8ec8 100755 --- a/bin/typo3migrate +++ b/bin/typo3migrate @@ -32,7 +32,7 @@ foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../../../../autoload.p use MichielRoos\TYPO3Migrate\Console\Application; -$app = new Application('TYPO3Migrate', '1.2.3'); +$app = new Application('TYPO3Migrate', '1.3.0'); $app->add(new \MichielRoos\TYPO3Migrate\Command\ConvertFluidNamespacesCommand()); $app->add(new \MichielRoos\TYPO3Migrate\Command\Xml2XlfCommand()); $app->run(); diff --git a/composer.json b/composer.json index 935cbf7..d6ccbd9 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "require": { "php": "^5.6 || ^7.0", "symfony/console": "^2.7 || ^2.8 || ^3.4", - "symfony/filesystem": "2.7 || ^2.8 || ^3.4" + "symfony/filesystem": "2.7 || ^2.8 || ^3.4", + "symfony/finder": "2.7 || ^2.8 || ^3.4" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index c0995e9..017d52a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "292bb252a73684022155138f5ea2b6e9", + "content-hash": "469050a0910f4cbdab45262f00a6f268", "packages": [ { "name": "psr/log", @@ -228,6 +228,55 @@ "homepage": "https://symfony.com", "time": "2018-11-11T19:48:54+00:00" }, + { + "name": "symfony/finder", + "version": "v2.8.49", + "source": { + "type": "git", + "url": "https://github.com/symfony/finder.git", + "reference": "1444eac52273e345d9b95129bf914639305a9ba4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/finder/zipball/1444eac52273e345d9b95129bf914639305a9ba4", + "reference": "1444eac52273e345d9b95129bf914639305a9ba4", + "shasum": "" + }, + "require": { + "php": ">=5.3.9" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.8-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Finder\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Finder Component", + "homepage": "https://symfony.com", + "time": "2018-11-11T11:18:13+00:00" + }, { "name": "symfony/polyfill-ctype", "version": "v1.10.0", diff --git a/src/TYPO3Migrate/Command/ConvertFluidNamespacesCommand.php b/src/TYPO3Migrate/Command/ConvertFluidNamespacesCommand.php index 5397a56..0836c42 100644 --- a/src/TYPO3Migrate/Command/ConvertFluidNamespacesCommand.php +++ b/src/TYPO3Migrate/Command/ConvertFluidNamespacesCommand.php @@ -29,6 +29,7 @@ use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Finder\Finder; /** * Class ConvertFluidNamespacesCommand @@ -48,7 +49,7 @@ protected function configure() ->setName('fluidNsToHtml') ->setDescription('Convert old Fluid namespaces (curly brace style) to html tag with attributes') ->setDefinition([ - new InputArgument('template', InputArgument::REQUIRED, 'File to convert') + new InputArgument('target', InputArgument::REQUIRED, 'File or directory to convert') ]) ->setHelp(<<fluidNsToHtml command converts old Fluid namespaces (curly brace style) to html tag with attributes. @@ -76,13 +77,36 @@ protected function execute(InputInterface $input, OutputInterface $output) $stdErr = $output->getErrorOutput(); } - $template = realpath($input->getArgument('template')); - if (!is_file($template)) { - $stdErr->writeln(sprintf('File does not exist: "%s"', $template)); + $target = realpath($input->getArgument('target')); + if (!is_file($target) && !is_dir($target)) { + $stdErr->writeln(sprintf('Path does not exist: "%s"', $target)); exit; } - $lines = file($template); + if (is_dir($target)) { + $templateFinder = new Finder(); + $templateList = $templateFinder->files()->in($target)->name('*.html'); + /** @var \SplFileInfo $templateFile */ + foreach ($templateList as $templateFile) { + $this->convertFile($templateFile->getPathname(), $input, $output); + } + } elseif (is_file($target)) { + $this->convertFile($target, $input, $output); + } + } + + /** + * Convert a file + * + * @param string $target + * @param InputInterface $input + * @param OutputInterface $output + * + * @return void|int + */ + protected function convertFile($target, InputInterface $input, OutputInterface $output) + { + $lines = file($target); $hasOldNamespaces = false; $namespaces = []; @@ -98,23 +122,24 @@ protected function execute(InputInterface $input, OutputInterface $output) $hasHtmlTag = false; foreach ($contentLines as $contentLine) { - if (trim($contentLine) === '') { + $contentLine = trim($contentLine); + if ($contentLine === '') { continue; } - if (preg_match('/\s*writeln(sprintf('Found %s old namespaces', 0)); - exit; + return; } if ($hasOldNamespaces && $hasHtmlTag) { $output->writeln('Found old namespaces but also a html tag. Please investigate.'); - exit; + return; } $output->writeln(sprintf('Found %s old namespaces:', count($namespaces))); @@ -122,12 +147,12 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln(sprintf('- %s', $namespace)); } - $htmlTag = 'touch($template); + $filesystem->touch($target); } catch (IOExceptionInterface $exception) { echo 'An error occurred while creating the template file at ' . $exception->getPath(); - exit; + return; } - $filesystem->dumpFile($template, $newTemplate); - $output->writeln(sprintf('Wrote template data to: %s', $template)); + $filesystem->dumpFile($target, $newTemplate); + $output->writeln(sprintf('Wrote template data to: %s', $target)); } }