Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform to laminas module #8

Merged
merged 4 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/nbproject/
/vendor/
/.idea/
/.idea/
composer.lock
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ matrix:
- php: 7.3
env:
- EXECUTE_SCRUTINIZER=true
- php: 7.0
env:
- PREFER_LOWEST=true
- XDEBUG_MODE=coverage

before_script:
- if [ -z "$PREFER_LOWEST" ]; then composer update --no-interaction; fi
Expand Down
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# MaglLegacyApplication - Legacy Applications in ZF2
# MaglLegacyApplication - Legacy Applications in Laminas

[![Latest Stable Version](https://poser.pugx.org/maglnet/magl-legacy-application/v/stable.svg)](https://packagist.org/packages/maglnet/magl-legacy-application)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/maglnet/MaglLegacyApplication/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/maglnet/MaglLegacyApplication/?branch=master)
[![License](https://poser.pugx.org/maglnet/magl-legacy-application/license.svg)](https://packagist.org/packages/maglnet/magl-legacy-application)
[![Build Status](https://travis-ci.org/maglnet/MaglLegacyApplication.svg?branch=master)](https://travis-ci.org/maglnet/MaglLegacyApplication)
[![Code Coverage](https://scrutinizer-ci.com/g/maglnet/MaglLegacyApplication/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/maglnet/MaglLegacyApplication/?branch=master)

Run your legacy applications within Zend Framework 2.
Run your legacy applications within Laminas.

## Introduction
Since rewriting your legacy application from scratch to a ZF2 application could be nearly impossible
due to time, effort, and resources, i was searching for a way to migrate a legacy application to a ZF2 application.
Since rewriting your legacy application from scratch to a Laminas application could be nearly impossible
due to time, effort, and resources, I was searching for a way to migrate a legacy application to a Laminas application.

A [great article by Chris Abernethy][1] described a way on how to run your legacy application within ZF1, so
I migrated this HowTo to a small ZF2 module, to be able to run a legacy application within ZF2.
I migrated this HowTo to a small Laminas module, to be able to run a legacy application within Laminas.

While running your legacy application within a ZF2 application it is possible to slowly migrate an existing application
to ZF2 by leaving your old application (nearly - see "Adjust your legacy Application") untouched and build new modules
with the power of ZF2.
By adding a simple wrapper (see "Using ZF2 within your legacy application") you could also use the new modules
While running your legacy application within a Laminas application it is possible to slowly migrate an existing application
to Laminas by leaving your old application (nearly - see "Adjust your legacy Application") untouched and build new modules
with the power of Laminas.
By adding a simple wrapper (see "Using Laminas within your legacy application") you could also use the new modules
within your legacy application.


Expand All @@ -32,19 +32,19 @@ Install through composer
}
```

Enable the module within your ZF2 `application.config.php`
Enable the module within your Laminas `application.config.php`
```php
'modules' => array(
'Application',
'MaglLegacyApplication',
),
```

Copy the provided file `data/magl-zf2-legacy-wrapper.php` to your `public/` folder.
Copy the provided file `data/magl-laminas-legacy-wrapper.php` to your `public/` folder.
Copy the provided file `data/.htaccess` to your publix folder.
Copy your legacy Application to your `public/` folder.

Your legacy application should now run within ZF2. :)
Your legacy application should now run within Laminas. :)


## Configuration
Expand All @@ -54,9 +54,9 @@ routes to legacy controller and remove the mod_rewrite rules from your `.htacces
return array(
'router' => array(
'routes' => array(
// example for transferring mod rewrite rules to zf2 routes
// example for transferring mod rewrite rules to laminas routes
'legacy-seo-calendar' => array(
'type' => 'Zend\Mvc\Router\Http\Regex',
'type' => 'Laminas\Mvc\Router\Http\Regex',
'options' => array(
'regex' => '/calendar/(?<foo>.+)',
'defaults' => array(
Expand All @@ -76,8 +76,8 @@ return array(
There are several cases in which your legacy application won't run without additional adjustments, here are some of them:

### Relative Paths
Using relative paths for `require`, `require_once` or `includes` will possibly fail now, since ZF2 will do a `chdir()`
to the ZF2's application root. So you will need to adjust your paths to match the new root.
Using relative paths for `require`, `require_once` or `includes` will possibly fail now, since Laminas will do a `chdir()`
to the Laminas' application root. So you will need to adjust your paths to match the new root.

Example:

Expand Down Expand Up @@ -112,7 +112,7 @@ $script_name = $legacy->getLegacyScriptName();

```

## Using ZF2 within your legacy application
## Using Laminas within your legacy application
```php
use MaglLegacyApplication\Application\MaglLegacy;
$application = MaglLegacy::getInstance()->getApplication();
Expand All @@ -121,12 +121,12 @@ $yourService = $application->getServiceManager()->get('YourService');

## Injecting responses from within your legacy application
from wherever you are within your legacy application, it is possible to bypass your legacy applications controller code
and send a response to the ZF2 Controller wrapper. This response will then be handled like within a normal ZF2 controller.
and send a response to the Laminas Controller wrapper. This response will then be handled like within a normal Laminas controller.
```php
use MaglLegacyApplication\Application\MaglLegacy;
$application = MaglLegacy::getInstance()->getApplication();
$application->getEventManager()->getSharedManager()->attach('*', MaglLegacy::EVENT_SHORT_CIRCUIT_RESPONSE, function(Event $e){
$response = new \Zend\Http\Response();
$response = new \Laminas\Http\Response();
$response->setStatusCode(404);
$response->setContent('not found');
$e->stopPropagation(true);
Expand All @@ -138,4 +138,4 @@ $application->getEventManager()->getSharedManager()->attach('*', MaglLegacy::EVE
If you have questions or problems regarding this module just open an issue or, even better,
solve it and open a pull request. :+1:

[1]: http://www.chrisabernethy.com/zend-framework-legacy-scripts/
[1]: http://www.chrisabernethy.com/zend-framework-legacy-scripts/
30 changes: 19 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{
"name": "maglnet/magl-legacy-application",
"type": "library",
"description": "Provides a ZF2 module that is able to run legacy applications with the power of ZF2.",
"description": "Provides a laminas module that is able to run legacy applications with the power of laminas.",
"homepage": "https://github.com/maglnet/MaglLegacyApplication",
"license": "BSD-3-Clause",
"keywords": ["legacy", "legacy application", "zend framework", "zf2"],
"keywords": [
"legacy",
"legacy application",
"laminas"
],
"support": {
"source": "https://github.com/maglnet/MaglLegacyApplication",
"issues": "https://github.com/maglnet/MaglLegacyApplication/issues"
Expand All @@ -18,18 +22,22 @@
],
"require": {
"php": "^7.0",
"zendframework/zendframework": "^2.5 || ^3.0",
"zendframework/zend-servicemanager": "^2.6 || ^3.0",
"zendframework/zend-mvc": "^2.5 || ^3.0"
"laminas/laminas-eventmanager": "^3.0",
"laminas/laminas-http": "^2.0",
"laminas/laminas-mvc": "^3.0",
"laminas/laminas-router": "^3.0",
"laminas/laminas-servicemanager": "^3.0",
"laminas/laminas-stdlib": "^3.0",
"laminas/laminas-view": "^2.5"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"zendframework/zend-modulemanager": "^2.5",
"zendframework/zend-view": "^2.5",
"zendframework/zend-router": "^2.5 || ^3.3"
"laminas/laminas-modulemanager": "^2.5",
"laminas/laminas-test": "^3.0",
"phpunit/phpunit": "^5.7"
},
"autoload": {
"classmap": ["src"],
"psr-0": {"MaglLegacyApplication": "src"}
"psr-4": {
"MaglLegacyApplication\\": "src/MaglLegacyApplication"
}
}
}
2 changes: 1 addition & 1 deletion config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
),
// example for transferring mod rewrite rules to zf2 routes
// 'legacy-seo-calendar' => array(
// 'type' => 'Zend\Mvc\Router\Http\Regex',
// 'type' => 'Laminas\Mvc\Router\Http\Regex',
// 'options' => array(
// 'regex' => '/calendar/(?<foo>.+)',
// 'defaults' => array(
Expand Down
2 changes: 1 addition & 1 deletion data/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{ENV:BASE}magl-zf2-legacy-wrapper.php [NC,L]
RewriteRule ^(.*)$ %{ENV:BASE}magl-laminas-legacy-wrapper.php [NC,L]

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
/**
* This is the index.php from the zf2 skeleton application
* it was renamed to index-zf2-wrapper.php since the
* it was renamed to magl-laminas-legacy-wrapper.php since the
* legacy application could have (and possibly already has) an index.php
*
* additionally it uses MaglLegacyApplication::run() to be able to access the ZF2 mvc application from
* additionally it uses MaglLegacyApplication::run() to be able to access the Laminas mvc application from
* within your legacy application
*/

Expand All @@ -22,6 +22,6 @@
// you do not need this, if MaglLegacyApplication is installed through composer
require_once realpath(__DIR__ . '/../module/MaglLegacyApplication/src/MaglLegacyApplication/Application/MaglLegacy.php');

$application = Zend\Mvc\Application::init(require 'config/application.config.php');
$application = Laminas\Mvc\Application::init(require 'config/application.config.php');
\MaglLegacyApplication\Application\MaglLegacy::getInstance()->setApplication($application);
$application->run();
18 changes: 11 additions & 7 deletions src/MaglLegacyApplication/Application/MaglLegacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

namespace MaglLegacyApplication\Application;

use Laminas\EventManager\EventManagerInterface;
use Laminas\Mvc\ApplicationInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;

class MaglLegacy
{

Expand All @@ -20,7 +24,7 @@ class MaglLegacy

/**
*
* @var \Zend\Mvc\ApplicationInterface
* @var ApplicationInterface
*/
private $application;

Expand Down Expand Up @@ -65,14 +69,14 @@ private function __clone()
return null;
}

public function setApplication(\Zend\Mvc\ApplicationInterface $application)
public function setApplication(ApplicationInterface $application)
{
$this->application = $application;
}

/**
*
* @return \Zend\Mvc\ApplicationInterface
* @return ApplicationInterface
*/
public function getApplication()
{
Expand All @@ -81,7 +85,7 @@ public function getApplication()

/**
*
* @return \Zend\ServiceManager\ServiceLocatorInterface
* @return ServiceLocatorInterface
*/
public static function getServiceManager()
{
Expand All @@ -90,7 +94,7 @@ public static function getServiceManager()

/**
*
* @return \Zend\EventManager\EventManagerInterface
* @return EventManagerInterface
*/
public static function getEventManager()
{
Expand All @@ -108,7 +112,7 @@ public function getLegacyScriptFilename()

/**
*
* @param string $legacyScriptFilename
* @param string $legacyScriptFilename
* @return boolean true, if the script filenamename was set, false otherwise, e.g. it has already been set
*/
public function setLegacyScriptFilename($legacyScriptFilename)
Expand All @@ -127,7 +131,7 @@ public function getLegacyScriptName()

/**
*
* @param string $legacyScriptName
* @param string $legacyScriptName
* @return boolean true, if the script name was set, false otherwise, e.g. it has already been set
*/
public function setLegacyScriptName($legacyScriptName)
Expand Down
16 changes: 7 additions & 9 deletions src/MaglLegacyApplication/Controller/LegacyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

namespace MaglLegacyApplication\Controller;

use Laminas\Http\Response;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
use MaglLegacyApplication\Application\MaglLegacy;
use MaglLegacyApplication\Options\LegacyControllerOptions;
use Zend\Http\Response;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class LegacyController extends AbstractActionController
{
Expand All @@ -37,7 +37,7 @@ public function __construct(LegacyControllerOptions $options, MaglLegacy $legacy
public function indexAction()
{
$docRoots = $this->options->getDocRoots();
foreach($docRoots as $key => $docRoot) {
foreach ($docRoots as $key => $docRoot) {
$docRoots[$key] = rtrim(getcwd() . '/' . $docRoot);
}

Expand All @@ -46,7 +46,7 @@ public function indexAction()
if (empty($scriptName)) {
$path = $this->params(('path')) ? $this->params('path') : '';
foreach ($this->options->getIndexFiles() as $indexFile) {
foreach($docRoots as $docRoot) {
foreach ($docRoots as $docRoot) {
if (is_file($docRoot . '/' . $path . $indexFile)) {
$this->legacy->setLegacyScriptName($path . $indexFile);
return $this->runScript($docRoot . '/' . $path . $indexFile);
Expand All @@ -55,11 +55,10 @@ public function indexAction()
}
}


$scriptUri = '/' . ltrim($scriptName, '/'); // force leading '/'
foreach($docRoots as $docRoot) {
foreach ($docRoots as $docRoot) {
$legacyScriptFilename = $docRoot . $scriptUri;
if(is_file($legacyScriptFilename)) {
if (is_file($legacyScriptFilename)) {
//inform the application about the used script
$this->legacy->setLegacyScriptName($scriptUri);

Expand Down Expand Up @@ -102,7 +101,6 @@ private function setGetVariables()
$routeParams = $this->getEvent()->getRouteMatch()->getParams();

foreach ($routeParams as $paramName => $paramValue) {

if ($globals_options['get'] && !isset($_GET[$paramName])) {
$_GET[$paramName] = $paramValue;
}
Expand Down
Loading