From 7b23a82adc0235d4fb644bfb07e52ea911db1c93 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 16 May 2017 10:20:56 +0200 Subject: [PATCH] FEATURE: Provide cli to generate migration commands * Provide symfony cli app to generate migration for filesystem and sql. --- composer.json | 6 +- src/CommandLine/App.php | 16 +++ .../Commands/Fluid/ReplaceNameSpaceImport.php | 99 +++++++++++++++++++ 3 files changed, 119 insertions(+), 2 deletions(-) create mode 100755 src/CommandLine/App.php create mode 100644 src/CommandLine/Commands/Fluid/ReplaceNameSpaceImport.php diff --git a/composer.json b/composer.json index 623bfde..dfc2ec1 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ }, "autoload": { "psr-4": { - "Typo3Update\\": "src/Standards/Typo3Update/" + "Typo3Update\\": "src/Standards/Typo3Update/", + "Typo3Update\\CommandLine\\": "src/CommandLine/" }, "files": [ "src/CodeSniffer/Tokenizers/TypoScript.php" @@ -27,11 +28,12 @@ "helmich/typo3-typoscript-parser": "1.1.*", "squizlabs/php_codesniffer": "2.8.*", "symfony/yaml": "3.2.*", + "symfony/finder": "3.2.*", + "symfony/console": "3.2.*", "higidi/composer-phpcodesniffer-standards-plugin": "*" }, "require-dev": { "phpunit/phpunit": "5.7.*", - "symfony/finder": "3.2.*", "phpmd/phpmd": "2.6.*", "pdepend/pdepend": "2.5.*" } diff --git a/src/CommandLine/App.php b/src/CommandLine/App.php new file mode 100755 index 0000000..1905bda --- /dev/null +++ b/src/CommandLine/App.php @@ -0,0 +1,16 @@ +#!/usr/bin/env php +add($command); + $application->setDefaultCommand($command->getName(), true); + + $application->run(); +}); diff --git a/src/CommandLine/Commands/Fluid/ReplaceNameSpaceImport.php b/src/CommandLine/Commands/Fluid/ReplaceNameSpaceImport.php new file mode 100644 index 0000000..b2b5b67 --- /dev/null +++ b/src/CommandLine/Commands/Fluid/ReplaceNameSpaceImport.php @@ -0,0 +1,99 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +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; +use Symfony\Component\Finder\Finder; + +/** + * + */ +class ReplaceNameSpaceImport extends Command +{ + protected $input; + protected $output; + + protected function configure() + { + $this->setName('Typo3Update:generateReplaceNamespaceImports') + ->setDescription('Will dump command to replace Namespace imports in Fluid.') + ->setHelp('Replaces =Tx_ExtName_ViewHelpers with =\Vendor\ExtName\ViewHelpers. SQL is provided as some might have fluid in their database.') + + ->addArgument('documentRoot', InputArgument::REQUIRED, 'The document root containing TYPO3 installation.') + ->addArgument('vendor', InputArgument::REQUIRED, 'Namespace vendor to use.') + + ->addOption('hide-cli', null, InputOption::VALUE_OPTIONAL, 'Do not display cli command output.', false) + ->addOption('hide-sql', null, InputOption::VALUE_OPTIONAL, 'Do not display sql query output.', false) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->input = $input; + $this->output = $output; + + foreach ($this->getExtensionKeys($input->getArgument('documentRoot')) as $extKey) { + $this->migrateExtension($extKey, $input->getArgument('vendor')); + } + } + + protected function migrateExtension($extKey, $vendor) + { + $extName = implode('', array_map('ucfirst', explode('_', $extKey))); + + if ($this->output->isVerbose()) { + $this->output->writeln("Processing: $extName"); + } + if ($this->input->getOption('hide-cli') == false) { + $this->output->writeln("LC_ALL=C sed -i '' 's#Tx_{$extName}_ViewHelpers#{$vendor}\\\\{$extName}\\\\ViewHelpers#' `ag 'Tx_{$extName}_ViewHelpers' --html -l`;"); + } + if ($this->input->getOption('hide-sql') == false) { + $this->output->writeln("UPDATE tt_content SET bodytext = replace(bodytext, 'Tx_{$extName}_ViewHelpers', '{$vendor}\\{$extName}\\ViewHelpers') WHERE bodytext LIKE '%Tx_{$extName}_ViewHelpers%';"); + } + } + + protected function getExtensionKeys($documentRoot) + { + $finder = new Finder(); + $folders = $finder->directories() + ->depth('==0') + ->in($this->getTypo3ConfFolder($documentRoot)) + ; + + foreach ($folders as $folder) { + yield $folder->getRelativePathname(); + } + } + + protected function getTypo3ConfFolder($documentRoot) + { + $confFolder = $documentRoot . DIRECTORY_SEPARATOR . 'typo3conf' . DIRECTORY_SEPARATOR . 'ext'; + if (is_dir($confFolder)) { + return $confFolder; + } + + throw new \Exception('typo3conf/ext folder coult not be found in document root "' . $documentRoot . '"', 1494920027); + } +}