Skip to content

Commit

Permalink
Namespace converter can handle directories
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuurlijk committed Dec 21, 2018
1 parent 4a36f7b commit 39975c2
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 22 deletions.
6 changes: 3 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ Will become:
</section>
</html>
```
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/
```
2 changes: 1 addition & 1 deletion bin/typo3migrate
Original file line number Diff line number Diff line change
Expand Up @@ -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();
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
51 changes: 50 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 41 additions & 16 deletions src/TYPO3Migrate/Command/ConvertFluidNamespacesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(<<<EOT
The <info>fluidNsToHtml</info> command converts old Fluid namespaces (curly brace style) to html tag with attributes.
Expand Down Expand Up @@ -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 = [];
Expand All @@ -98,49 +122,50 @@ 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*<html.*/', $line)) {
if (preg_match('/\s*<html.*/', $contentLine)) {
$hasHtmlTag = true;
break;
}
}

if (!$hasOldNamespaces) {
if (!$hasOldNamespaces && $hasHtmlTag) {
$output->writeln(sprintf('Found <info>%s</info> 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 <info>%s</info> old namespaces:', count($namespaces)));
foreach ($namespaces as $namespace) {
$output->writeln(sprintf('- <comment>%s</comment>', $namespace));
}

$htmlTag = '<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"' . PHP_EOL;
$htmlTag = '<html xmlns:f="https://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"' . PHP_EOL;
foreach ($namespaces as $namespace) {
$namespace = trim($namespace, '{}');
$namespace = preg_replace('/^namespace\s+([^ ]*)/', '$1', $namespace);
list($key, $class) = explode('=', $namespace);
$htmlTag .= sprintf("\t xmlns:%s=\"http://typo3.org/ns/%s\"" . PHP_EOL, $key, str_replace('\\', '/', $class));
$htmlTag .= sprintf("\t xmlns:%s=\"https://typo3.org/ns/%s\"" . PHP_EOL, $key, str_replace('\\', '/', $class));
}
$htmlTag .= ' data-namespace-typo3-fluid="true">' . PHP_EOL;

$newTemplate = $htmlTag . implode('', $contentLines) . PHP_EOL . '</html>';

$filesystem = new Filesystem();
try {
$filesystem->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: <info>%s</info>', $template));
$filesystem->dumpFile($target, $newTemplate);
$output->writeln(sprintf('Wrote template data to: <info>%s</info>', $target));
}
}

0 comments on commit 39975c2

Please sign in to comment.