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

Rewrite autoloader so we have all rules defined in separate class #4

Merged
merged 8 commits into from
Apr 1, 2019
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/composer.lock export-ignore
/docs/ export-ignore
/mkdocs.yml export-ignore
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/composer.lock
/docs/html/
/vendor/
/laminas-mkdoc-theme.tgz
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
"autoload": {
"files": [
"src/autoload.php"
]
],
"psr-4": {
"Laminas\\ZendFrameworkBridge\\": "src//"
}
},
"extra": {
"branch-alias": {
Expand Down
39 changes: 39 additions & 0 deletions src/Autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* @see https://github.com/laminas/laminas-zendframework-bridge for the canonical source repository
* @copyright Copyright (c) 2019 Laminas Foundation (https://getlaminas.org)
* @license https://github.com/laminas/laminas-zendframework-bridge/blob/master/LICENSE.md New BSD License
*/

namespace Laminas\ZendFrameworkBridge;

/**
* Alias legacy Zend Framework project classes/interfaces/traits to Laminas equivalents.
*/
class Autoloader
{
/**
* This autoloader is _append_, so a mix of legacy and Laminas classes can be used.
*/
public static function load()
{
$classes = RewriteRules::namespaceRewrite();
spl_autoload_register(static function ($class) use ($classes) {
$segments = explode('\\', $class);

$i = 0;
$check = '';

while (isset($classes[$check . $segments[$i] . '\\'])) {
$check .= $segments[$i] . '\\';
++$i;
}
michalbundyra marked this conversation as resolved.
Show resolved Hide resolved

if ($check !== '') {
michalbundyra marked this conversation as resolved.
Show resolved Hide resolved
$alias = $classes[$check] . substr($class, strlen($check));
class_alias($alias, $class);
return;
}
});
}
}
37 changes: 37 additions & 0 deletions src/RewriteRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* @see https://github.com/laminas/laminas-zendframework-bridge for the canonical source repository
* @copyright Copyright (c) 2019 Laminas Foundation (https://getlaminas.org)
weierophinney marked this conversation as resolved.
Show resolved Hide resolved
* @license https://github.com/laminas/laminas-zendframework-bridge/blob/master/LICENSE.md New BSD License
*/

namespace Laminas\ZendFrameworkBridge;

class RewriteRules
{
/**
* @return array
*/
public static function namespaceRewrite()
{
// @todo Need to determine the final name and namespace for zf-console
return array(
// Expressive
'Zend\\ProblemDetails\\' => 'Expressive\\',
'Zend\\Expressive\\' => 'Expressive\\',
// Laminas
'Zend\\' => 'Laminas\\',
'ZF\\ComposerAutoloading\\' => 'Laminas\\ComposerAutoloading\\',
'ZF\\Deploy\\' => 'Laminas\\Deploy\\',
'ZF\\DevelopmentMode\\' => 'Laminas\\DevelopmentMode\\',
// Apigility
'ZF\\Apigility\\' => 'Apigility\\',
'ZF\\' => 'Apigility\\',
// ZendXml, API wrappers, zend-http OAuth support, zend-diagnostics
'ZendXml\\' => 'Laminas\\Xml\\',
'ZendService\\' => 'Laminas\\',
'ZendOAuth\\' => 'Laminas\\OAuth\\',
'ZendDiagnostics\\' => 'Laminas\\Diagnostics\\',
);
}
}
30 changes: 0 additions & 30 deletions src/autoload-functions-containerconfigtest.php

This file was deleted.

76 changes: 0 additions & 76 deletions src/autoload-functions-diactoros.php

This file was deleted.

47 changes: 0 additions & 47 deletions src/autoload-functions-stratigility.php

This file was deleted.

56 changes: 1 addition & 55 deletions src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,4 @@
* @license https://github.com/laminas/laminas-zendframework-bridge/blob/master/LICENSE.md New BSD License
*/

if (PHP_VERSION_ID >= 71000) {
require __DIR__ . '/autoload-functions-containerconfigtest.php';
require __DIR__ . '/autoload-functions-diactoros.php';
require __DIR__ . '/autoload-functions-stratigility.php';
}

/**
* Alias legacy Zend Framework project classes/interfaces/traits to Laminas equivalents.
*
* This autoloader is _appended_, so a mix of legacy and Laminas classes can be used.
*/
spl_autoload_register(function ($class) {
// Zend and Expressive
if (strpos($class, 'Zend\\') === 0) {
$alias = strpos($class, 'Zend\\Expressive\\') === 0 || strpos($class, 'Zend\\ProblemDetails\\') === 0
? str_replace(array('Zend\\Expressive\\', 'Zend\\ProblemDetails\\'), 'Expressive\\', $class)
: str_replace('Zend\\', 'Laminas\\', $class);

class_alias($alias, $class);
return;
}

// Apigility
// @todo Need to determine the final name and namespace for zf-console
if (strpos($class, 'ZF\\') === 0) {
$alias = preg_match('#^ZF\\(ComposerAutoloading|Deploy|DevelopmentMode)\\#', $class)
? str_replace('ZF\\', 'Laminas\\', $class)
: str_replace('ZF\\', 'Apigility\\', $class);

// zf-apigility-doctrine
if (strpos($alias, 'Apigility\\Apigility\\') === 0) {
$alias = str_replace('Apigility\\Apigility\\', 'Apigility\\', $alias);
}

class_alias($alias, $class);
return;
}

// ZendXml, API wrappers, zend-http OAuth support, zend-diagnostics
if (strpos($class, 'ZendXml\\') === 0
|| strpos($class, 'ZendService\\') === 0
|| strpos($class, 'ZendOAuth\\') === 0
|| strpos($class, 'ZendDiagnostics\\') === 0
) {
// phpcs:disable
$alias = str_replace(
array( 'ZendXml\\', 'ZendService\\', 'ZendOAuth\\', 'ZendDiagnostics\\'),
array('Laminas\\Xml\\', 'Laminas\\', 'Laminas\\OAuth\\', 'Laminas\\Diagnostics\\'),
$class
);
// phpcs:enable
class_alias($alias, $class);
return;
}
});
\Laminas\ZendFrameworkBridge\Autoloader::load();