This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/expressive-2.2-migration'
Close #71
- Loading branch information
Showing
8 changed files
with
746 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.