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

Adds typehints compatibility #20

Merged
merged 3 commits into from
Apr 25, 2019
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
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
"test/classes.php"
],
"psr-4": {
"LaminasTest\\ZendFrameworkBridge\\": "test//"
"LaminasTest\\ZendFrameworkBridge\\": "test//",
"Apigility\\": "test//TestAsset//Apigility//",
"Expressive\\": "test//TestAsset//Expressive//",
"Laminas\\": "test//TestAsset//Laminas//"
}
},
"extra": {
Expand Down
61 changes: 57 additions & 4 deletions src/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,61 @@

namespace Laminas\ZendFrameworkBridge;

use Composer\Autoload\ClassLoader;
use RuntimeException;

/**
* Alias legacy Zend Framework project classes/interfaces/traits to Laminas equivalents.
*/
class Autoloader
{
/**
* This autoloader is _appended_, so a mix of legacy and Laminas classes
* can be used.
* We attach two autoloaders:
* - _prepend_ to handle new classes and add aliases for legacy classes.
* This is required to keep typehints compatibility.
* - _append_ to handle legacy classes and alias them to new classes.
*/
public static function load()
{
static $loaded = array();

$classLoader = self::getClassLoader();

$namespaces = RewriteRules::namespaceReverse();
spl_autoload_register(function ($class) use ($namespaces, $classLoader, &$loaded) {
if (isset($loaded[$class])) {
return;
}

$segments = explode('\\', $class);

$i = 0;
$check = '';

while (isset($segments[$i + 1], $namespaces[$check . $segments[$i] . '\\'])) {
$check .= $segments[$i] . '\\';
++$i;
}

if ($check === '') {
return;
}

if ($classLoader->loadClass($class)) {
$legacy = $namespaces[$check] . str_replace('Laminas', 'Zend', substr($class, strlen($check)));
class_alias($class, $legacy);
}
}, true, true);

$namespaces = RewriteRules::namespaceRewrite();
spl_autoload_register(function ($class) use ($namespaces) {
spl_autoload_register(function ($class) use ($namespaces, &$loaded) {
$segments = explode('\\', $class);

$i = 0;
$check = '';

// We are checking segments of the namespace to match quicker
while (isset($namespaces[$check . $segments[$i] . '\\'])) {
while (isset($segments[$i + 1], $namespaces[$check . $segments[$i] . '\\'])) {
$check .= $segments[$i] . '\\';
++$i;
}
Expand All @@ -37,9 +72,27 @@ public static function load()

$alias = $namespaces[$check] . str_replace('Zend', 'Laminas', substr($class, strlen($check)));

$loaded[$alias] = true;
if (class_exists($alias) || interface_exists($alias) || trait_exists($alias)) {
class_alias($alias, $class);
}
});
}

/**
* @return ClassLoader
* @throws RuntimeException
*/
private static function getClassLoader()
{
if (file_exists(__DIR__ . '/../../../autoload.php')) {
return include __DIR__ . '/../../../autoload.php';
}

if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
return include __DIR__ . '/../vendor/autoload.php';
}

throw new RuntimeException('Cannot detect composer autoload. Please run composer install');
}
}
41 changes: 41 additions & 0 deletions src/RewriteRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,45 @@ public static function namespaceRewrite()
'ZendDiagnostics\\' => 'Laminas\\Diagnostics\\',
);
}

/**
* @return array
*/
public static function namespaceReverse()
{
return array(
// ZendXml, ZendOAuth, ZendDiagnostics
'Laminas\\Xml\\' => 'ZendXml\\',
'Laminas\\OAuth\\' => 'ZendOAuth\\',
'Laminas\\Diagnostics\\' => 'ZendDiagnostics\\',

// Zend Service
'Laminas\\Amazon\\' => 'ZendService\\Amazon\\',
'Laminas\\Apple\\' => 'ZendService\\Apple\\',
'Laminas\\Google\\' => 'ZendService\\Google\\',
'Laminas\\ReCaptcha\\' => 'ZendService\\ReCaptcha\\',
'Laminas\\Twitter\\' => 'ZendService\\Twitter\\',

// Zend
'Laminas\\' => 'Zend\\',

// Expressive
'Expressive\\ProblemDetails\\' => 'Zend\\ProblemDetails\\',
'Expressive\\' => 'Zend\\Expressive\\',

// Laminas to ZfCampus
'Laminas\\ComposerAutoloading\\' => 'ZF\\ComposerAutoloading\\',
'Laminas\\Deploy\\' => 'ZF\\Deploy\\',
'Laminas\\DevelopmentMode\\' => 'ZF\\DevelopmentMode\\',

// Apigility
'Apigility\\Admin\\' => 'ZF\\Apigility\\Admin\\',
'Apigility\\Doctrine\\' => 'ZF\\Apigility\\Doctrine\\',
'Apigility\\Documentation\\' => 'ZF\\Apigility\\Documentation\\',
'Apigility\\Example\\' => 'ZF\\Apigility\\Example\\',
'Apigility\\Provider\\' => 'ZF\\Apigility\\Provider\\',
'Apigility\\Welcome\\' => 'ZF\\Apiglity\\Welcome\\',
'Apigility\\' => 'ZF\\',
);
}
}
61 changes: 61 additions & 0 deletions test/AutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@

