Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Commit 7e4d106

Browse files
committed
First real commit
1 parent d6c02d7 commit 7e4d106

File tree

6 files changed

+180
-0
lines changed

6 files changed

+180
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace phpFastCache\Bundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6+
use Symfony\Component\Config\Definition\ConfigurationInterface;
7+
8+
/**
9+
* Class Configuration
10+
* @package phpFastCache\Bundle\DependencyInjection
11+
*/
12+
class Configuration implements ConfigurationInterface
13+
{
14+
/**
15+
* {@inheritDoc}
16+
*
17+
* @throws \RuntimeException
18+
*/
19+
public function getConfigTreeBuilder()
20+
{
21+
$treeBuilder = new TreeBuilder();
22+
$rootNode = $treeBuilder->root('php_fast_cache');
23+
24+
$rootNode
25+
->children()
26+
->arrayNode('drivers')
27+
->useAttributeAsKey('name')
28+
->prototype('array')
29+
->children()
30+
->enumNode('type')->isRequired()->values(array('Files', 'MongoDb', 'Apc'))->end() // @TODO : Add all available drivers
31+
->arrayNode('parameters')->isRequired()->prototype('scalar')->end()
32+
->end()
33+
->end()
34+
->end() // drivers
35+
->end();
36+
37+
return $treeBuilder;
38+
}
39+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace phpFastCache\Bundle\DependencyInjection;
4+
5+
use phpFastCache\Exceptions\phpFastCacheDriverException;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\Config\FileLocator;
8+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
9+
use Symfony\Component\DependencyInjection\Loader;
10+
11+
/**
12+
* Class phpFastCacheExtension
13+
* @package phpFastCache\Bundle\DependencyInjection
14+
*/
15+
class phpFastCacheExtension extends Extension
16+
{
17+
/**
18+
* {@inheritDoc}
19+
*
20+
* @throws \phpFastCache\Exceptions\phpFastCacheDriverCheckException
21+
* @throws \phpFastCache\Exceptions\phpFastCacheDriverException
22+
* @throws \Exception
23+
* @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
24+
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
25+
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
26+
*/
27+
public function load(array $configs, ContainerBuilder $container)
28+
{
29+
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
30+
$loader->load('services.yml');
31+
32+
$configuration = new Configuration();
33+
$config = $this->processConfiguration($configuration, $configs);
34+
35+
foreach ($config['drivers'] as $name => $driver) {
36+
$class = "phpFastCache\\Drivers\\" . $driver['type'] . '\Driver';
37+
foreach ($driver['parameters'] as $parameter_name => $parameter) {
38+
if (!$class::isValidOption($parameter_name, $parameter)) {
39+
throw new phpFastCacheDriverException("Option $parameter_name in driver {$driver['type']} doesn't exists");
40+
}
41+
}
42+
}
43+
44+
$container->setParameter('phpfastcache', $config);
45+
46+
}
47+
}

Resources/config/services.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
parameters:
2+
php_fast_cache.cache.class: phpFastCache\Bundle\Service\Cache
3+
4+
services:
5+
php_fast_cache.cache.service:
6+
class: "%php_fast_cache.cache.class%"
7+
arguments:
8+
- "%phpfastcache%"

Service/Cache.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace phpFastCache\Bundle\Service;
4+
5+
use phpFastCache\Cache\ExtendedCacheItemPoolInterface;
6+
use phpFastCache\CacheManager;
7+
use phpFastCache\Exceptions\phpFastCacheDriverException;
8+
9+
/**
10+
* Class Cache
11+
* @package phpFastCache\Bundle\Service
12+
*/
13+
class Cache
14+
{
15+
/**
16+
* Contains all cache instances
17+
*
18+
* @var \phpFastCache\Cache\ExtendedCacheItemPoolInterface[]
19+
*/
20+
private $cacheInstances = [];
21+
22+
/**
23+
* Cache constructor.
24+
*
25+
* @param $drivers
26+
*
27+
* @throws \phpFastCache\Exceptions\phpFastCacheDriverException
28+
*/
29+
public function __construct($drivers)
30+
{
31+
foreach ($drivers['drivers'] as $name => $driver) {
32+
dump($driver);
33+
$this->createInstance($name, CacheManager::getInstance($driver['type'], $driver['parameters']));
34+
}
35+
}
36+
37+
/**
38+
* Set a new cache instance
39+
*
40+
* @param string $name
41+
* @param ExtendedCacheItemPoolInterface $instance
42+
*
43+
* @throws \phpFastCache\Exceptions\phpFastCacheDriverException
44+
*/
45+
public function createInstance($name, ExtendedCacheItemPoolInterface $instance)
46+
{
47+
if (array_key_exists($name, $this->cacheInstances) && $this->cacheInstances[$name] instanceof ExtendedCacheItemPoolInterface) {
48+
throw new phpFastCacheDriverException("Driver named $name already exists");
49+
}
50+
$this->cacheInstances[$name] = $instance;
51+
}
52+
53+
/**
54+
* get a cache instance
55+
*
56+
* @param string $name Name of configured driver
57+
*
58+
* @return \phpFastCache\Cache\ExtendedCacheItemPoolInterface
59+
*
60+
* @throws \phpFastCache\Exceptions\phpFastCacheDriverException
61+
*/
62+
public function get($name)
63+
{
64+
if (!array_key_exists($name, $this->cacheInstances)) {
65+
throw new phpFastCacheDriverException("Driver named $name not exists");
66+
}
67+
if (!$this->cacheInstances[$name] instanceof ExtendedCacheItemPoolInterface) {
68+
throw new phpFastCacheDriverException("Driver named $name already instanciated");
69+
}
70+
71+
return $this->cacheInstances[$name];
72+
}
73+
}

phpFastCacheBundle.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace phpFastCache\Bundle;
4+
5+
use Symfony\Component\HttpKernel\Bundle\Bundle;
6+
7+
/**
8+
* Class phpFastCacheBundle
9+
* @package phpFastCache\Bundle
10+
*/
11+
class phpFastCacheBundle extends Bundle
12+
{
13+
}

src/phpfastcache-bundle/index.html

Whitespace-only changes.

0 commit comments

Comments
 (0)