Initial commit

This commit is contained in:
Paul Nicoué 2021-10-29 18:05:46 +02:00
commit 1ff19bf38f
830 changed files with 159212 additions and 0 deletions

View file

@ -0,0 +1 @@
Copyright (c) 2020 Laminas Project a Series of LF Projects, LLC. (https://getlaminas.org/)

View file

@ -0,0 +1,26 @@
Copyright (c) 2020 Laminas Project a Series of LF Projects, LLC.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of Laminas Foundation nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View file

@ -0,0 +1,61 @@
{
"name": "laminas/laminas-escaper",
"description": "Securely and safely escape HTML, HTML attributes, JavaScript, CSS, and URLs",
"license": "BSD-3-Clause",
"keywords": [
"laminas",
"escaper"
],
"homepage": "https://laminas.dev",
"support": {
"docs": "https://docs.laminas.dev/laminas-escaper/",
"issues": "https://github.com/laminas/laminas-escaper/issues",
"source": "https://github.com/laminas/laminas-escaper",
"rss": "https://github.com/laminas/laminas-escaper/releases.atom",
"chat": "https://laminas.dev/chat",
"forum": "https://discourse.laminas.dev"
},
"config": {
"sort-packages": true
},
"extra": {
},
"require": {
"php": "^7.3 || ~8.0.0",
"laminas/laminas-zendframework-bridge": "^1.0"
},
"suggest": {
"ext-iconv": "*",
"ext-mbstring": "*"
},
"require-dev": {
"laminas/laminas-coding-standard": "~1.0.0",
"phpunit/phpunit": "^9.3",
"psalm/plugin-phpunit": "^0.12.2",
"vimeo/psalm": "^3.16"
},
"autoload": {
"psr-4": {
"Laminas\\Escaper\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"LaminasTest\\Escaper\\": "test/"
}
},
"scripts": {
"check": [
"@cs-check",
"@test"
],
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"static-analysis": "psalm --shepherd --stats",
"test": "phpunit --colors=always",
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml"
},
"replace": {
"zendframework/zend-escaper": "^2.6.1"
}
}

View file

@ -0,0 +1,391 @@
<?php
/**
* @see https://github.com/laminas/laminas-escaper for the canonical source repository
* @copyright https://github.com/laminas/laminas-escaper/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-escaper/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Escaper;
/**
* Context specific methods for use in secure output escaping
*/
class Escaper
{
/**
* Entity Map mapping Unicode codepoints to any available named HTML entities.
*
* While HTML supports far more named entities, the lowest common denominator
* has become HTML5's XML Serialisation which is restricted to the those named
* entities that XML supports. Using HTML entities would result in this error:
* XML Parsing Error: undefined entity
*
* @var array
*/
protected static $htmlNamedEntityMap = [
34 => 'quot', // quotation mark
38 => 'amp', // ampersand
60 => 'lt', // less-than sign
62 => 'gt', // greater-than sign
];
/**
* Current encoding for escaping. If not UTF-8, we convert strings from this encoding
* pre-escaping and back to this encoding post-escaping.
*
* @var string
*/
protected $encoding = 'utf-8';
/**
* Holds the value of the special flags passed as second parameter to
* htmlspecialchars().
*
* @var int
*/
protected $htmlSpecialCharsFlags;
/**
* Static Matcher which escapes characters for HTML Attribute contexts
*
* @var callable
*/
protected $htmlAttrMatcher;
/**
* Static Matcher which escapes characters for Javascript contexts
*
* @var callable
*/
protected $jsMatcher;
/**
* Static Matcher which escapes characters for CSS Attribute contexts
*
* @var callable
*/
protected $cssMatcher;
/**
* List of all encoding supported by this class
*
* @var array
*/
protected $supportedEncodings = [
'iso-8859-1', 'iso8859-1', 'iso-8859-5', 'iso8859-5',
'iso-8859-15', 'iso8859-15', 'utf-8', 'cp866',
'ibm866', '866', 'cp1251', 'windows-1251',
'win-1251', '1251', 'cp1252', 'windows-1252',
'1252', 'koi8-r', 'koi8-ru', 'koi8r',
'big5', '950', 'gb2312', '936',
'big5-hkscs', 'shift_jis', 'sjis', 'sjis-win',
'cp932', '932', 'euc-jp', 'eucjp',
'eucjp-win', 'macroman'
];
/**
* Constructor: Single parameter allows setting of global encoding for use by
* the current object.
*
* @param string $encoding
* @throws Exception\InvalidArgumentException
*/
public function __construct($encoding = null)
{
if ($encoding !== null) {
if (! is_string($encoding)) {
throw new Exception\InvalidArgumentException(
get_class($this) . ' constructor parameter must be a string, received ' . gettype($encoding)
);
}
if ($encoding === '') {
throw new Exception\InvalidArgumentException(
get_class($this) . ' constructor parameter does not allow a blank value'
);
}
$encoding = strtolower($encoding);
if (! in_array($encoding, $this->supportedEncodings)) {
throw new Exception\InvalidArgumentException(
'Value of \'' . $encoding . '\' passed to ' . get_class($this)
. ' constructor parameter is invalid. Provide an encoding supported by htmlspecialchars()'
);
}
$this->encoding = $encoding;
}
// We take advantage of ENT_SUBSTITUTE flag to correctly deal with invalid UTF-8 sequences.
$this->htmlSpecialCharsFlags = ENT_QUOTES | ENT_SUBSTITUTE;
// set matcher callbacks
$this->htmlAttrMatcher = [$this, 'htmlAttrMatcher'];
$this->jsMatcher = [$this, 'jsMatcher'];
$this->cssMatcher = [$this, 'cssMatcher'];
}
/**
* Return the encoding that all output/input is expected to be encoded in.
*
* @return string
*/
public function getEncoding()
{
return $this->encoding;
}
/**
* Escape a string for the HTML Body context where there are very few characters
* of special meaning. Internally this will use htmlspecialchars().
*
* @param string $string
* @return string
*/
public function escapeHtml($string)
{
return htmlspecialchars($string, $this->htmlSpecialCharsFlags, $this->encoding);
}
/**
* Escape a string for the HTML Attribute context. We use an extended set of characters
* to escape that are not covered by htmlspecialchars() to cover cases where an attribute
* might be unquoted or quoted illegally (e.g. backticks are valid quotes for IE).
*
* @param string $string
* @return string
*/
public function escapeHtmlAttr($string)
{
$string = $this->toUtf8($string);
if ($string === '' || ctype_digit($string)) {
return $string;
}
$result = preg_replace_callback('/[^a-z0-9,\.\-_]/iSu', $this->htmlAttrMatcher, $string);
return $this->fromUtf8($result);
}
/**
* Escape a string for the Javascript context. This does not use json_encode(). An extended
* set of characters are escaped beyond ECMAScript's rules for Javascript literal string
* escaping in order to prevent misinterpretation of Javascript as HTML leading to the
* injection of special characters and entities. The escaping used should be tolerant
* of cases where HTML escaping was not applied on top of Javascript escaping correctly.
* Backslash escaping is not used as it still leaves the escaped character as-is and so
* is not useful in a HTML context.
*
* @param string $string
* @return string
*/
public function escapeJs($string)
{
$string = $this->toUtf8($string);
if ($string === '' || ctype_digit($string)) {
return $string;
}
$result = preg_replace_callback('/[^a-z0-9,\._]/iSu', $this->jsMatcher, $string);
return $this->fromUtf8($result);
}
/**
* Escape a string for the URI or Parameter contexts. This should not be used to escape
* an entire URI - only a subcomponent being inserted. The function is a simple proxy
* to rawurlencode() which now implements RFC 3986 since PHP 5.3 completely.
*
* @param string $string
* @return string
*/
public function escapeUrl($string)
{
return rawurlencode($string);
}
/**
* Escape a string for the CSS context. CSS escaping can be applied to any string being
* inserted into CSS and escapes everything except alphanumerics.
*
* @param string $string
* @return string
*/
public function escapeCss($string)
{
$string = $this->toUtf8($string);
if ($string === '' || ctype_digit($string)) {
return $string;
}
$result = preg_replace_callback('/[^a-z0-9]/iSu', $this->cssMatcher, $string);
return $this->fromUtf8($result);
}
/**
* Callback function for preg_replace_callback that applies HTML Attribute
* escaping to all matches.
*
* @param array $matches
* @return string
*/
protected function htmlAttrMatcher($matches)
{
$chr = $matches[0];
$ord = ord($chr);
/**
* The following replaces characters undefined in HTML with the
* hex entity for the Unicode replacement character.
*/
if (($ord <= 0x1f && $chr != "\t" && $chr != "\n" && $chr != "\r")
|| ($ord >= 0x7f && $ord <= 0x9f)
) {
return '&#xFFFD;';
}
/**
* Check if the current character to escape has a name entity we should
* replace it with while grabbing the integer value of the character.
*/
if (strlen($chr) > 1) {
$chr = $this->convertEncoding($chr, 'UTF-32BE', 'UTF-8');
}
$hex = bin2hex($chr);
$ord = hexdec($hex);
if (isset(static::$htmlNamedEntityMap[$ord])) {
return '&' . static::$htmlNamedEntityMap[$ord] . ';';
}
/**
* Per OWASP recommendations, we'll use upper hex entities
* for any other characters where a named entity does not exist.
*/
if ($ord > 255) {
return sprintf('&#x%04X;', $ord);
}
return sprintf('&#x%02X;', $ord);
}
/**
* Callback function for preg_replace_callback that applies Javascript
* escaping to all matches.
*
* @param array $matches
* @return string
*/
protected function jsMatcher($matches)
{
$chr = $matches[0];
if (strlen($chr) == 1) {
return sprintf('\\x%02X', ord($chr));
}
$chr = $this->convertEncoding($chr, 'UTF-16BE', 'UTF-8');
$hex = strtoupper(bin2hex($chr));
if (strlen($hex) <= 4) {
return sprintf('\\u%04s', $hex);
}
$highSurrogate = substr($hex, 0, 4);
$lowSurrogate = substr($hex, 4, 4);
return sprintf('\\u%04s\\u%04s', $highSurrogate, $lowSurrogate);
}
/**
* Callback function for preg_replace_callback that applies CSS
* escaping to all matches.
*
* @param array $matches
* @return string
*/
protected function cssMatcher($matches)
{
$chr = $matches[0];
if (strlen($chr) == 1) {
$ord = ord($chr);
} else {
$chr = $this->convertEncoding($chr, 'UTF-32BE', 'UTF-8');
$ord = hexdec(bin2hex($chr));
}
return sprintf('\\%X ', $ord);
}
/**
* Converts a string to UTF-8 from the base encoding. The base encoding is set via this
* class' constructor.
*
* @param string $string
* @throws Exception\RuntimeException
* @return string
*/
protected function toUtf8($string)
{
if ($this->getEncoding() === 'utf-8') {
$result = $string;
} else {
$result = $this->convertEncoding($string, 'UTF-8', $this->getEncoding());
}
if (! $this->isUtf8($result)) {
throw new Exception\RuntimeException(
sprintf('String to be escaped was not valid UTF-8 or could not be converted: %s', $result)
);
}
return $result;
}
/**
* Converts a string from UTF-8 to the base encoding. The base encoding is set via this
* class' constructor.
* @param string $string
* @return string
*/
protected function fromUtf8($string)
{
if ($this->getEncoding() === 'utf-8') {
return $string;
}
return $this->convertEncoding($string, $this->getEncoding(), 'UTF-8');
}
/**
* Checks if a given string appears to be valid UTF-8 or not.
*
* @param string $string
* @return bool
*/
protected function isUtf8($string)
{
return ($string === '' || preg_match('/^./su', $string));
}
/**
* Encoding conversion helper which wraps iconv and mbstring where they exist or throws
* and exception where neither is available.
*
* @param string $string
* @param string $to
* @param array|string $from
* @throws Exception\RuntimeException
* @return string
*/
protected function convertEncoding($string, $to, $from)
{
if (function_exists('iconv')) {
$result = iconv($from, $to, $string);
} elseif (function_exists('mb_convert_encoding')) {
$result = mb_convert_encoding($string, $to, $from);
} else {
throw new Exception\RuntimeException(
get_class($this)
. ' requires either the iconv or mbstring extension to be installed'
. ' when escaping for non UTF-8 strings.'
);
}
if ($result === false) {
return ''; // return non-fatal blank string on encoding errors from users
}
return $result;
}
}

View file

@ -0,0 +1,13 @@
<?php
/**
* @see https://github.com/laminas/laminas-escaper for the canonical source repository
* @copyright https://github.com/laminas/laminas-escaper/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-escaper/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Escaper\Exception;
interface ExceptionInterface
{
}

View file

@ -0,0 +1,17 @@
<?php
/**
* @see https://github.com/laminas/laminas-escaper for the canonical source repository
* @copyright https://github.com/laminas/laminas-escaper/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-escaper/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Escaper\Exception;
/**
* Invalid argument exception
*/
class InvalidArgumentException extends \InvalidArgumentException implements
ExceptionInterface
{
}

View file

@ -0,0 +1,17 @@
<?php
/**
* @see https://github.com/laminas/laminas-escaper for the canonical source repository
* @copyright https://github.com/laminas/laminas-escaper/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-escaper/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Escaper\Exception;
/**
* Invalid argument exception
*/
class RuntimeException extends \RuntimeException implements
ExceptionInterface
{
}

View file

@ -0,0 +1 @@
Copyright (c) 2020 Laminas Project a Series of LF Projects, LLC. (https://getlaminas.org/)

View file

@ -0,0 +1,26 @@
Copyright (c) 2020 Laminas Project a Series of LF Projects, LLC.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of Laminas Foundation nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View file

@ -0,0 +1,61 @@
{
"name": "laminas/laminas-zendframework-bridge",
"description": "Alias legacy ZF class names to Laminas Project equivalents.",
"license": "BSD-3-Clause",
"keywords": [
"autoloading",
"laminas",
"zf",
"zendframework"
],
"support": {
"issues": "https://github.com/laminas/laminas-zendframework-bridge/issues",
"source": "https://github.com/laminas/laminas-zendframework-bridge",
"rss": "https://github.com/laminas/laminas-zendframework-bridge/releases.atom",
"forum": "https://discourse.laminas.dev/"
},
"require": {
"php": "^7.3 || ^8.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7 || ^6.5 || ^7.5 || ^8.1 || ^9.3",
"psalm/plugin-phpunit": "^0.15.1",
"squizlabs/php_codesniffer": "^3.5",
"vimeo/psalm": "^4.6"
},
"autoload": {
"files": [
"src/autoload.php"
],
"psr-4": {
"Laminas\\ZendFrameworkBridge\\": "src//"
}
},
"autoload-dev": {
"files": [
"test/classes.php"
],
"psr-4": {
"LaminasTest\\ZendFrameworkBridge\\": "test/",
"LaminasTest\\ZendFrameworkBridge\\TestAsset\\": "test/TestAsset/classes/",
"Laminas\\ApiTools\\": "test/TestAsset/LaminasApiTools/",
"Mezzio\\": "test/TestAsset/Mezzio/",
"Laminas\\": "test/TestAsset/Laminas/"
}
},
"extra": {
"laminas": {
"module": "Laminas\\ZendFrameworkBridge"
}
},
"config": {
"sort-packages": true
},
"scripts": {
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"static-analysis": "psalm --shepherd --stats",
"test": "phpunit --colors=always",
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml"
}
}

View file

@ -0,0 +1,372 @@
<?php
return [
// NEVER REWRITE
'zendframework/zendframework' => 'zendframework/zendframework',
'zend-developer-tools/toolbar/bjy' => 'zend-developer-tools/toolbar/bjy',
'zend-developer-tools/toolbar/doctrine' => 'zend-developer-tools/toolbar/doctrine',
// NAMESPACES
// Zend Framework components
'Zend\\AuraDi\\Config' => 'Laminas\\AuraDi\\Config',
'Zend\\Authentication' => 'Laminas\\Authentication',
'Zend\\Barcode' => 'Laminas\\Barcode',
'Zend\\Cache' => 'Laminas\\Cache',
'Zend\\Captcha' => 'Laminas\\Captcha',
'Zend\\Code' => 'Laminas\\Code',
'ZendCodingStandard\\Sniffs' => 'LaminasCodingStandard\\Sniffs',
'ZendCodingStandard\\Utils' => 'LaminasCodingStandard\\Utils',
'Zend\\ComponentInstaller' => 'Laminas\\ComponentInstaller',
'Zend\\Config' => 'Laminas\\Config',
'Zend\\ConfigAggregator' => 'Laminas\\ConfigAggregator',
'Zend\\ConfigAggregatorModuleManager' => 'Laminas\\ConfigAggregatorModuleManager',
'Zend\\ConfigAggregatorParameters' => 'Laminas\\ConfigAggregatorParameters',
'Zend\\Console' => 'Laminas\\Console',
'Zend\\ContainerConfigTest' => 'Laminas\\ContainerConfigTest',
'Zend\\Crypt' => 'Laminas\\Crypt',
'Zend\\Db' => 'Laminas\\Db',
'ZendDeveloperTools' => 'Laminas\\DeveloperTools',
'Zend\\Di' => 'Laminas\\Di',
'Zend\\Diactoros' => 'Laminas\\Diactoros',
'ZendDiagnostics\\Check' => 'Laminas\\Diagnostics\\Check',
'ZendDiagnostics\\Result' => 'Laminas\\Diagnostics\\Result',
'ZendDiagnostics\\Runner' => 'Laminas\\Diagnostics\\Runner',
'Zend\\Dom' => 'Laminas\\Dom',
'Zend\\Escaper' => 'Laminas\\Escaper',
'Zend\\EventManager' => 'Laminas\\EventManager',
'Zend\\Feed' => 'Laminas\\Feed',
'Zend\\File' => 'Laminas\\File',
'Zend\\Filter' => 'Laminas\\Filter',
'Zend\\Form' => 'Laminas\\Form',
'Zend\\Http' => 'Laminas\\Http',
'Zend\\HttpHandlerRunner' => 'Laminas\\HttpHandlerRunner',
'Zend\\Hydrator' => 'Laminas\\Hydrator',
'Zend\\I18n' => 'Laminas\\I18n',
'Zend\\InputFilter' => 'Laminas\\InputFilter',
'Zend\\Json' => 'Laminas\\Json',
'Zend\\Ldap' => 'Laminas\\Ldap',
'Zend\\Loader' => 'Laminas\\Loader',
'Zend\\Log' => 'Laminas\\Log',
'Zend\\Mail' => 'Laminas\\Mail',
'Zend\\Math' => 'Laminas\\Math',
'Zend\\Memory' => 'Laminas\\Memory',
'Zend\\Mime' => 'Laminas\\Mime',
'Zend\\ModuleManager' => 'Laminas\\ModuleManager',
'Zend\\Mvc' => 'Laminas\\Mvc',
'Zend\\Navigation' => 'Laminas\\Navigation',
'Zend\\Paginator' => 'Laminas\\Paginator',
'Zend\\Permissions' => 'Laminas\\Permissions',
'Zend\\Pimple\\Config' => 'Laminas\\Pimple\\Config',
'Zend\\ProblemDetails' => 'Mezzio\\ProblemDetails',
'Zend\\ProgressBar' => 'Laminas\\ProgressBar',
'Zend\\Psr7Bridge' => 'Laminas\\Psr7Bridge',
'Zend\\Router' => 'Laminas\\Router',
'Zend\\Serializer' => 'Laminas\\Serializer',
'Zend\\Server' => 'Laminas\\Server',
'Zend\\ServiceManager' => 'Laminas\\ServiceManager',
'ZendService\\ReCaptcha' => 'Laminas\\ReCaptcha',
'ZendService\\Twitter' => 'Laminas\\Twitter',
'Zend\\Session' => 'Laminas\\Session',
'Zend\\SkeletonInstaller' => 'Laminas\\SkeletonInstaller',
'Zend\\Soap' => 'Laminas\\Soap',
'Zend\\Stdlib' => 'Laminas\\Stdlib',
'Zend\\Stratigility' => 'Laminas\\Stratigility',
'Zend\\Tag' => 'Laminas\\Tag',
'Zend\\Test' => 'Laminas\\Test',
'Zend\\Text' => 'Laminas\\Text',
'Zend\\Uri' => 'Laminas\\Uri',
'Zend\\Validator' => 'Laminas\\Validator',
'Zend\\View' => 'Laminas\\View',
'ZendXml' => 'Laminas\\Xml',
'Zend\\Xml2Json' => 'Laminas\\Xml2Json',
'Zend\\XmlRpc' => 'Laminas\\XmlRpc',
'ZendOAuth' => 'Laminas\\OAuth',
// class ZendAcl in zend-expressive-authorization-acl
'ZendAcl' => 'LaminasAcl',
'Zend\\Expressive\\Authorization\\Acl\\ZendAcl' => 'Mezzio\\Authorization\\Acl\\LaminasAcl',
// class ZendHttpClientDecorator in zend-feed
'ZendHttp' => 'LaminasHttp',
// class ZendModuleProvider in zend-config-aggregator-modulemanager
'ZendModule' => 'LaminasModule',
// class ZendRbac in zend-expressive-authorization-rbac
'ZendRbac' => 'LaminasRbac',
'Zend\\Expressive\\Authorization\\Rbac\\ZendRbac' => 'Mezzio\\Authorization\\Rbac\\LaminasRbac',
// class ZendRouter in zend-expressive-router-zendrouter
'ZendRouter' => 'LaminasRouter',
'Zend\\Expressive\\Router\\ZendRouter' => 'Mezzio\\Router\\LaminasRouter',
// class ZendViewRenderer in zend-expressive-zendviewrenderer
'ZendViewRenderer' => 'LaminasViewRenderer',
'Zend\\Expressive\\ZendView\\ZendViewRenderer' => 'Mezzio\\LaminasView\\LaminasViewRenderer',
'a\\Zend' => 'a\\Zend',
'b\\Zend' => 'b\\Zend',
'c\\Zend' => 'c\\Zend',
'd\\Zend' => 'd\\Zend',
'e\\Zend' => 'e\\Zend',
'f\\Zend' => 'f\\Zend',
'g\\Zend' => 'g\\Zend',
'h\\Zend' => 'h\\Zend',
'i\\Zend' => 'i\\Zend',
'j\\Zend' => 'j\\Zend',
'k\\Zend' => 'k\\Zend',
'l\\Zend' => 'l\\Zend',
'm\\Zend' => 'm\\Zend',
'n\\Zend' => 'n\\Zend',
'o\\Zend' => 'o\\Zend',
'p\\Zend' => 'p\\Zend',
'q\\Zend' => 'q\\Zend',
'r\\Zend' => 'r\\Zend',
's\\Zend' => 's\\Zend',
't\\Zend' => 't\\Zend',
'u\\Zend' => 'u\\Zend',
'v\\Zend' => 'v\\Zend',
'w\\Zend' => 'w\\Zend',
'x\\Zend' => 'x\\Zend',
'y\\Zend' => 'y\\Zend',
'z\\Zend' => 'z\\Zend',
// Expressive
'Zend\\Expressive' => 'Mezzio',
'ZendAuthentication' => 'LaminasAuthentication',
'ZendAcl' => 'LaminasAcl',
'ZendRbac' => 'LaminasRbac',
'ZendRouter' => 'LaminasRouter',
'ExpressiveUrlGenerator' => 'MezzioUrlGenerator',
'ExpressiveInstaller' => 'MezzioInstaller',
// Apigility
'ZF\\Apigility' => 'Laminas\\ApiTools',
'ZF\\ApiProblem' => 'Laminas\\ApiTools\\ApiProblem',
'ZF\\AssetManager' => 'Laminas\\ApiTools\\AssetManager',
'ZF\\ComposerAutoloading' => 'Laminas\\ComposerAutoloading',
'ZF\\Configuration' => 'Laminas\\ApiTools\\Configuration',
'ZF\\ContentNegotiation' => 'Laminas\\ApiTools\\ContentNegotiation',
'ZF\\ContentValidation' => 'Laminas\\ApiTools\\ContentValidation',
'ZF\\DevelopmentMode' => 'Laminas\\DevelopmentMode',
'ZF\\Doctrine\\QueryBuilder' => 'Laminas\\ApiTools\\Doctrine\\QueryBuilder',
'ZF\\Hal' => 'Laminas\\ApiTools\\Hal',
'ZF\\HttpCache' => 'Laminas\\ApiTools\\HttpCache',
'ZF\\MvcAuth' => 'Laminas\\ApiTools\\MvcAuth',
'ZF\\OAuth2' => 'Laminas\\ApiTools\\OAuth2',
'ZF\\Rest' => 'Laminas\\ApiTools\\Rest',
'ZF\\Rpc' => 'Laminas\\ApiTools\\Rpc',
'ZF\\Versioning' => 'Laminas\\ApiTools\\Versioning',
'a\\ZF' => 'a\\ZF',
'b\\ZF' => 'b\\ZF',
'c\\ZF' => 'c\\ZF',
'd\\ZF' => 'd\\ZF',
'e\\ZF' => 'e\\ZF',
'f\\ZF' => 'f\\ZF',
'g\\ZF' => 'g\\ZF',
'h\\ZF' => 'h\\ZF',
'i\\ZF' => 'i\\ZF',
'j\\ZF' => 'j\\ZF',
'k\\ZF' => 'k\\ZF',
'l\\ZF' => 'l\\ZF',
'm\\ZF' => 'm\\ZF',
'n\\ZF' => 'n\\ZF',
'o\\ZF' => 'o\\ZF',
'p\\ZF' => 'p\\ZF',
'q\\ZF' => 'q\\ZF',
'r\\ZF' => 'r\\ZF',
's\\ZF' => 's\\ZF',
't\\ZF' => 't\\ZF',
'u\\ZF' => 'u\\ZF',
'v\\ZF' => 'v\\ZF',
'w\\ZF' => 'w\\ZF',
'x\\ZF' => 'x\\ZF',
'y\\ZF' => 'y\\ZF',
'z\\ZF' => 'z\\ZF',
'ApigilityModuleInterface' => 'ApiToolsModuleInterface',
'ApigilityProviderInterface' => 'ApiToolsProviderInterface',
'ApigilityVersionController' => 'ApiToolsVersionController',
// PACKAGES
// ZF components, MVC
'zendframework/skeleton-application' => 'laminas/skeleton-application',
'zendframework/zend-auradi-config' => 'laminas/laminas-auradi-config',
'zendframework/zend-authentication' => 'laminas/laminas-authentication',
'zendframework/zend-barcode' => 'laminas/laminas-barcode',
'zendframework/zend-cache' => 'laminas/laminas-cache',
'zendframework/zend-captcha' => 'laminas/laminas-captcha',
'zendframework/zend-code' => 'laminas/laminas-code',
'zendframework/zend-coding-standard' => 'laminas/laminas-coding-standard',
'zendframework/zend-component-installer' => 'laminas/laminas-component-installer',
'zendframework/zend-composer-autoloading' => 'laminas/laminas-composer-autoloading',
'zendframework/zend-config-aggregator' => 'laminas/laminas-config-aggregator',
'zendframework/zend-config' => 'laminas/laminas-config',
'zendframework/zend-console' => 'laminas/laminas-console',
'zendframework/zend-container-config-test' => 'laminas/laminas-container-config-test',
'zendframework/zend-crypt' => 'laminas/laminas-crypt',
'zendframework/zend-db' => 'laminas/laminas-db',
'zendframework/zend-developer-tools' => 'laminas/laminas-developer-tools',
'zendframework/zend-diactoros' => 'laminas/laminas-diactoros',
'zendframework/zenddiagnostics' => 'laminas/laminas-diagnostics',
'zendframework/zend-di' => 'laminas/laminas-di',
'zendframework/zend-dom' => 'laminas/laminas-dom',
'zendframework/zend-escaper' => 'laminas/laminas-escaper',
'zendframework/zend-eventmanager' => 'laminas/laminas-eventmanager',
'zendframework/zend-feed' => 'laminas/laminas-feed',
'zendframework/zend-file' => 'laminas/laminas-file',
'zendframework/zend-filter' => 'laminas/laminas-filter',
'zendframework/zend-form' => 'laminas/laminas-form',
'zendframework/zend-httphandlerrunner' => 'laminas/laminas-httphandlerrunner',
'zendframework/zend-http' => 'laminas/laminas-http',
'zendframework/zend-hydrator' => 'laminas/laminas-hydrator',
'zendframework/zend-i18n' => 'laminas/laminas-i18n',
'zendframework/zend-i18n-resources' => 'laminas/laminas-i18n-resources',
'zendframework/zend-inputfilter' => 'laminas/laminas-inputfilter',
'zendframework/zend-json' => 'laminas/laminas-json',
'zendframework/zend-json-server' => 'laminas/laminas-json-server',
'zendframework/zend-ldap' => 'laminas/laminas-ldap',
'zendframework/zend-loader' => 'laminas/laminas-loader',
'zendframework/zend-log' => 'laminas/laminas-log',
'zendframework/zend-mail' => 'laminas/laminas-mail',
'zendframework/zend-math' => 'laminas/laminas-math',
'zendframework/zend-memory' => 'laminas/laminas-memory',
'zendframework/zend-mime' => 'laminas/laminas-mime',
'zendframework/zend-modulemanager' => 'laminas/laminas-modulemanager',
'zendframework/zend-mvc' => 'laminas/laminas-mvc',
'zendframework/zend-navigation' => 'laminas/laminas-navigation',
'zendframework/zend-oauth' => 'laminas/laminas-oauth',
'zendframework/zend-paginator' => 'laminas/laminas-paginator',
'zendframework/zend-permissions-acl' => 'laminas/laminas-permissions-acl',
'zendframework/zend-permissions-rbac' => 'laminas/laminas-permissions-rbac',
'zendframework/zend-pimple-config' => 'laminas/laminas-pimple-config',
'zendframework/zend-progressbar' => 'laminas/laminas-progressbar',
'zendframework/zend-psr7bridge' => 'laminas/laminas-psr7bridge',
'zendframework/zend-recaptcha' => 'laminas/laminas-recaptcha',
'zendframework/zend-router' => 'laminas/laminas-router',
'zendframework/zend-serializer' => 'laminas/laminas-serializer',
'zendframework/zend-server' => 'laminas/laminas-server',
'zendframework/zend-servicemanager' => 'laminas/laminas-servicemanager',
'zendframework/zendservice-recaptcha' => 'laminas/laminas-recaptcha',
'zendframework/zendservice-twitter' => 'laminas/laminas-twitter',
'zendframework/zend-session' => 'laminas/laminas-session',
'zendframework/zend-skeleton-installer' => 'laminas/laminas-skeleton-installer',
'zendframework/zend-soap' => 'laminas/laminas-soap',
'zendframework/zend-stdlib' => 'laminas/laminas-stdlib',
'zendframework/zend-stratigility' => 'laminas/laminas-stratigility',
'zendframework/zend-tag' => 'laminas/laminas-tag',
'zendframework/zend-test' => 'laminas/laminas-test',
'zendframework/zend-text' => 'laminas/laminas-text',
'zendframework/zend-uri' => 'laminas/laminas-uri',
'zendframework/zend-validator' => 'laminas/laminas-validator',
'zendframework/zend-view' => 'laminas/laminas-view',
'zendframework/zend-xml2json' => 'laminas/laminas-xml2json',
'zendframework/zend-xml' => 'laminas/laminas-xml',
'zendframework/zend-xmlrpc' => 'laminas/laminas-xmlrpc',
// Expressive packages
'zendframework/zend-expressive' => 'mezzio/mezzio',
'zendframework/zend-expressive-zendrouter' => 'mezzio/mezzio-laminasrouter',
'zendframework/zend-problem-details' => 'mezzio/mezzio-problem-details',
'zendframework/zend-expressive-zendviewrenderer' => 'mezzio/mezzio-laminasviewrenderer',
// Apigility packages
'zfcampus/apigility-documentation' => 'laminas-api-tools/documentation',
'zfcampus/statuslib-example' => 'laminas-api-tools/statuslib-example',
'zfcampus/zf-apigility' => 'laminas-api-tools/api-tools',
'zfcampus/zf-api-problem' => 'laminas-api-tools/api-tools-api-problem',
'zfcampus/zf-asset-manager' => 'laminas-api-tools/api-tools-asset-manager',
'zfcampus/zf-configuration' => 'laminas-api-tools/api-tools-configuration',
'zfcampus/zf-content-negotiation' => 'laminas-api-tools/api-tools-content-negotiation',
'zfcampus/zf-content-validation' => 'laminas-api-tools/api-tools-content-validation',
'zfcampus/zf-development-mode' => 'laminas/laminas-development-mode',
'zfcampus/zf-doctrine-querybuilder' => 'laminas-api-tools/api-tools-doctrine-querybuilder',
'zfcampus/zf-hal' => 'laminas-api-tools/api-tools-hal',
'zfcampus/zf-http-cache' => 'laminas-api-tools/api-tools-http-cache',
'zfcampus/zf-mvc-auth' => 'laminas-api-tools/api-tools-mvc-auth',
'zfcampus/zf-oauth2' => 'laminas-api-tools/api-tools-oauth2',
'zfcampus/zf-rest' => 'laminas-api-tools/api-tools-rest',
'zfcampus/zf-rpc' => 'laminas-api-tools/api-tools-rpc',
'zfcampus/zf-versioning' => 'laminas-api-tools/api-tools-versioning',
// CONFIG KEYS, SCRIPT NAMES, ETC
// ZF components
'::fromZend' => '::fromLaminas', // psr7bridge
'::toZend' => '::toLaminas', // psr7bridge
'use_zend_loader' => 'use_laminas_loader', // zend-modulemanager
'zend-config' => 'laminas-config',
'zend-developer-tools/' => 'laminas-developer-tools/',
'zend-tag-cloud' => 'laminas-tag-cloud',
'zenddevelopertools' => 'laminas-developer-tools',
'zendbarcode' => 'laminasbarcode',
'ZendBarcode' => 'LaminasBarcode',
'zendcache' => 'laminascache',
'ZendCache' => 'LaminasCache',
'zendconfig' => 'laminasconfig',
'ZendConfig' => 'LaminasConfig',
'zendfeed' => 'laminasfeed',
'ZendFeed' => 'LaminasFeed',
'zendfilter' => 'laminasfilter',
'ZendFilter' => 'LaminasFilter',
'zendform' => 'laminasform',
'ZendForm' => 'LaminasForm',
'zendi18n' => 'laminasi18n',
'ZendI18n' => 'LaminasI18n',
'zendinputfilter' => 'laminasinputfilter',
'ZendInputFilter' => 'LaminasInputFilter',
'zendlog' => 'laminaslog',
'ZendLog' => 'LaminasLog',
'zendmail' => 'laminasmail',
'ZendMail' => 'LaminasMail',
'zendmvc' => 'laminasmvc',
'ZendMvc' => 'LaminasMvc',
'zendpaginator' => 'laminaspaginator',
'ZendPaginator' => 'LaminasPaginator',
'zendserializer' => 'laminasserializer',
'ZendSerializer' => 'LaminasSerializer',
'zendtag' => 'laminastag',
'ZendTag' => 'LaminasTag',
'zendtext' => 'laminastext',
'ZendText' => 'LaminasText',
'zendvalidator' => 'laminasvalidator',
'ZendValidator' => 'LaminasValidator',
'zendview' => 'laminasview',
'ZendView' => 'LaminasView',
'zend-framework.flf' => 'laminas-project.flf',
// Expressive-related
"'zend-expressive'" => "'mezzio'",
'"zend-expressive"' => '"mezzio"',
'zend-expressive.' => 'mezzio.',
'zend-expressive-authorization' => 'mezzio-authorization',
'zend-expressive-hal' => 'mezzio-hal',
'zend-expressive-session' => 'mezzio-session',
'zend-expressive-swoole' => 'mezzio-swoole',
'zend-expressive-tooling' => 'mezzio-tooling',
// Apigility-related
"'zf-apigility'" => "'api-tools'",
'"zf-apigility"' => '"api-tools"',
'zf-apigility/' => 'api-tools/',
'zf-apigility-admin' => 'api-tools-admin',
'zf-content-negotiation' => 'api-tools-content-negotiation',
'zf-hal' => 'api-tools-hal',
'zf-rest' => 'api-tools-rest',
'zf-rpc' => 'api-tools-rpc',
'zf-content-validation' => 'api-tools-content-validation',
'zf-apigility-ui' => 'api-tools-ui',
'zf-apigility-documentation-blueprint' => 'api-tools-documentation-blueprint',
'zf-apigility-documentation-swagger' => 'api-tools-documentation-swagger',
'zf-apigility-welcome' => 'api-tools-welcome',
'zf-api-problem' => 'api-tools-api-problem',
'zf-configuration' => 'api-tools-configuration',
'zf-http-cache' => 'api-tools-http-cache',
'zf-mvc-auth' => 'api-tools-mvc-auth',
'zf-oauth2' => 'api-tools-oauth2',
'zf-versioning' => 'api-tools-versioning',
'ZfApigilityDoctrineQueryProviderManager' => 'LaminasApiToolsDoctrineQueryProviderManager',
'ZfApigilityDoctrineQueryCreateFilterManager' => 'LaminasApiToolsDoctrineQueryCreateFilterManager',
'zf-apigility-doctrine' => 'api-tools-doctrine',
'zf-development-mode' => 'laminas-development-mode',
'zf-doctrine-querybuilder' => 'api-tools-doctrine-querybuilder',
// 3rd party Apigility packages
'api-skeletons/zf-' => 'api-skeletons/zf-', // api-skeletons packages
'zf-oauth2-' => 'zf-oauth2-', // api-skeletons OAuth2-related packages
'ZF\\OAuth2\\Client' => 'ZF\\OAuth2\\Client', // api-skeletons/zf-oauth2-client
'ZF\\OAuth2\\Doctrine' => 'ZF\\OAuth2\\Doctrine', // api-skeletons/zf-oauth2-doctrine
];

View file

@ -0,0 +1,166 @@
<?php
namespace Laminas\ZendFrameworkBridge;
use ArrayObject;
use Composer\Autoload\ClassLoader;
use RuntimeException;
use function array_values;
use function class_alias;
use function class_exists;
use function explode;
use function file_exists;
use function interface_exists;
use function spl_autoload_register;
use function strlen;
use function strtr;
use function substr;
use function trait_exists;
/**
* Alias legacy Zend Framework project classes/interfaces/traits to Laminas equivalents.
*/
class Autoloader
{
/**
* Attach autoloaders for managing legacy ZF artifacts.
*
* We attach two autoloaders:
*
* - The first is _prepended_ to handle new classes and add aliases for
* legacy classes. PHP expects any interfaces implemented, classes
* extended, or traits used when declaring class_alias() to exist and/or
* be autoloadable already at the time of declaration. If not, it will
* raise a fatal error. This autoloader helps mitigate errors in such
* situations.
*
* - The second is _appended_ in order to create aliases for legacy
* classes.
*/
public static function load()
{
$loaded = new ArrayObject([]);
spl_autoload_register(self::createPrependAutoloader(
RewriteRules::namespaceReverse(),
self::getClassLoader(),
$loaded
), true, true);
spl_autoload_register(self::createAppendAutoloader(
RewriteRules::namespaceRewrite(),
$loaded
));
}
/**
* @return ClassLoader
* @throws RuntimeException
*/
private static function getClassLoader()
{
if (getenv('COMPOSER_VENDOR_DIR') && file_exists(getenv('COMPOSER_VENDOR_DIR') . '/autoload.php')) {
return include getenv('COMPOSER_VENDOR_DIR') . '/autoload.php';
}
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');
}
/**
* @return callable
*/
private static function createPrependAutoloader(array $namespaces, ClassLoader $classLoader, ArrayObject $loaded)
{
/**
* @param string $class Class name to autoload
* @return void
*/
return static 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]
. strtr(substr($class, strlen($check)), [
'ApiTools' => 'Apigility',
'Mezzio' => 'Expressive',
'Laminas' => 'Zend',
]);
class_alias($class, $legacy);
}
};
}
/**
* @return callable
*/
private static function createAppendAutoloader(array $namespaces, ArrayObject $loaded)
{
/**
* @param string $class Class name to autoload
* @return void
*/
return static function ($class) use ($namespaces, $loaded) {
$segments = explode('\\', $class);
if ($segments[0] === 'ZendService' && isset($segments[1])) {
$segments[0] .= '\\' . $segments[1];
unset($segments[1]);
$segments = array_values($segments);
}
$i = 0;
$check = '';
// We are checking segments of the namespace to match quicker
while (isset($segments[$i + 1], $namespaces[$check . $segments[$i] . '\\'])) {
$check .= $segments[$i] . '\\';
++$i;
}
if ($check === '') {
return;
}
$alias = $namespaces[$check]
. strtr(substr($class, strlen($check)), [
'Apigility' => 'ApiTools',
'Expressive' => 'Mezzio',
'Zend' => 'Laminas',
'AbstractZendServer' => 'AbstractZendServer',
'ZendServerDisk' => 'ZendServerDisk',
'ZendServerShm' => 'ZendServerShm',
'ZendMonitor' => 'ZendMonitor',
]);
$loaded[$alias] = true;
if (class_exists($alias) || interface_exists($alias) || trait_exists($alias)) {
class_alias($alias, $class);
}
};
}
}

