diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index 05822e38c..b791b3496 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -13,6 +13,7 @@ bin/ + include/ src/ tests/ diff --git a/composer.json b/composer.json index ac0da0f6b..91747e556 100644 --- a/composer.json +++ b/composer.json @@ -49,7 +49,10 @@ "autoload": { "psr-4": { "AmpProject\\": "src/" - } + }, + "files": [ + "include/compatibility-fixes.php" + ] }, "autoload-dev": { "psr-4": { diff --git a/include/compatibility-fixes.php b/include/compatibility-fixes.php new file mode 100644 index 000000000..6cb69d683 --- /dev/null +++ b/include/compatibility-fixes.php @@ -0,0 +1,14 @@ +> $compatibilityFixes + */ +$compatibilityFixes = [ + CompatibilityFix\MovedClasses::class, +]; + +foreach ($compatibilityFixes as $compatibilityFix) { + $compatibilityFix::register(); +} diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 028a78b2a..4c98e5f04 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -5,6 +5,7 @@ parameters: level: 5 inferPrivatePropertyTypeFromConstructor: true paths: + - include/ - src/ ignoreErrors: - '#^Unsafe usage of new static\(\).$#' diff --git a/src/CompatibilityFix.php b/src/CompatibilityFix.php new file mode 100644 index 000000000..06b3198dd --- /dev/null +++ b/src/CompatibilityFix.php @@ -0,0 +1,19 @@ + Associative array of class alias mappings. + */ + const ALIASES = [ + // v0.9.0 - moved HTML-based utility into a separate `Html` sub-namespace. + 'AmpProject\AtRule' => 'AmpProject\Html\AtRule', + 'AmpProject\Attribute' => 'AmpProject\Html\Attribute', + 'AmpProject\LengthUnit' => 'AmpProject\Html\LengthUnit', + 'AmpProject\RequestDestination' => 'AmpProject\Html\RequestDestination', + 'AmpProject\Role' => 'AmpProject\Html\Role', + 'AmpProject\Tag' => 'AmpProject\Html\Tag', + + // v0.9.0 - extracted `Encoding` out of `Dom\Document`, as it is turned into AMP value object. + 'AmpProject\Dom\Document\Encoding' => 'AmpProject\Encoding', + + ]; + + /** + * Register the compatibility fix. + * + * @return void + */ + public static function register() + { + spl_autoload_register(__CLASS__ . '::autoloader'); + } + + /** + * Autoloader to register. + * + * @param string $oldClassName Old class name that was requested to be autoloaded. + * @return void + */ + public static function autoloader($oldClassName) + { + if (! array_key_exists($oldClassName, self::ALIASES)) { + return; + } + + class_alias(self::ALIASES[$oldClassName], $oldClassName, true); + } +} diff --git a/tests/CompatibilityFix/MovedClassesTest.php b/tests/CompatibilityFix/MovedClassesTest.php new file mode 100644 index 000000000..20fed1fc7 --- /dev/null +++ b/tests/CompatibilityFix/MovedClassesTest.php @@ -0,0 +1,22 @@ +assertTrue(class_exists($old) || interface_exists($old)); + } + } +}