Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge pull request #2 from M6Web/feature/importUpdateFromPrivateRepo
Browse files Browse the repository at this point in the history
Import all modifications from internal repo
  • Loading branch information
valentin-claras committed Feb 11, 2016
2 parents 4f0caa9 + 1911490 commit 9bad15c
Show file tree
Hide file tree
Showing 26 changed files with 438 additions and 157 deletions.
3 changes: 2 additions & 1 deletion .atoum.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<?php
$runner->addTestsFromDirectory(__DIR__.'/src/M6Web/Bundle/CacheExtraBundle/Tests');

$runner->addTestsFromDirectory(__DIR__.'/src/Bundle/Tests');
2 changes: 1 addition & 1 deletion .coke
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
standard=Cytron
standard=vendor/m6web/symfony2-coding-standard/M6Web_Symfony2

src/
!Tests/
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
vendor
bin/
vendor/
composer.lock

7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ language: php
php:
- 5.4
- 5.5
- 5.6
- 7.0

before_script:
- wget http://getcomposer.org/composer.phar
- php composer.phar install --dev
- php composer.phar install

script:
- vendor/bin/atoum
- bin/atoum
- bin/coke
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ Add some features for caching system:
## Launch tests

```shell
$ ./vendor/bin/atoum
$ ./bin/atoum
```
23 changes: 17 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@
"name": "m6web/cache-extra-bundle",
"description": "SF2 Cache extra features",
"type": "symfony-bundle",
"autoload": {
"psr-0": { "": "src/" }
},
"minimum-stability" : "dev",
"prefer-stable": true,
"require": {
"php": ">5.4"
},
"require-dev": {
"m6web/firewall-bundle": "~0.3",
"atoum/atoum-bundle": "~1.0"
"symfony/symfony": "~2.3|~3.0",
"m6web/firewall-bundle": "~1.0",
"atoum/atoum": "~2.0",
"m6web/coke": "~2.1",
"m6web/symfony2-coding-standard": "~3.1"
},
"autoload": {
"psr-4": {
"M6Web\\Bundle\\CacheExtraBundle\\": "src/Bundle",
"M6Web\\Component\\CacheExtra\\": "src/Component"
}
},
"config": {
"bin-dir": "bin/"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public function __construct($cacheActionListener)
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$this->data = [
'blocks' => null,
'total' => null,
'hits' => null,
'miss' => null,
];

if (!is_null($this->cacheActionListener)) {
$blocks = $this->cacheActionListener->getCachedBlocks();
$total = 0;
Expand All @@ -40,12 +47,12 @@ public function collect(Request $request, Response $response, \Exception $except
$cached ? $nbHits++ : $nbMiss++;
}

$this->data = array(
$this->data = [
'blocks' => $blocks,
'total' => $total,
'hits' => $nbHits,
'miss' => $nbMiss
);
'miss' => $nbMiss,
];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class M6WebCacheExtraExtension extends Extension
{
protected $twigExtension = array('renderCache');

/**
* @return string
*/
public function getAlias()
{
return 'm6_cache_extra';
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -45,6 +53,7 @@ public function load(array $configs, ContainerBuilder $container)
}
}


/**
* Load the cache action configuration
*
Expand All @@ -58,7 +67,7 @@ protected function loadActionCacheConfiguration(ContainerBuilder $container, $ca
if (!$container->hasParameter('m6.listener.cache_action.excludekeys')) {
$container->setParameter(
'm6.listener.cache_action.excludekeys',
array('_template', '_cache', '_method')
['_template', '_cache', '_method']
);
}

Expand All @@ -70,13 +79,4 @@ protected function loadActionCacheConfiguration(ContainerBuilder $container, $ca
$definition->addTag('kernel.event_subscriber');
$container->setDefinition('m6.action_cache.listener', $definition);
}


/**
* @return string
*/
public function getAlias()
{
return 'm6_cache_extra';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
*/
class CachedFragmentRenderer extends InlineFragmentRenderer
{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'cached';
}

/**
* {@inheritdoc}
*/
Expand All @@ -30,12 +38,4 @@ protected function createSubRequest($uri, Request $request)

return $subRequest;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'cached';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,22 @@ class CacheActionListener implements EventSubscriberInterface

/**
* Constructor.
*
* @param boolean $debug active or not debug mode
* @param string $env kernel env
*
* @return void
*/
public function __construct($debug, $env)
{
$this->cachedBlocks = array();
$this->cachedBlocks = [];
$this->debug = $debug;
$this->env = $env;
$this->excludeKey = array();
$this->excludeKey = [];
}

/**
* Set the cache service
* @param CacheInterface $cacheService The cache service to use to store response
*
* @return void
* @param CacheInterface $cacheService The cache service to use to store response
*/
public function setCacheService(CacheInterface $cacheService)
{
Expand All @@ -62,6 +60,29 @@ public function setCacheKeyExclude(array $exclude)
$this->excludeKey = $exclude;
}

/**
* Return list of cached blocks with status
*
* @return array list of cached controller with status
*/
public function getCachedBlocks()
{
return $this->cachedBlocks;
}

/**
* Return static list of subscribed events
*
* @return array List of events we want to subscribe to
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => 'onKernelResponse',
KernelEvents::REQUEST => 'onKernelRequest',
];
}

/**
* Method called on kernel.request event. Handle only "subrequest"
* @param KernelEvent $event The received event
Expand All @@ -82,8 +103,8 @@ public function onKernelRequest(KernelEvent $event)
$controller = $request->attributes->get('controllerName');
$fromCache = false;

$responseContent = $this->cacheService->get($cacheKey);
if ($responseContent || ($responseContent === '' && !$request->attributes->get('ignore_errors')) ) {
$responseContent = $this->cacheService->getConcurrent($cacheKey);
if ($responseContent || ($responseContent === '' && !$request->attributes->get('ignore_errors'))) {

$response = new Response($responseContent);
$response->headers->set('server_cached', 1);
Expand Down Expand Up @@ -119,7 +140,7 @@ public function onKernelResponse(FilterResponseEvent $event)

$cacheKey = $request->attributes->get('cache_key');
$ttl = $response->headers->getCacheControlDirective('max-age');
$this->cacheService->set($cacheKey, $response->getContent(), $ttl);
$this->cacheService->setConcurrent($cacheKey, $response->getContent(), $ttl);
if ($this->debug) {
$this->decorateResponse($request, $response, $request->attributes->get('_controller'), false, $ttl);
}
Expand All @@ -133,12 +154,12 @@ public function onKernelResponse(FilterResponseEvent $event)
*
* @return string The cache key to use
*
* @throws \M6Web\Component\CacheExtra\CacheException
* @throws \Exception
*/
private function getRequestCacheKey(Request $request)
{
$p = $request->attributes->all();
$parameters = array();
$parameters = [];
foreach ($p as $k => $v) {

// On ne prend pas ces clefs
Expand Down Expand Up @@ -171,21 +192,17 @@ private function getRequestCacheKey(Request $request)
private function validateRequestParameter($value)
{
if (null == $value) {

return true;
}

if (is_array($value)) {

foreach ($value as $v) {
if (!$this->validateRequestParameter($v)) {

return false;
}
}

} elseif (!is_scalar($value)) {

return false;
}

Expand Down Expand Up @@ -227,27 +244,4 @@ private function decorateResponse(Request $request, Response $response, $control

$response->setContent($html);
}

/**
* Return static list of subscribed events
*
* @return array List of events we want to subscribe to
*/
public static function getSubscribedEvents()
{
return array(
KernelEvents::RESPONSE => 'onKernelResponse',
KernelEvents::REQUEST => 'onKernelRequest'
);
}

/**
* Return list of cached blocks with status
*
* @return array list of cached controller with status
*/
public function getCachedBlocks()
{
return $this->cachedBlocks;
}
}
Loading

0 comments on commit 9bad15c

Please sign in to comment.