-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from FriendsOfSymfony/configure-tags
Also have configuration rules for tags
- Loading branch information
Showing
16 changed files
with
431 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,13 @@ | |
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; | ||
|
||
/** | ||
* This is the class that validates and merges configuration from your app/config files | ||
* This class contains the configuration information for the bundle | ||
* | ||
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class} | ||
* This information is solely responsible for how the different configuration | ||
* sections are normalized, and merged. | ||
* | ||
* @author David de Boer <[email protected]> | ||
* @author David Buchmann <[email protected]> | ||
*/ | ||
class Configuration implements ConfigurationInterface | ||
{ | ||
|
@@ -24,6 +28,18 @@ public function getConfigTreeBuilder() | |
$rootNode = $treeBuilder->root('fos_http_cache'); | ||
|
||
$rootNode | ||
->validate() | ||
->ifTrue(function ($v) {return $v['cache_manager']['enabled'] && !isset($v['proxy_client']);}) | ||
->then(function ($v) { | ||
if ('auto' === $v['cache_manager']['enabled']) { | ||
$v['cache_manager']['enabled'] = false; | ||
|
||
return $v; | ||
} | ||
throw new InvalidConfigurationException('You need to configure a proxy_client to use the cache_manager.'); | ||
}) | ||
->end() | ||
|
||
->children() | ||
->booleanNode('debug') | ||
->defaultValue('%kernel.debug%') | ||
|
@@ -39,9 +55,8 @@ public function getConfigTreeBuilder() | |
$this->addUserContextListenerSection($rootNode); | ||
$this->addRulesSection($rootNode); | ||
$this->addProxyClientSection($rootNode); | ||
$this->addTagListenerSection($rootNode); | ||
$this->addCacheManager($rootNode); | ||
$this->addFlashMessageListenerSection($rootNode); | ||
$this->addInvalidatorsSection($rootNode); | ||
|
||
return $treeBuilder; | ||
} | ||
|
@@ -106,6 +121,21 @@ private function addRulesSection(ArrayNodeDefinition $rootNode) | |
|
||
$this->addMatchSection($rules); | ||
$this->addHeaderSection($rules); | ||
$this->addTagSection($rules); | ||
} | ||
|
||
private function addTagSection(NodeBuilder $rules) | ||
{ | ||
$rules | ||
->arrayNode('tags') | ||
->prototype('scalar') | ||
->info('Tags to add to the response on safe requests, to invalidate on unsafe requests') | ||
->end()->end() | ||
->arrayNode('tag_expressions') | ||
->prototype('scalar') | ||
->info('Tags to add to the response on safe requests, to invalidate on unsafe requests') | ||
->end() | ||
; | ||
} | ||
|
||
private function addMatchSection(NodeBuilder $rules) | ||
|
@@ -221,9 +251,37 @@ private function addProxyClientSection(ArrayNodeDefinition $rootNode) | |
->end(); | ||
} | ||
|
||
private function addTagListenerSection(ArrayNodeDefinition $rootNode) | ||
private function addCacheManager(ArrayNodeDefinition $rootNode) | ||
{ | ||
$rootNode | ||
$invalidationNode = $rootNode | ||
->children() | ||
->arrayNode('cache_manager') | ||
->addDefaultsIfNotSet() | ||
->beforeNormalization() | ||
->ifArray() | ||
->then(function ($v) { | ||
$v['enabled'] = isset($v['enabled']) ? $v['enabled'] : true; | ||
|
||
return $v; | ||
}) | ||
->end() | ||
->info('Configure the cache manager. Needs a proxy_client to be configured.') | ||
->children() | ||
->enumNode('enabled') | ||
->values(array(true, false, 'auto')) | ||
->defaultValue('auto') | ||
->info('Allows to disable the invalidation manager. Enabled by default if you configure a proxy client.') | ||
->end() | ||
->end() | ||
; | ||
|
||
$this->addTagListenerSection($invalidationNode); | ||
$this->addInvalidatorsSection($invalidationNode); | ||
} | ||
|
||
private function addTagListenerSection(ArrayNodeDefinition $invalidationNode) | ||
{ | ||
$invalidationNode | ||
->children() | ||
->arrayNode('tag_listener') | ||
->addDefaultsIfNotSet() | ||
|
@@ -272,21 +330,24 @@ private function addFlashMessageListenerSection(ArrayNodeDefinition $rootNode) | |
->end(); | ||
} | ||
|
||
private function addInvalidatorsSection(ArrayNodeDefinition $rootNode) | ||
private function addInvalidatorsSection(ArrayNodeDefinition $invalidationNode) | ||
{ | ||
$rootNode | ||
$invalidationNode | ||
->children() | ||
->arrayNode('invalidators') | ||
->arrayNode('route_invalidators') | ||
->useAttributeAsKey('name') | ||
->info('Groups of origin routes that invalidate target routes when a request is made') | ||
->prototype('array') | ||
->children() | ||
->arrayNode('origin_routes') | ||
->isRequired() | ||
->requiresAtLeastOneElement() | ||
->prototype('scalar')->end() | ||
->info('Invalidate the target routes in this group when one of these routes is called') | ||
->end() | ||
->arrayNode('invalidate_routes') | ||
->useAttributeAsKey('name') | ||
->info('Target routes to invalidate when an origin route is called') | ||
->prototype('array') | ||
->children() | ||
->scalarNode('parameter_mapper')->end() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace FOS\HttpCacheBundle\EventListener; | ||
|
||
use FOS\HttpCacheBundle\Http\RuleMatcherInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class AbstractRuleSubscriber | ||
{ | ||
/** | ||
* @var array List of arrays with RuleMatcher, settings array. | ||
*/ | ||
private $rulesMap = array(); | ||
|
||
/** | ||
* Add a rule matcher with a list of header directives to apply if the | ||
* request and response are matched. | ||
* | ||
* @param RuleMatcherInterface $ruleMatcher The headers apply to responses matched by this matcher. | ||
* @param array $settings An array of header configuration. | ||
* @param int $priority Optional priority of this matcher. Higher priority is applied first. | ||
*/ | ||
public function addRule( | ||
RuleMatcherInterface $ruleMatcher, | ||
array $settings = array(), | ||
$priority = 0 | ||
) { | ||
if (!isset($this->rulesMap[$priority])) { | ||
$this->rulesMap[$priority] = array(); | ||
} | ||
$this->rulesMap[$priority][] = array($ruleMatcher, $settings); | ||
} | ||
/** | ||
* Return the settings for the current request if any rule matches. | ||
* | ||
* @param Request $request | ||
* @param Response $response | ||
* | ||
* @return array|false Settings to apply or false if no rule matched. | ||
*/ | ||
protected function matchConfiguration(Request $request, Response $response) | ||
{ | ||
foreach ($this->getRules() as $elements) { | ||
if ($elements[0]->matches($request, $response)) { | ||
return $elements[1]; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Get the rules ordered by priority. | ||
* | ||
* @return array of array with rule matcher, settings | ||
*/ | ||
private function getRules() | ||
{ | ||
$sortedRules = array(); | ||
krsort($this->rulesMap); | ||
foreach ($this->rulesMap as $rules) { | ||
$sortedRules = array_merge($sortedRules, $rules); | ||
} | ||
|
||
return $sortedRules; | ||
} | ||
} |
Oops, something went wrong.