Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/expressive-2.2-migration'
Browse files Browse the repository at this point in the history
Close #71
  • Loading branch information
weierophinney committed Mar 12, 2018
2 parents 84597b5 + 8cb6d0e commit ed7a89e
Show file tree
Hide file tree
Showing 8 changed files with 746 additions and 0 deletions.
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,35 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 0.4.7 - 2018-03-12

### Added

- [#71](https://github.com/zendframework/zend-expressive-tooling/pull/71) adds
the new command `migrate:expressive-v2.2`. This command does the following:

- Adds `Zend\Expressive\Router\ConfigProvider` to `config/config.php`.
- Adds `Zend\Expressive\ConfigProvider` to `config/config.php`.
- Replaces `pipeRoutingMiddleware()` calls with `pipe(\Zend\Expressive\Router\Middleware\RouteMiddleware::class)`.
- Replaces `pipeDispatchMiddleware()` calls with `pipe(\Zend\Expressive\Router\Middleware\DispatchMiddleware::class)`.
- Replaces `pipe()` calls that pipe `Implicit*Middleware` to reference zend-expressive-router variants.

### Changed

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Nothing.

## 0.4.6 - 2018-01-29

### Added
Expand Down
1 change: 1 addition & 0 deletions bin/expressive
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ $application = new Application('expressive', VERSION);
$application->addCommands([
new CreateMiddleware\CreateMiddlewareCommand('middleware:create'),
new GenerateProgrammaticPipelineFromConfig\PipelineFromConfigCommand('migrate:pipeline'),
new MigrateExpressive22\MigrateExpressive22Command('migrate:expressive-v2.2'),
new MigrateOriginalMessageCalls\MigrateOriginalMessageCallsCommand('migrate:original-messages'),
new ScanForErrorMiddleware\ScanForErrorMiddlewareCommand('migrate:error-middleware-scanner'),
new Module\CreateCommand('module:create'),
Expand Down
94 changes: 94 additions & 0 deletions src/MigrateExpressive22/MigrateExpressive22Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive-tooling for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-expressive-tooling/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Expressive\Tooling\MigrateExpressive22;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class MigrateExpressive22Command extends Command
{
const HELP = <<< 'EOT'
Migrate an Expressive application to version 2.2.
This command does the following:
- Adds entries for the zend-expressive and zend-expressive-router
ConfigProvider classes to config/config.php
- Updates entries to pipeRoutingMiddleware() to instead pipe the
zend-expressive-router RouteMiddleware.
- Updates entries to pipeDispatchMiddleware() to instead pipe the
zend-expressive-router DispatchMiddleware.
- Updates entries to pipe the various Implicit*Middleware to pipe the
new zend-expressive-router versions.
These changes are made to prepare your application for version 3, and to remove
known deprecation messages.
EOT;

/**
* @var null|string Root path of the application being updated; defaults to $PWD
*/
private $projectDir;

/**
* @var null|string Project root in which to make updates.
*/
public function setProjectDir($path)
{
$this->projectDir = $path;
}

/**
* Configure the console command.
*/
protected function configure()
{
$this->setDescription('Migrate an Expressive application to version 2.2.');
$this->setHelp(self::HELP);
}

/**
* Execute console command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int Exit status
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$projectDir = $this->getProjectDir();

$output->writeln('<info>Migrating application to Expressive 2.2...</info>');

$output->writeln('<info>- Updating config/config.php</info>');
$updateConfig = new UpdateConfig();
$updateConfig($output, $projectDir);

$output->writeln('<info>- Updating config/pipeline.php</info>');
$updatePipeline = new UpdatePipeline();
$updatePipeline($output, $projectDir);

$output->writeln('<info>Done!</info>');

return 0;
}

/**
* Retrieve the project root directory.
*
* Uses result of getcwd() if not previously set.
*
* @return string
*/
private function getProjectDir()
{
return $this->projectDir ?: realpath(getcwd());
}
}
47 changes: 47 additions & 0 deletions src/MigrateExpressive22/UpdateConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive-tooling for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-expressive-tooling/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Expressive\Tooling\MigrateExpressive22;

use Symfony\Component\Console\Output\OutputInterface;

class UpdateConfig
{
/**
* @param string $projectPath
* @return void
*/
public function __invoke(OutputInterface $output, $projectPath)
{
$filename = sprintf('%s/config/config.php', $projectPath);
$contents = file_get_contents($filename);

$components = [
\Zend\Expressive\Router\ConfigProvider::class,
\Zend\Expressive\ConfigProvider::class,
];

$pattern = sprintf(
"/(new (?:%s?%s)?ConfigAggregator\(\s*(?:array\(|\[)\s*)(?:\r|\n|\r\n)(\s*)/",
preg_quote('\\'),
preg_quote('Zend\ConfigAggregator\\')
);

$replacementTemplate = "\$1\n\$2\\%s::class,\n\$2";

foreach ($components as $component) {
$output->writeln(sprintf(
'<info>Adding %s to config/config.php</info>',
$component
));
$replacement = sprintf($replacementTemplate, $component);
$contents = preg_replace($pattern, $replacement, $contents);
}

file_put_contents($filename, $contents);
}
}
70 changes: 70 additions & 0 deletions src/MigrateExpressive22/UpdatePipeline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive-tooling for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-expressive-tooling/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Expressive\Tooling\MigrateExpressive22;

use Symfony\Component\Console\Output\OutputInterface;

class UpdatePipeline
{
// @codingStandardsIgnoreStart
/**
* PCRE strings to match, and their replacements.
* @var string[]
*/
private $matches = [
'/-\>pipeRoutingMiddleware\(\)/' => '->pipe(\Zend\Expressive\Router\Middleware\RouteMiddleware::class)',
'/-\>pipeDispatchMiddleware\(\)/' => '->pipe(\Zend\Expressive\Router\Middleware\DispatchMiddleware::class)',
'/-\>pipe\(.*?(Implicit(Head|Options)Middleware).*?\)/' => '->pipe(\Zend\Expressive\Router\Middleware\\\\$1::class)'
];
// @codingStandardsIgnoreEnd

/**
* @param string $projectPath
* @return void
*/
public function __invoke(OutputInterface $output, $projectPath)
{
$filename = sprintf('%s/config/pipeline.php', $projectPath);
$contents = '';
$fh = fopen($filename, 'r+');

while (! feof($fh)) {
if (false === ($line = fgets($fh))) {
break;
}

$contents .= $this->matchAndReplace($output, $line);
}

fclose($fh);

file_put_contents($filename, $contents);
}

/**
* @param string $line
* @return string
*/
private function matchAndReplace(OutputInterface $output, $line)
{
$updated = $line;
foreach ($this->matches as $pattern => $replacement) {
$updated = preg_replace($pattern, $replacement, $updated);
}

if ($updated !== $line) {
$output->writeln(sprintf(
'<info>Rewrote line "%s" to "%s"</info>',
$line,
$updated
));
}

return $updated;
}
}
Loading

0 comments on commit ed7a89e

Please sign in to comment.