View file

@ -0,0 +1,426 @@
<?php
namespace Laminas\ZendFrameworkBridge;
use function array_intersect_key;
use function array_key_exists;
use function array_pop;
use function in_array;
use function is_array;
use function is_callable;
use function is_int;
use function is_string;
class ConfigPostProcessor
{
/** @internal */
const SERVICE_MANAGER_KEYS_OF_INTEREST = [
'aliases' => true,
'factories' => true,
'invokables' => true,
'services' => true,
];
/** @var array String keys => string values */
private $exactReplacements = [
'zend-expressive' => 'mezzio',
'zf-apigility' => 'api-tools',
];
/** @var Replacements */
private $replacements;
/** @var callable[] */
private $rulesets;
public function __construct()
{
$this->replacements = new Replacements();
/* Define the rulesets for replacements.
*
* Each ruleset has the following signature:
*
* @param mixed $value
* @param string[] $keys Full nested key hierarchy leading to the value
* @return null|callable
*
* If no match is made, a null is returned, allowing it to fallback to
* the next ruleset in the list. If a match is made, a callback is returned,
* and that will be used to perform the replacement on the value.
*
* The callback should have the following signature:
*
* @param mixed $value
* @param string[] $keys
* @return mixed The transformed value
*/
$this->rulesets = [
// Exact values
function ($value) {
return is_string($value) && isset($this->exactReplacements[$value])
? [$this, 'replaceExactValue']
: null;
},
// Router (MVC applications)
// We do not want to rewrite these.
function ($value, array $keys) {
$key = array_pop($keys);
// Only worried about a top-level "router" key.
return $key === 'router' && $keys === [] && is_array($value)
? [$this, 'noopReplacement']
: null;
},
// service- and pluginmanager handling
function ($value) {
return is_array($value) && array_intersect_key(self::SERVICE_MANAGER_KEYS_OF_INTEREST, $value) !== []
? [$this, 'replaceDependencyConfiguration']
: null;
},
// Array values
function ($value, array $keys) {
return $keys !== [] && is_array($value)
? [$this, '__invoke']
: null;
},
];
}
/**
* @param string[] $keys Hierarchy of keys, for determining location in
* nested configuration.
* @return array
*/
public function __invoke(array $config, array $keys = [])
{
$rewritten = [];
foreach ($config as $key => $value) {
// Determine new key from replacements
$newKey = is_string($key) ? $this->replace($key, $keys) : $key;
// Keep original values with original key, if the key has changed, but only at the top-level.
if (empty($keys) && $newKey !== $key) {
$rewritten[$key] = $value;
}
// Perform value replacements, if any
$newValue = $this->replace($value, $keys, $newKey);
// Key does not already exist and/or is not an array value
if (! array_key_exists($newKey, $rewritten) || ! is_array($rewritten[$newKey])) {
// Do not overwrite existing values with null values
$rewritten[$newKey] = array_key_exists($newKey, $rewritten) && null === $newValue
? $rewritten[$newKey]
: $newValue;
continue;
}
// New value is null; nothing to do.
if (null === $newValue) {
continue;
}
// Key already exists as an array value, but $value is not an array
if (! is_array($newValue)) {
$rewritten[$newKey][] = $newValue;
continue;
}
// Key already exists as an array value, and $value is also an array
$rewritten[$newKey] = static::merge($rewritten[$newKey], $newValue);
}
return $rewritten;
}
/**
* Perform substitutions as needed on an individual value.
*
* The $key is provided to allow fine-grained selection of rewrite rules.
*
* @param mixed $value
* @param string[] $keys Key hierarchy
* @param null|int|string $key
* @return mixed
*/
private function replace($value, array $keys, $key = null)
{
// Add new key to the list of keys.
// We do not need to remove it later, as we are working on a copy of the array.
$keys[] = $key;
// Identify rewrite strategy and perform replacements
$rewriteRule = $this->replacementRuleMatch($value, $keys);
return $rewriteRule($value, $keys);
}
/**
* Merge two arrays together.
*
* If an integer key exists in both arrays, the value from the second array
* will be appended to the first array. If both values are arrays, they are
* merged together, else the value of the second array overwrites the one
* of the first array.
*
* Based on zend-stdlib Zend\Stdlib\ArrayUtils::merge
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
*
* @return array
*/
public static function merge(array $a, array $b)
{
foreach ($b as $key => $value) {
if (! isset($a[$key]) && ! array_key_exists($key, $a)) {
$a[$key] = $value;
continue;
}
if (null === $value && array_key_exists($key, $a)) {
// Leave as-is if value from $b is null
continue;
}
if (is_int($key)) {
$a[] = $value;
continue;
}
if (is_array($value) && is_array($a[$key])) {
$a[$key] = static::merge($a[$key], $value);
continue;
}
$a[$key] = $value;
}
return $a;
}
/**
* @param mixed $value
* @param null|int|string $key
* @return callable Callable to invoke with value
*/
private function replacementRuleMatch($value, $key = null)
{
foreach ($this->rulesets as $ruleset) {
$result = $ruleset($value, $key);
if (is_callable($result)) {
return $result;
}
}
return [$this, 'fallbackReplacement'];
}
/**
* Replace a value using the translation table, if the value is a string.
*
* @param mixed $value
* @return mixed
*/
private function fallbackReplacement($value)
{
return is_string($value)
? $this->replacements->replace($value)
: $value;
}
/**
* Replace a value matched exactly.
*
* @param mixed $value
* @return mixed
*/
private function replaceExactValue($value)
{
return $this->exactReplacements[$value];
}
private function replaceDependencyConfiguration(array $config)
{
$aliases = isset($config['aliases']) && is_array($config['aliases'])
? $this->replaceDependencyAliases($config['aliases'])
: [];
if ($aliases) {
$config['aliases'] = $aliases;
}
$config = $this->replaceDependencyInvokables($config);
$config = $this->replaceDependencyFactories($config);
$config = $this->replaceDependencyServices($config);
$keys = self::SERVICE_MANAGER_KEYS_OF_INTEREST;
foreach ($config as $key => $data) {
if (isset($keys[$key])) {
continue;
}
$config[$key] = is_array($data) ? $this->__invoke($data, [$key]) : $data;
}
return $config;
}
/**
* Rewrite dependency aliases array
*
* In this case, we want to keep the alias as-is, but rewrite the target.
*
* We need also provide an additional alias if the alias key is a legacy class.
*
* @return array
*/
private function replaceDependencyAliases(array $aliases)
{
foreach ($aliases as $alias => $target) {
if (! is_string($alias) || ! is_string($target)) {
continue;
}
$newTarget = $this->replacements->replace($target);
$newAlias = $this->replacements->replace($alias);
$notIn = [$newTarget];
$name = $newTarget;
while (isset($aliases[$name])) {
$notIn[] = $aliases[$name];
$name = $aliases[$name];
}
if ($newAlias === $alias && ! in_array($alias, $notIn, true)) {
$aliases[$alias] = $newTarget;
continue;
}
if (isset($aliases[$newAlias])) {
continue;
}
if (! in_array($newAlias, $notIn, true)) {
$aliases[$alias] = $newAlias;
$aliases[$newAlias] = $newTarget;
}
}
return $aliases;
}
/**
* Rewrite dependency invokables array
*
* In this case, we want to keep the alias as-is, but rewrite the target.
*
* We need also provide an additional alias if invokable is defined with
* an alias which is a legacy class.
*
* @return array
*/
private function replaceDependencyInvokables(array $config)
{
if (empty($config['invokables']) || ! is_array($config['invokables'])) {
return $config;
}
foreach ($config['invokables'] as $alias => $target) {
if (! is_string($alias)) {
continue;
}
$newTarget = $this->replacements->replace($target);
$newAlias = $this->replacements->replace($alias);
if ($alias === $target || isset($config['aliases'][$newAlias])) {
$config['invokables'][$alias] = $newTarget;
continue;
}
$config['invokables'][$newAlias] = $newTarget;
if ($newAlias === $alias) {
continue;
}
$config['aliases'][$alias] = $newAlias;
unset($config['invokables'][$alias]);
}
return $config;
}
/**
* @param mixed $value
* @return mixed Returns $value verbatim.
*/
private function noopReplacement($value)
{
return $value;
}
private function replaceDependencyFactories(array $config)
{
if (empty($config['factories']) || ! is_array($config['factories'])) {
return $config;
}
foreach ($config['factories'] as $service => $factory) {
if (! is_string($service)) {
continue;
}
$replacedService = $this->replacements->replace($service);
$factory = is_string($factory) ? $this->replacements->replace($factory) : $factory;
$config['factories'][$replacedService] = $factory;
if ($replacedService === $service) {
continue;
}
unset($config['factories'][$service]);
if (isset($config['aliases'][$service])) {
continue;
}
$config['aliases'][$service] = $replacedService;
}
return $config;
}
private function replaceDependencyServices(array $config)
{
if (empty($config['services']) || ! is_array($config['services'])) {
return $config;
}
foreach ($config['services'] as $service => $serviceInstance) {
if (! is_string($service)) {
continue;
}
$replacedService = $this->replacements->replace($service);
$serviceInstance = is_array($serviceInstance) ? $this->__invoke($serviceInstance) : $serviceInstance;
$config['services'][$replacedService] = $serviceInstance;
if ($service === $replacedService) {
continue;
}
unset($config['services'][$service]);
if (isset($config['aliases'][$service])) {
continue;
}
$config['aliases'][$service] = $replacedService;
}
return $config;
}
}

