Skip to content

Commit

Permalink
[SymfonyDI] add LatteEngineFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 9, 2017
1 parent 4e91065 commit 2e13e37
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 12 deletions.
17 changes: 12 additions & 5 deletions src/DependencyInjection/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ApiGen\DependencyInjection;

use ApiGen\DependencyInjection\CompilerPass\CollectorCompilerPass;
use ApiGen\DependencyInjection\CompilerPass\LatteFiltersCollectorCompilerPass;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
Expand All @@ -14,6 +15,11 @@ public function __construct()
parent::__construct('dev',true);
}

public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(__DIR__ . '/../config/services.yml');
}

/**
* @return string[]
*/
Expand All @@ -22,18 +28,19 @@ public function registerBundles(): array
return [];
}

public function registerContainerConfiguration(LoaderInterface $loader): void
public function getCacheDir(): string
{
$loader->load(__DIR__ . '/../config/services.yml');
return sys_get_temp_dir() . '/_apigen_kernel_cache';
}

public function getCacheDir(): string
public function getLogDir(): string
{
return sys_get_temp_dir();
return sys_get_temp_dir() . '/_apigen_kernel_log';
}

protected function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new CollectorCompilerPass());
$container->addCompilerPass(new CollectorCompilerPass);
$container->addCompilerPass(new LatteFiltersCollectorCompilerPass);
}
}
26 changes: 26 additions & 0 deletions src/DependencyInjection/CompilerPass/CollectorCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace ApiGen\DependencyInjection\CompilerPass;

use ApiGen\Annotation\AnnotationDecorator;
use ApiGen\Annotation\Contract\AnnotationSubscriber\AnnotationSubscriberInterface;
use ApiGen\Element\Contract\ReflectionCollector\BasicReflectionCollectorInterface;
use ApiGen\Element\ReflectionCollectorCollector;
use ApiGen\ModularConfiguration\CommandDecorator;
Expand All @@ -10,6 +12,8 @@
use ApiGen\ModularConfiguration\Contract\Option\OptionInterface;
use ApiGen\Reflection\Contract\Transformer\TransformerInterface;
use ApiGen\Reflection\TransformerCollector;
use ApiGen\StringRouting\Contract\Route\RouteInterface;
use ApiGen\StringRouting\StringRouter;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
Expand All @@ -25,6 +29,8 @@ public function process(ContainerBuilder $containerBuilder): void
$this->collectOptionsToConfigurationResolver($containerBuilder);
$this->collectTransformersToTransformerCollector($containerBuilder);
$this->collectReflectionCollectorsToReflectionCollectorCollector($containerBuilder);
$this->collectAnnotationSubscribersToAnnotationDecorator($containerBuilder);
$this->collectRoutesToStringRouter($containerBuilder);
}

private function collectCommandsToApplication(ContainerBuilder $containerBuilder): void
Expand Down Expand Up @@ -76,4 +82,24 @@ private function collectReflectionCollectorsToReflectionCollectorCollector(Conta
'addReflectionCollector'
);
}

private function collectAnnotationSubscribersToAnnotationDecorator(ContainerBuilder $containerBuilder): void
{
DefinitionCollector::loadCollectorWithType(
$containerBuilder,
AnnotationDecorator::class,
AnnotationSubscriberInterface::class,
'addAnnotationSubscriber'
);
}

private function collectRoutesToStringRouter(ContainerBuilder $containerBuilder): void
{
DefinitionCollector::loadCollectorWithType(
$containerBuilder,
StringRouter::class,
RouteInterface::class,
'addRoute'
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

namespace ApiGen\DependencyInjection\CompilerPass;

use ApiGen\Latte\FiltersAwareLatteEngineFactory;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symplify\ModularLatteFilters\Contract\DI\LatteFiltersProviderInterface;
use Symplify\PackageBuilder\Adapter\Symfony\DependencyInjection\DefinitionCollector;

final class LatteFiltersCollectorCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $containerBuilder): void
{
DefinitionCollector::loadCollectorWithType(
$containerBuilder,
FiltersAwareLatteEngineFactory::class,
LatteFiltersProviderInterface::class,
'addFiltersProvider'
);
}
}
31 changes: 31 additions & 0 deletions src/Latte/FiltersAwareLatteEngineFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace ApiGen\Latte;

use Latte\Engine;
use Symplify\ModularLatteFilters\Contract\DI\LatteFiltersProviderInterface;

final class FiltersAwareLatteEngineFactory
{
/**
* @var LatteFiltersProviderInterface[]
*/
private $filtersProviders = [];

public function addFiltersProvider(LatteFiltersProviderInterface $filtersProvider): void
{
$this->filtersProviders[] = $filtersProvider;
}

public function create(): Engine
{
$latteEngine = new Engine;
foreach ($this->filtersProviders as $filtersProvider) {
foreach ($filtersProvider->getFilters() as $name => $callback) {
$latteEngine->addFilter($name, $callback);
}
}

return $latteEngine;
}
}
18 changes: 11 additions & 7 deletions src/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ services:

# 3rd party services (cannot be autowired)
Symfony\Component\Console\Output\ConsoleOutput: ~
phpDocumentor\Reflection\DocBlockFactory: ~
phpDocumentor\Reflection\DocBlock\StandardTagFactory: ~

# DocBlock
phpDocumentor\Reflection\DocBlockFactory:
factory: phpDocumentor\Reflection\DocBlockFactory::createInstance

FSHL\Highlighter: ~
FSHL\Output\Html: ~

Latte\Engine: ~
Symfony\Component\EventDispatcher\EventDispatcher: ~
phpDocumentor\Reflection\Types\ContextFactory: ~
phpDocumentor\Reflection\FqsenResolver: ~
Expand All @@ -24,8 +25,6 @@ services:
ApiGen\:
resource: '../../src'
exclude: '../../src/{Application/Command,Event,Exception}'
# ApiGen\Console\:
# resource: '../../src/Console'
ApiGen\ModularConfiguration\Option\:
resource: '../../packages/ModularConfiguration/src/Option'
ApiGen\Element\:
Expand All @@ -36,7 +35,7 @@ services:
resource: '../../packages/Annotation/src'
ApiGen\Reflection\:
resource: '../../packages/Reflection/src'
exclude: "../../packages/Reflection/src/{Exception,Reflection}"
exclude: '../../packages/Reflection/src/{Exception,Reflection}'

# root collectors (move to package builder?)
ApiGen\ModularConfiguration\CommandDecorator: ~
Expand All @@ -48,4 +47,9 @@ services:
ApiGen\Configuration\Configuration: ~
ApiGen\Reflection\Parser\Parser: ~
ApiGen\Reflection\ReflectionStorage: ~
ApiGen\Utils\RelativePathResolver: ~
ApiGen\Utils\RelativePathResolver: ~

# Filter Aware Latte Engine
ApiGen\Latte\FiltersAwareLatteEngineFactory: ~
Latte\Engine:
factory: 'ApiGen\Latte\FiltersAwareLatteEngineFactory:create'

0 comments on commit 2e13e37

Please sign in to comment.