namespace LaminasTest\ZendFrameworkBridge;

use Laminas\LegacyTypeHint;
use PHPUnit\Framework\TestCase;

class AutoloaderTest extends TestCase
{
/**
* @return array[]
*/
public function classProvider()
{
return array(
Expand All @@ -28,6 +32,7 @@ public function classProvider()
array('Zend\Expressive\ZendView\ZendViewRenderer', 'Expressive\LaminasView\LaminasViewRenderer'),
array('Zend\ProblemDetails\ProblemDetails', 'Expressive\ProblemDetails\ProblemDetails'),
// Laminas
array('Zend\Expressive', 'Laminas\Expressive'),
array('Zend\Main', 'Laminas\Main'),
array('Zend\Psr7Bridge\Psr7Bridge', 'Laminas\Psr7Bridge\Psr7Bridge'),
array('Zend\Psr7Bridge\ZendBridge', 'Laminas\Psr7Bridge\LaminasBridge'),
Expand Down Expand Up @@ -56,4 +61,60 @@ public function testLegacyClassIsAliasToLaminas($legacy, $actual)
self::assertTrue(class_exists($legacy));
self::assertSame($actual, get_class(new $legacy()));
}

public function testTypeHint()
{
self::assertTrue(class_exists('Laminas\LegacyTypeHint'));
new LegacyTypeHint(new \Laminas\Example());
}

/**
* @return array[]
*/
public function reverseClassProvider()
{
return array(
// Apigility
array('Apigility\Admin\Example', 'ZF\Apigility\Admin\Example'),
array('Apigility\Doctrine\Example', 'ZF\Apigility\Doctrine\Example'),
array('Apigility\Documentation\Example', 'ZF\Apigility\Documentation\Example'),
array('Apigility\Example\Example', 'ZF\Apigility\Example\Example'),
array('Apigility\Provider\Example', 'ZF\Apigility\Provider\Example'),
array('Apigility\Welcome\Example', 'ZF\Apigility\Welcome\Example'),
array('Apigility\Other\Example', 'ZF\Other\Example'),
array('Apigility\Example', 'ZF\Example'),

// Expressive
array('Expressive\ProblemDetails\Example', 'Zend\ProblemDetails\Example'),
array('Expressive\Other\Example', 'Zend\Expressive\Other\Example'),
array('Expressive\Example', 'Zend\Expressive\Example'),

// Laminas
array('Laminas\Amazon\Example', 'ZendService\Amazon\Example'),
array('Laminas\Apple\Example', 'ZendService\Apple\Example'),
array('Laminas\Google\Example', 'ZendService\Google\Example'),
array('Laminas\ReCaptcha\Example', 'ZendService\ReCaptcha\Example'),
array('Laminas\Twitter\Example', 'ZendService\Twitter\Example'),
array('Laminas\ComposerAutoloading\Example', 'ZF\ComposerAutoloading\Example'),
array('Laminas\Deploy\Example', 'ZF\Deploy\Example'),
array('Laminas\DevelopmentMode\Example', 'ZF\DevelopmentMode\Example'),
array('Laminas\Diagnostics\Example', 'ZendDiagnostics\Example'),
array('Laminas\OAuth\Example', 'ZendOAuth\Example'),
array('Laminas\Xml\Example', 'ZendXml\Example'),
array('Laminas\Other\LaminasExample', 'Zend\Other\ZendExample'),
array('Laminas\Other\Example', 'Zend\Other\Example'),
array('Laminas\Example', 'Zend\Example'),
);
}

/**
* @dataProvider reverseClassProvider
* @param string $actual
* @param string $legacy
*/
public function testReverseAliasCreated($actual, $legacy)
{
self::assertTrue(class_exists($actual));
self::assertTrue(class_exists($legacy));
}
}
7 changes: 7 additions & 0 deletions test/TestAsset/Apigility/Admin/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Apigility\Admin;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Apigility/Doctrine/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Apigility\Doctrine;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Apigility/Documentation/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Apigility\Documentation;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Apigility/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Apigility;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Apigility/Example/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Apigility\Example;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Apigility/Other/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Apigility\Other;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Apigility/Provider/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Apigility\Provider;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Apigility/Welcome/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Apigility\Welcome;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Expressive/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Expressive;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Expressive/Other/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Expressive\Other;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Expressive/ProblemDetails/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Expressive\ProblemDetails;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Laminas/Amazon/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Laminas\Amazon;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Laminas/Apple/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Laminas\Apple;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Laminas/ComposerAutoloading/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Laminas\ComposerAutoloading;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Laminas/Deploy/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Laminas\Deploy;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Laminas/DevelopmentMode/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Laminas\DevelopmentMode;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Laminas/Diagnostics/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Laminas\Diagnostics;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Laminas/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Laminas;

class Example
{
}
7 changes: 7 additions & 0 deletions test/TestAsset/Laminas/Google/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Laminas\Google;

class Example
{
}
10 changes: 10 additions & 0 deletions test/TestAsset/Laminas/LegacyTypeHint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Laminas;

class LegacyTypeHint
{
public function __construct(\Zend\Example $example)
{
}
}
Loading