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

Add legacy autoloader to support Matomo namespaces in 3.X #16069

Merged
merged 5 commits into from
Jun 17, 2020
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
31 changes: 31 additions & 0 deletions LegacyAutoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

class LegacyAutoloader
{
public function __construct()
{
spl_autoload_register(array($this, 'load_class'));
}

public static function register()
{
new LegacyAutoloader();
}

public function load_class($className)
{
if (strpos($className, 'Matomo\\') === 0) {
$newName = 'Piwik' . substr($className, 6);
if (class_exists($newName) && !class_exists($className, false)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw the second class_exists is needed because things could happen like

* new \Matomo\Foo
* It then does `class_exists(\Piwik\Foo)`
* It then loads `core/Foo.php`
* This class defines `Matomo\Foo`
* Then it adds alias `Piwik\Foo` to `Matomo\Foo`
* This first `class_exists($newName)` basically caused the file `core/Foo.php` to load which defines `Matomo\Foo` so we no longer want to execute `class_alias(\Matomo\Foo, Piwik\Foo)` since the first class_exists actually defined the class already indirectly.

@class_alias($newName, $className);
}
} elseif (strpos($className, 'Piwik\\') === 0) {
$newName = 'Matomo' . substr($className, 5);
if (class_exists($newName) && !class_exists($className, false)) {
@class_alias($newName, $className);
}
}
}
}

LegacyAutoloader::register();
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@
"HTML_": "libs/",
"PEAR_": "libs/",
"Archive_": "libs/"
}
},
"files": ["LegacyAutoloader.php"]
},
"autoload-dev": {
"psr-4": {
Expand Down