From 93305beedcf778cdae53128939307b765f161948 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20R?= Date: Mon, 18 Feb 2019 13:59:25 +0100 Subject: [PATCH] Adapt AppKernel.hp and routing.yml file in init --- INSTALL.md | 83 ++++------------ bundle/Command/LegacyInitCommand.php | 143 ++++++++++++++++++++++----- 2 files changed, 133 insertions(+), 93 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index 3bc4dc14..3a5aa5fa 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -3,76 +3,35 @@ Instructions below will take you true installing legacy bridge and implicit legacy on top of a eZ Platform 1.13.x - 2.x install. -### Add the composer `legacy post-*-scripts` +_TIP:_ +> Before starting make sure to check-in (e.g. to Git) your eZ Platform project working space so you'll be able to see & verify changes applied to your setup separate from initial clean project install. + +### Install `ezsystems/legacy-bridge` and run `init` command + +Package can be installed using Composer in the following way: -Edit `composer.json`, and add those lines to both `post-update-cmd` and `post-install-cmd` blocks at the end, but before -`eZ\Bundle\EzPublishCoreBundle\Composer\ScriptHandler::dumpAssets`: ``` -"scripts": { - "legacy-scripts": [ - "eZ\\Bundle\\EzPublishLegacyBundle\\Composer\\ScriptHandler::installAssets", - "eZ\\Bundle\\EzPublishLegacyBundle\\Composer\\ScriptHandler::installLegacyBundlesExtensions", - "eZ\\Bundle\\EzPublishLegacyBundle\\Composer\\ScriptHandler::generateAutoloads", - "eZ\\Bundle\\EzPublishLegacyBundle\\Composer\\ScriptHandler::symlinkLegacyFiles" - ], - "post-install-cmd": [ - ..., - "@legacy-scripts" - ], - "post-update-cmd": [ - ..., - "@legacy-scripts" - ], -} +composer require --update-no-dev "ezsystems/legacy-bridge" ``` -Example: In the case of stock eZ Platform 1.13 that would specifically be: +One installed run the following command to setup your install for legacy usage: ``` -"scripts": { - "legacy-scripts": [ - "eZ\\Bundle\\EzPublishLegacyBundle\\Composer\\ScriptHandler::installAssets", - "eZ\\Bundle\\EzPublishLegacyBundle\\Composer\\ScriptHandler::installLegacyBundlesExtensions", - "eZ\\Bundle\\EzPublishLegacyBundle\\Composer\\ScriptHandler::generateAutoloads", - "eZ\\Bundle\\EzPublishLegacyBundle\\Composer\\ScriptHandler::symlinkLegacyFiles" - ], - "symfony-scripts": [ - "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters", - "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", - "eZ\\Bundle\\EzPublishCoreBundle\\Composer\\ScriptHandler::clearCache", - "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets", - "@legacy-scripts", - "eZ\\Bundle\\EzPublishCoreBundle\\Composer\\ScriptHandler::dumpAssets", - "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile" - ], - "post-install-cmd": [ - "@symfony-scripts" - ], - "post-update-cmd": [ - "@symfony-scripts" - ], - (...) +php app/console ezpublish:legacy:init ``` +During it's execution it will advice you on which command to run **after** you have moved over your legacy files +_(extensions, settings and optionally designs)_. -### Enable EzPublishLegacyBundle -Edit `app/AppKernel.php`, and add `new eZ\Bundle\EzPublishLegacyBundle\EzPublishLegacyBundle( $this ),` -at the end of the `$bundles` array. Pay close attention to the `$this` argument, LegacyBundle is a bit -spoiled and has high expectations from its collaborators ;) +_TIP:_ +> If you are in need of applying these steps manually, see INSTALL.md from a 1.4 or a 2.0 version of LegacyBridge. -### Add legacy routes -Edit `app/config/routing.yml`, and add the LegacyBundle routes at the end of the file. - -``` -# NOTE: Always keep at the end of the file so native symfony routes always have precendence, to avoid legacy -# REST pattern overriding possible eZ Platform REST routes. -_ezpublishLegacyRoutes: - resource: @EzPublishLegacyBundle/Resources/config/routing.yml -``` ### Enable legacy_mode for your backoffice siteaccess The Legacy Backoffice requires the `legacy_mode` option to be enabled. -This can be done in app/config/config.yml or app/config/ezplatform.yml: + +This can be done in app/config/config.yml or app/config/ezplatform.yml, where `site_admin` is the name of the admin +siteaccess: ``` ez_publish_legacy: @@ -92,17 +51,9 @@ ezpublish_setup: ``` _Tip:_ -> Enabling Setup wizard is only needed if you intend to perform a new install with legacy demo data, you can also install Plaform data _(clean, demo)_ and afterwards when everything is setup use Platform UI to change Richtext FieldTypes to XmlText _(using [ezplatform-xmltext-fieldtype](https://github.com/ezsystems/ezplatform-xmltext-fieldtype))_, or install [Netgen RichTextDataType Bundle for legacy](https://github.com/netgen/NetgenRichTextDataTypeBundle) to make legacy allow raw editing of these. If you install eZ Platform Enterprise and it's demo data, there will also be Landing Page field type to handle in similar way _(contributions to Legacy Bridge on this more than welcome ;))_ +> Enabling Setup wizard is only needed if you intend to perform a new install with legacy demo data, you can also install Platform data _(clean, demo)_ and afterwards when everything is setup use Platform UI to change Richtext FieldTypes to XmlText _(using [ezplatform-xmltext-fieldtype](https://github.com/ezsystems/ezplatform-xmltext-fieldtype))_, or install [Netgen RichTextDataType Bundle for legacy](https://github.com/netgen/NetgenRichTextDataTypeBundle) to make legacy allow raw editing of these. If you install eZ Platform Enterprise and it's demo data, there will also be Landing Page field type to handle in similar way _(contributions to Legacy Bridge on this more than welcome ;))_ -### Install `ezsystems/legacy-bridge` - -Package can be installed using Composer in the following way: - -``` -composer require --update-no-dev "ezsystems/legacy-bridge:^1.5.0" -``` - ### Optional: Add missing legacy extensions Several ezpublish-legacy extensions are no longer installed by default with ezpublish-legacy package, such as ezfind or eztags. diff --git a/bundle/Command/LegacyInitCommand.php b/bundle/Command/LegacyInitCommand.php index 8501bac7..4861d985 100644 --- a/bundle/Command/LegacyInitCommand.php +++ b/bundle/Command/LegacyInitCommand.php @@ -12,7 +12,6 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\ConsoleOutputInterface; -use Symfony\Component\Filesystem\Filesystem; class LegacyInitCommand extends ContainerAwareCommand { @@ -25,26 +24,24 @@ protected function configure() new InputArgument('src', InputArgument::OPTIONAL, 'The src directory for legacy files', 'src/legacy_files'), ) ) - ->setDescription('Inits folders you can use when migrating from eZ Publish install to eZ Platform with Legacy Bridge') + ->setDescription('Inits Platform installation for legacy usage') ->setHelp( <<%command.name% creates the following folders for migration usage: -- src/AppBundle/ezpublish_legacy => Optionaly for extensions you want to version in your project source (as opposed to install using composer) -- src/legacy_files/design => Optionally for custom designs not already made part of an extension -- src/legacy_files/settings/override => Folder for override settings for legacy -- src/legacy_files/settings/siteaccess => Folder for siteaccess settings for legacy +The command %command.name% prepares install for use with eZ Publish legacy and LegacyBridge. +1. It creates the following folders which can be safely versioned, and symlinks them into ezpublish_legacy folder on composer install/update: +- src/AppBundle/ezpublish_legacy => Optionally for extensions you want to version in your project source (as opposed to install using composer) +- src/legacy_files/design => Optionally for custom designs not already made part of an extension +- src/legacy_files/settings/override => Folder for override settings for legacy +- src/legacy_files/settings/siteaccess => Folder for siteaccess settings for legacy +NOTE: You'll get instructions on which commands to run to setup symlinks once you have populated the folders. -src/legacy_files stored in your root project for -any design/extension/settings project files, and symlinks these into ezpublish_legacy/ which is installed by composer. +2. It configures @legacy-scrips in composer.json to make sure all needed scripts are executed on composer install/update -The benefit of this is: -1. Being able to version your design/extension/settings files in git without versioning legacy itself -2. A predefined convention for where to place these files when migrating from older versions -3. Placing these files directly in ezpublish_legacy folder will lead to them getting removed in some cases when composer - needs to completely replace ezpublish-legacy package for different reasons. +3. It enables EzPublishLegacyBundle and XmlTextFieldTypeBundle in app/AppKernel.php if needed + +4. It appends legacy routes to app/config/routing.yml if needed -NOTE: Once this is ran you'll get instructions on which commands to run to setup symlinks once you have populated the folders. EOT ); } @@ -53,6 +50,18 @@ protected function execute(InputInterface $input, OutputInterface $output) { $this->createDirectories($input, $output); $this->updateComposeJson($output); + $this->enableBundles($output); + $this->enableRoutes($output); + + $output->writeln(<<<'EOT' +All steps completed. + +You can now check changes done and start to move over any legacy files (see above). + +One done you can run the following command to setup symlinks, dump assets, (...): +- composer symfony-scripts +EOT + ); } protected function createDirectories(InputInterface $input, OutputInterface $output) @@ -74,18 +83,13 @@ protected function createDirectories(InputInterface $input, OutputInterface $out "$srcArg/settings/siteaccess", ]); - $output->writeln(<<writeln(<<<'EOT' The following folders should have been created (or where already present): -- src/AppBundle/ezpublish_legacy (for extensions) -- src/legacy_files/design -- src/legacy_files/settings/override -- src/legacy_files/settings/siteaccess - -Move over files and directories from older installation and afterwards run the following command to setup symlinks into -legacy install: -- composer legacy-scripts - +- src/AppBundle/ezpublish_legacy (for extensions) +- src/legacy_files/design (optional if you have a design which is not provided by an extension) +- src/legacy_files/settings/override +- src/legacy_files/settings/siteaccess EOT ); } @@ -115,7 +119,7 @@ protected function updateComposeJson(OutputInterface $output) if (!array_key_exists('symfony-scripts', $composerJson['scripts'])) { $composerJson['scripts']['symfony-scripts'] = ['@legacy-scripts']; $updateComposerJson = true; - } elseif (!in_array('@legacy-scripts', $composerJson['scripts']['symfony-scripts'])) { + } elseif (!\in_array('@legacy-scripts', $composerJson['scripts']['symfony-scripts'])) { $symfonyScripts = $composerJson['scripts']['symfony-scripts']; $offset = array_search('@php bin/console assetic:dump', $symfonyScripts); @@ -125,7 +129,7 @@ protected function updateComposeJson(OutputInterface $output) if ($offset === false) { $errOutput->writeln('Warning : Unable to find "assetic:dump / ScriptHandler::dumpAsset" in [symfony-scripts], putting "@legacy_scripts" at the end of array'); - $offset = count($symfonyScripts); + $offset = \count($symfonyScripts); } } @@ -138,4 +142,89 @@ protected function updateComposeJson(OutputInterface $output) file_put_contents('composer.json', json_encode($composerJson, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL); } } + + protected function enableBundles(OutputInterface $output) + { + $errOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output; + if (!$text = file_get_contents('app/AppKernel.php')) { + $errOutput->writeln('Error: Unable to load app/AppKernel.php'); + + return; + } + + $changed = false; + $lines = preg_split('/\r\n|\r|\n/', $text); + $appOffset = false; + foreach ($lines as $i => $line) { + if (stripos($line, 'new AppBundle\AppBundle()') !== false) { + $appOffset = $i; + // Make sure element has comma (",") at end, however don't mark as $changed + if (stripos($line, 'new AppBundle\AppBundle(),') === false) { + $lines[$i] = $line . ','; + } + + break; + } + } + + if (!$appOffset) { + $errOutput->writeln('Error: Could not find "new AppBundle\AppBundle()" in app/AppKernel.php'); + + return; + } + + // Enable Bundles (if needed) + if (stripos($text, 'EzPublishLegacyBundle') === false) { + // Add legacy bundle to the end (on purpose without indenting) + array_splice( + $lines, + $appOffset + 1, + 0, + 'new eZ\Bundle\EzPublishLegacyBundle\EzPublishLegacyBundle( $this ),' + ); + $changed = true; + } + + if (stripos($text, 'EzSystemsEzPlatformXmlTextFieldTypeBundle') === false) { + // Add XmlText bundle bundle before AppBundle (on purpose without indenting) + array_splice( + $lines, + trim($lines[$appOffset - 1]) === '// Application' ? $appOffset - 1 : $appOffset, + 0, + 'new EzSystems\EzPlatformXmlTextFieldTypeBundle\EzSystemsEzPlatformXmlTextFieldTypeBundle(),' + ); + $changed = true; + } + + if ($changed) { + file_put_contents('app/AppKernel.php', implode(PHP_EOL, $lines)); + } + } + + protected function enableRoutes(OutputInterface $output) + { + $errOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output; + if (!$text = file_get_contents('app/config/routing.yml')) { + $errOutput->writeln('Error: Unable to load app/config/routing.yml'); + + return; + } + + $changed = false; + if (stripos($text, 'resource: @EzPublishLegacyBundle/Resources/config/routing.yml') === false) { + // Add routes to the end of routes file + $text .= <<<'EOT' + +# NOTE: ALWAYS keep this at the end of your routing rules so native symfony routes have precedence +# To avoid legacy REST pattern overriding possible eZ Platform REST routes and so on. +_ezpublishLegacyRoutes: + resource: @EzPublishLegacyBundle/Resources/config/routing.yml +EOT; + $changed = true; + } + + if ($changed) { + file_put_contents('app/config/routing.yml', $text); + } + } }