View file

@ -0,0 +1,48 @@
<?php
namespace Laminas\ZendFrameworkBridge;
use Laminas\ModuleManager\Listener\ConfigMergerInterface;
use Laminas\ModuleManager\ModuleEvent;
use Laminas\ModuleManager\ModuleManager;
class Module
{
/**
* Initialize the module.
*
* Type-hinting deliberately omitted to allow unit testing
* without dependencies on packages that do not exist yet.
*
* @param ModuleManager $moduleManager
*/
public function init($moduleManager)
{
$moduleManager
->getEventManager()
->attach('mergeConfig', [$this, 'onMergeConfig']);
}
/**
* Perform substitutions in the merged configuration.
*
* Rewrites keys and values matching known ZF classes, namespaces, and
* configuration keys to their Laminas equivalents.
*
* Type-hinting deliberately omitted to allow unit testing
* without dependencies on packages that do not exist yet.
*
* @param ModuleEvent $event
*/
public function onMergeConfig($event)
{
/** @var ConfigMergerInterface */
$configMerger = $event->getConfigListener();
$processor = new ConfigPostProcessor();
$configMerger->setMergedConfig(
$processor(
$configMerger->getMergedConfig($returnAsObject = false)
)
);
}
}

View file

@ -0,0 +1,40 @@
<?php
namespace Laminas\ZendFrameworkBridge;
use function array_merge;
use function str_replace;
use function strpos;
use function strtr;
class Replacements
{
/** @var string[] */
private $replacements;
public function __construct(array $additionalReplacements = [])
{
$this->replacements = array_merge(
require __DIR__ . '/../config/replacements.php',
$additionalReplacements
);
// Provide multiple variants of strings containing namespace separators
foreach ($this->replacements as $original => $replacement) {
if (false === strpos($original, '\\')) {
continue;
}
$this->replacements[str_replace('\\', '\\\\', $original)] = str_replace('\\', '\\\\', $replacement);
$this->replacements[str_replace('\\', '\\\\\\\\', $original)] = str_replace('\\', '\\\\\\\\', $replacement);
}
}
/**
* @param string $value
* @return string
*/
public function replace($value)
{
return strtr($value, $this->replacements);
}
}

