Files
Phraseanet/lib/Alchemy/Phrasea/Plugin/Management/AutoloaderGenerator.php
Romain Neutron 9dcb6a6342 Fix CS
2013-05-31 19:34:38 +02:00

108 lines
2.4 KiB
PHP

<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Plugin\Management;
use Alchemy\Phrasea\Plugin\Exception\RegistrationFailureException;
class AutoloaderGenerator
{
private $pluginDirectory;
public function __construct($pluginDirectory)
{
$this->pluginDirectory = $pluginDirectory;
}
public function write($manifests)
{
$this
->doWrite('autoload.php', $this->createLoader($manifests))
->doWrite('services.php', $this->createServices($manifests));
return $this;
}
private function doWrite($file, $data)
{
if (false === file_put_contents($this->pluginDirectory . DIRECTORY_SEPARATOR . $file, $data)) {
throw new RegistrationFailureException(sprintf('Failed to write %s', $file));
}
return $this;
}
private function createLoader($manifests)
{
$buffer = <<<EOF
<?php
// This file is automatically generated, please do not edit it.
// To update configuration, use bin/console plugins:* commands.
return call_user_func(function () {
EOF;
foreach ($manifests as $manifest) {
$autoloader = '/' . $manifest->getName() . DIRECTORY_SEPARATOR . "vendor" . DIRECTORY_SEPARATOR . "autoload.php";
$buffer .= <<<EOF
require __DIR__ . '$autoloader';
EOF;
}
// composer loader are preprent
$autoloader = '/../vendor/autoload.php';
$buffer .= <<<EOF
\$loader = require __DIR__ . '$autoloader';
return \$loader;\n});
EOF;
return $buffer;
}
private function createServices($manifests)
{
$buffer = <<<EOF
<?php
// This file is automatically generated, please do not edit it.
// To update configuration, use bin/console plugins:* commands.
use Alchemy\Phrasea\Application;
return call_user_func(function (Application \$app) {
EOF;
foreach ($manifests as $manifest) {
foreach ($manifest->getServices() as $service) {
$class = $service['class'];
$buffer .= <<<EOF
\$app->register($class::create(\$app));
EOF;
}
}
$buffer .= <<<EOF
return \$app;
}, \$app);
EOF;
return $buffer;
}
}