Skip to content

Commit

Permalink
use generator without AOP
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbumbacea committed Dec 7, 2016
1 parent 167999c commit 47d0f3c
Show file tree
Hide file tree
Showing 19 changed files with 671 additions and 172 deletions.
35 changes: 0 additions & 35 deletions app/AppKernel.php

This file was deleted.

7 changes: 3 additions & 4 deletions app/config.yml
@@ -1,17 +1,16 @@
parameters:
cache.service: cache.service

jms_aop:
cache_dir: %kernel.cache_dir%/jms_aop


monolog:
handlers:
main:
type: test
level: info

services:
cache_warmer:
class: Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate
arguments: [""]
cache.service:
class: Symfony\Component\Cache\Adapter\ArrayAdapter
cache.testservice:
Expand Down
7 changes: 3 additions & 4 deletions composer.json
Expand Up @@ -10,8 +10,8 @@
}
],
"require": {
"php": ">=5.6",
"jms/aop-bundle": "1.*",
"php": ">=7.0",
"ocramius/proxy-manager": "@stable",
"psr/log": "~1",
"psr/cache": "1.0.*",
"doctrine/annotations": "@stable"
Expand All @@ -31,8 +31,7 @@
"symfony/framework-bundle": "@stable"
},
"autoload": {
"psr-4": { "CacheBundle\\": "src/CacheBundle" },
"psr-0": { "CacheBundle\\Annotation": "src/" }
"psr-4": { "CacheBundle\\": "src/CacheBundle/" }
},
"config": {
"optimize-autoloader": true
Expand Down
23 changes: 20 additions & 3 deletions src/CacheBundle/CacheBundle.php
Expand Up @@ -2,16 +2,33 @@

namespace CacheBundle;

use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use CacheBundle\DependencyInjection\CacheCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class CacheBundle extends Bundle
{
protected $autoloader;
/** @var \ProxyManager\Configuration */
protected $config;
protected $getProxyConfig;

public function shutdown()
{
spl_autoload_unregister($this->container->get('emag.cache.proxy.config')->getProxyAutoloader());
}

public function boot()
{
$this->autoloader = spl_autoload_register($this->container->get('emag.cache.proxy.config')->getProxyAutoloader());
}

public function build(ContainerBuilder $container)
{
parent::build($container);
$compilerPass = new CacheCompilerPass();

$container->addCompilerPass(new CacheCompilerPass(), PassConfig::TYPE_OPTIMIZE);
$container->addCompilerPass($compilerPass);
parent::build($container);
}
}
49 changes: 0 additions & 49 deletions src/CacheBundle/CacheCompilerPass.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/CacheBundle/ContextAwareCache.php

This file was deleted.

86 changes: 86 additions & 0 deletions src/CacheBundle/DependencyInjection/CacheCompilerPass.php
@@ -0,0 +1,86 @@
<?php

namespace CacheBundle\DependencyInjection;


use CacheBundle\Annotation\Cache;
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Filesystem\Filesystem;

class CacheCompilerPass implements CompilerPassInterface
{
/**
* You can modify the container here before it is dumped to PHP code.
*
* @param ContainerBuilder $container
*
* @api
*/
public function process(ContainerBuilder $container)
{
$fs = new Filesystem();
$fs->mkdir(
str_replace(
'%kernel.cache_dir%',
$container->getParameter('kernel.cache_dir'),
$container->getParameter('emag.cacheable.service.path')
)
);


$this->proxyServicesToBeCached($container);
}

/**
* @param ContainerBuilder $container
* @return array
*/
protected function proxyServicesToBeCached(ContainerBuilder $container)
{
$annotationReader = new AnnotationReader();
$servicesToBeCached = [];
foreach ($container->getDefinitions() as $serviceId => $definition) {
if ($definition->getClass() === null) {
continue;
}
$originalReflection = new \ReflectionClass($definition->getClass());
foreach ($originalReflection->getMethods() as $method) {
if ($annotation = $annotationReader->getMethodAnnotation($method, Cache::class)) {
if ($method->isFinal()) {
throw new BadMethodCallException('Final methods can not be cached!');
}
if ($method->isAbstract()) {
throw new BadMethodCallException('Abstract methods can not be cached!');
}
if ($method->isStatic()) {
throw new BadMethodCallException('Static methods can not be cached!');
}

$factory = new Definition($definition->getClass());
$factory->setFactory([new Reference('emag.cache.proxy.factory'), 'generate']);
$factory->setTags($definition->getTags());
$factory->setArguments([$definition->getClass(), $definition->getArguments()]);
$factory->setMethodCalls($definition->getMethodCalls());
$factory->setProperties($definition->getProperties());
$factory->setProperties($definition->getProperties());
$factory->addMethodCall('setReaderForCacheMethod', [new Reference("annotation_reader")]);
$factory->addMethodCall('setCacheServiceForMethod', [new Reference($container->getParameter('cache.service'))]);
$container->getDefinition('emag.cache.warmup')->addMethodCall('addClassToGenerate', [$definition->getClass()]);


$container->setDefinition($serviceId, $factory);
break;
}
}
}

return $servicesToBeCached;
}


}
8 changes: 6 additions & 2 deletions src/CacheBundle/DependencyInjection/CacheExtension.php
Expand Up @@ -18,8 +18,12 @@ class CacheExtension extends Extension
/**
* @inheritdoc
*/
public function load(array $config, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container)
{
// TODO: Implement load() method.
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}
64 changes: 0 additions & 64 deletions src/CacheBundle/DependencyInjection/PointCut.php

This file was deleted.

0 comments on commit 47d0f3c

Please sign in to comment.