Skip to content

Commit

Permalink
Adapt AppKernel.hp and routing.yml file in init
Browse files Browse the repository at this point in the history
  • Loading branch information
andrerom committed Feb 18, 2019
1 parent 4652b0e commit 93305be
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 93 deletions.
83 changes: 17 additions & 66 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down
143 changes: 116 additions & 27 deletions bundle/Command/LegacyInitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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(
<<<EOT
The command <info>%command.name%</info> 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 <info>%command.name%</info> 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:
- <info>src/AppBundle/ezpublish_legacy</info> => Optionally for extensions you want to version in your project source (as opposed to install using composer)
- <info>src/legacy_files/design</info> => Optionally for custom designs not already made part of an extension
- <info>src/legacy_files/settings/override</info> => Folder for override settings for legacy
- <info>src/legacy_files/settings/siteaccess</info> => Folder for siteaccess settings for legacy
<comment>NOTE: You'll get instructions on which commands to run to setup symlinks once you have populated the folders.</comment>
<info>src/legacy_files</info> stored in your root project for
any design/extension/settings project files, and symlinks these into <info>ezpublish_legacy/</info> which is installed by composer.
2. It configures <info>@legacy-scrips</info> in composer.json to make sure all needed scripts are executed on <info>composer install/update</info>
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 <info>EzPublishLegacyBundle</info> and <info>XmlTextFieldTypeBundle</info> in <info>app/AppKernel.php</info> if needed
4. It appends legacy routes to <info>app/config/routing.yml</info> if needed
<comment>NOTE: Once this is ran you'll get instructions on which commands to run to setup symlinks once you have populated the folders.</comment>
EOT
);
}
Expand All @@ -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, (...):
- <info>composer symfony-scripts</info>
EOT
);
}

protected function createDirectories(InputInterface $input, OutputInterface $output)
Expand All @@ -74,18 +83,13 @@ protected function createDirectories(InputInterface $input, OutputInterface $out
"$srcArg/settings/siteaccess",
]);

$output->writeln(<<<EOT
$output->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:
- <info>composer legacy-scripts</info>
- <info>src/AppBundle/ezpublish_legacy</info> (for extensions)
- <info>src/legacy_files/design</info> (optional if you have a design which is not provided by an extension)
- <info>src/legacy_files/settings/override</info>
- <info>src/legacy_files/settings/siteaccess</info>
EOT
);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}
}

0 comments on commit 93305be

Please sign in to comment.