View file

@ -0,0 +1,73 @@
<?php
namespace Laminas\ZendFrameworkBridge;
class RewriteRules
{
/**
* @return array
*/
public static function namespaceRewrite()
{
return [
// Expressive
'Zend\\ProblemDetails\\' => 'Mezzio\\ProblemDetails\\',
'Zend\\Expressive\\' => 'Mezzio\\',
// Laminas
'Zend\\' => 'Laminas\\',
'ZF\\ComposerAutoloading\\' => 'Laminas\\ComposerAutoloading\\',
'ZF\\DevelopmentMode\\' => 'Laminas\\DevelopmentMode\\',
// Apigility
'ZF\\Apigility\\' => 'Laminas\\ApiTools\\',
'ZF\\' => 'Laminas\\ApiTools\\',
// ZendXml, API wrappers, zend-http OAuth support, zend-diagnostics, ZendDeveloperTools
'ZendXml\\' => 'Laminas\\Xml\\',
'ZendOAuth\\' => 'Laminas\\OAuth\\',
'ZendDiagnostics\\' => 'Laminas\\Diagnostics\\',
'ZendService\\ReCaptcha\\' => 'Laminas\\ReCaptcha\\',
'ZendService\\Twitter\\' => 'Laminas\\Twitter\\',
'ZendDeveloperTools\\' => 'Laminas\\DeveloperTools\\',
];
}
/**
* @return array
*/
public static function namespaceReverse()
{
return [
// ZendXml, ZendOAuth, ZendDiagnostics, ZendDeveloperTools
'Laminas\\Xml\\' => 'ZendXml\\',
'Laminas\\OAuth\\' => 'ZendOAuth\\',
'Laminas\\Diagnostics\\' => 'ZendDiagnostics\\',
'Laminas\\DeveloperTools\\' => 'ZendDeveloperTools\\',
// Zend Service
'Laminas\\ReCaptcha\\' => 'ZendService\\ReCaptcha\\',
'Laminas\\Twitter\\' => 'ZendService\\Twitter\\',
// Zend
'Laminas\\' => 'Zend\\',
// Expressive
'Mezzio\\ProblemDetails\\' => 'Zend\\ProblemDetails\\',
'Mezzio\\' => 'Zend\\Expressive\\',
// Laminas to ZfCampus
'Laminas\\ComposerAutoloading\\' => 'ZF\\ComposerAutoloading\\',
'Laminas\\DevelopmentMode\\' => 'ZF\\DevelopmentMode\\',
// Apigility
'Laminas\\ApiTools\\Admin\\' => 'ZF\\Apigility\\Admin\\',
'Laminas\\ApiTools\\Doctrine\\' => 'ZF\\Apigility\\Doctrine\\',
'Laminas\\ApiTools\\Documentation\\' => 'ZF\\Apigility\\Documentation\\',
'Laminas\\ApiTools\\Example\\' => 'ZF\\Apigility\\Example\\',
'Laminas\\ApiTools\\Provider\\' => 'ZF\\Apigility\\Provider\\',
'Laminas\\ApiTools\\Welcome\\' => 'ZF\\Apiglity\\Welcome\\',
'Laminas\\ApiTools\\' => 'ZF\\',
];
}
}

View file

@ -0,0 +1,3 @@
<?php
Laminas\ZendFrameworkBridge\Autoloader::load();