Open Screenfeed opened 3 years ago
I found a tool to convert from PSR-0 to PSR-4: sdrobov/autopsr4.
It's definitely beta software but interesting – in the same arena as Mozart.
I played around with it and the following composer.json should work for you:
{
"repositories": [
{
"url": "https://github.com/sdrobov/autopsr4",
"type": "git"
}
],
"require": {
"php": ">=5.6.0",
"league/container": "^2.4",
"mustache/mustache": "^2.13",
"composer/installers": "~1.0"
},
"require-dev": {
"coenjacobs/mozart": "0.6.0-beta-3",
"sdrobov/autopsr4": "dev-master"
},
"extra": {
"mozart": {
"dep_namespace": "Screenfeed\\AdminbarTools\\Dependencies\\",
"dep_directory": "/src/classes/Dependencies/",
"classmap_directory": "/src/classmap/dependencies/",
"classmap_prefix": "ScreenfeedAdminbarTools_"
}
},
"scripts": {
"convert-mustache-to-psr4": [
"php -r 'require_once __DIR__ . \"/vendor/autoload.php\"; chdir(\"vendor/mustache/mustache/src/\"); use AutoPsr4\\Parser; use AutoPsr4\\Replacer; use AutoPsr4\\UsageFinder; class Mustache_Replacer extends AutoPsr4\\App { public function run($argv) { $parser = new Parser(\"Mustache\", \"Mustache\"); $parser->parse(); $usageFinder = new UsageFinder($parser->getFiles(), $parser->getClasses()); $usageFinder->findUsages(); $replacer = new Replacer($usageFinder->getFiles()); $replacer->replace(); } }; (new Mustache_Replacer())->run(array());' "
],
"post-install-cmd": [
"@convert-mustache-to-psr4",
"\"vendor/bin/mozart\" compose",
"composer dump-autoload"
],
"post-update-cmd": [
"@convert-mustache-to-psr4",
"\"vendor/bin/mozart\" compose",
"composer dump-autoload"
]
}
}
The php code in there is because running autopsr4 required being in the correct working directory and having the correct arguments passed via the command line. A little refactoring, some error handling and a vendor/bin script could have it running nicely in Composer scripts.
<?php
require_once __DIR__ . "/vendor/autoload.php";
chdir("vendor/mustache/mustache/src/");
use AutoPsr4\Parser;
use AutoPsr4\Replacer;
use AutoPsr4\UsageFinder;
class Mustache_Replacer extends AutoPsr4\App {
public function run($argv) {
$parser = new Parser("Mustache", "Mustache");
$parser->parse();
$usageFinder = new UsageFinder($parser->getFiles(), $parser->getClasses());
$usageFinder->findUsages();
$replacer = new Replacer($usageFinder->getFiles());
$replacer->replace();
}
};
(new Mustache_Replacer())->run(array());
Thank you @BrianHenryIE, and sorry for the late answer.
I tried to use AutoPsr4 and it almost worked. The main issue comes from Mustach's Exception classes, like this one:
class Mustache_Exception_InvalidArgumentException extends InvalidArgumentException implements Mustache_Exception
{
Once all the prefixes removed and namespaces added, it becomes:
namespace Screenfeed\AdminbarTools\Dependencies\Mustache\Exception;
use Screenfeed\AdminbarTools\Dependencies\Mustache\Exception;
class InvalidArgumentException extends InvalidArgumentException implements Exception
{
This obviously leads to a fatal error.
Even after manually editing the 3 classes that have this issue, then:
PHP Fatal error: Uncaught Error: Class '__Mustache_a43d8d63357a090f4d90df06cc37cec7' not found in /sf-adminbar-tools/src/classes/Dependencies/Mustache/Engine.php:764
That's too much things to fix imho, so I guess I'll stick with the classmap. Anyway, thanks for your help :)
Hello, I'm trying to use Mozart
0.6.0-beta-3
with Mustache and other packages, but Mustache is the only one that doesn't seem to be namespaced. Fair warning, it's the first time I use Mozart so it can be a misconfiguration/misunderstanding on my side.composer.json
composer.lock
Taking a look at my code the following classes are not found:
While this one is:
Then if I look at Mustache's files, they are moved to the right folder but no namespaces:
src/classes/Dependencies/Mustache/Engine.php
src/classes/Dependencies/Mustache/Loader/FilesystemLoader.php
Finally, I managed to prefix the classes by using a classmap:
Did I miss something? Thanks.