<?php
namespace phpDocumentor\Configuration;
use Cilex\Application;
use Cilex\ServiceProviderInterface;
use Doctrine\Common\Annotations\AnnotationReader;
use JMS\Serializer\Serializer;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\Console\Input\InputOption;
class ServiceProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
$this->addMerger($app);
$app->extend(
'console',
function (ConsoleApplication $console) {
$console->getDefinition()->addOption(
new InputOption(
'config',
'c',
InputOption::VALUE_OPTIONAL,
'Location of a custom configuration file'
)
);
return $console;
}
);
$app['config.path.template'] = __DIR__ . '/Resources/phpdoc.tpl.xml';
$app['config.path.user'] = getcwd()
. ((file_exists(getcwd() . '/phpdoc.xml')) ? '/phpdoc.xml' : '/phpdoc.dist.xml');
$app['config.class'] = 'phpDocumentor\Configuration';
$app['config'] = $app->share(
function ($app) {
$loader = new Loader($app['serializer'], $app['config.merger']);
return $loader->load($app['config.path.template'], $app['config.path.user'], $app['config.class']);
}
);
}
private function addMerger(Application $container)
{
$this->addMergerAnnotations($container);
$container['config.merger'] = $container->share(
function () {
return new Merger(new AnnotationReader());
}
);
}
private function addMergerAnnotations(Application $container)
{
if (!isset($container['serializer.annotations'])) {
throw new \RuntimeException(
'The configuration service provider depends on the JmsSerializer Service Provider but the '
. '"serializer.annotations" key could not be found in the container.'
);
}
$annotations = $container['serializer.annotations'];
$annotations[] = array(
'namespace' => 'phpDocumentor\Configuration\Merger\Annotation',
'path' => __DIR__ . '/../../'
);
$container['serializer.annotations'] = $annotations;
}
}