Skip to content

update to use 2.0 of FOSHttpCache #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ branches:

matrix:
include:
- php: 5.5
env:
- COMPOSER_FLAGS="--prefer-lowest"
- SYMFONY_VERSION='2.8.*'
# fails to install. try to get this to work again before releasing
# - php: 5.5
# env:
# - COMPOSER_FLAGS="--prefer-lowest"
# - SYMFONY_VERSION='2.8.*'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jordi thinks we hit some hard to tell edge case: https://twitter.com/seldaek/status/805698883456270336

i comment this build out for now and try again when we released the library

- php: 7.1
env:
- SYMFONY_VERSION='3.1.*'
Expand Down
28 changes: 25 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,33 @@ Changelog
2.0.0 (unreleased)
------------------

* [User Context] Added an option always_vary_on_context_hash to make it
possible to disable automatically setting the vary headers for the user
* [Proxy Client Configuration] The configuration for the proxy client has been
adjusted. Proxy servers are now configured under `http` and `servers` must be
a list - a comma separated string of server IPs is no longer supported.

* [User Context] Added an option always_vary_on_context_hash to make it
possible to disable automatically setting the vary headers for the user
hash.

* [Event Listeners] Renamed the event listener classes to XxxLlistener
* [Event Listeners] Renamed the event listener classes to XxxLlistener.

* Updated the version of FOSHttpCache to 2.*. See [FOSHttpCache changelog]
(https://github.com/FriendsOfSymfony/FOSHttpCache/blob/master/CHANGELOG.md).
Most important, there is no more hard coupling on Guzzle HTTP client. We now
use the HTTPlug HTTP client abstraction. Your composer.json now needs to
specify which HTTP client to install, see [installation instructions]
(http://foshttpcachebundle.readthedocs.org/en/stable/installation.html)

* [Tags] The TagHandler has been split. Invalidating tags happens through the
CacheManager (if you use annotations for tag invalidation, you don't need to
change anything). Recording tags and writing them into the responses is done
through the SymfonyResponseTagger now.

* [Test] Dropped the proxy client services as they where not used anywhere. The
services `fos_http_cache.test.client.varnish` and `fos_http_cache.test.client.nginx`
no longer exist.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ddeboer can you agree with this? i feel that with httplug and discovery, its easy enough to do this now on your own in tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!


* Deprecated methods have been removed.

1.3.7
-----
Expand Down
46 changes: 6 additions & 40 deletions Command/InvalidateTagCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use FOS\HttpCache\Handler\TagHandler;

/**
* A command to trigger cache invalidation by tag from the command line.
Expand All @@ -26,39 +25,18 @@
class InvalidateTagCommand extends ContainerAwareCommand
{
/**
* @var TagHandler
* @var CacheManager
*/
private $tagHandler;
private $cacheManager;

/**
* @var string
*/
private $commandName;

/**
* If no cache manager is specified explicitly, fos_http_cache.cache_manager
* is automatically loaded.
*
* Passing CacheManager as argument is deprecated and will be restricted to TagHandler in 2.0.
*
* @param TagHandler|CacheManager|null $tagHandler The tag handler to talk to
* @param string $commandName Name of this command, in case you want to reuse it
*/
public function __construct($tagHandler = null, $commandName = 'fos:httpcache:invalidate:tag')
public function __construct(CacheManager $cacheManager)
{
if (!($tagHandler instanceof TagHandler || $tagHandler instanceof CacheManager || null === $tagHandler)) {
throw new \InvalidArgumentException(
sprintf(
'Expected instance of TagHandler, CacheManager or null, but got %s',
get_class($tagHandler)
)
);
}
if ($tagHandler instanceof CacheManager) {
@trigger_error('Passing the CacheManager to '.__CLASS__.' is deprecated since version 1.2 and will be removed in 2.0. Provide the TagHandler instead.', E_USER_DEPRECATED);
}
$this->commandName = $commandName;
$this->tagHandler = $tagHandler;
$this->cacheManager = $cacheManager;
parent::__construct();
}

Expand All @@ -68,7 +46,7 @@ public function __construct($tagHandler = null, $commandName = 'fos:httpcache:in
protected function configure()
{
$this
->setName($this->commandName)
->setName('fos:httpcache:invalidate:tag')
->setDescription('Invalidate cached content matching the specified tags on all configured caching proxies')
->addArgument(
'tags',
Expand All @@ -93,18 +71,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$tags = $input->getArgument('tags');

$this->getTagManager()->invalidateTags($tags);
}

/**
* @return TagHandler|CacheManager
*/
protected function getTagManager()
{
if (!$this->tagHandler) {
$this->tagHandler = $this->getContainer()->get('fos_http_cache.handler.tag_handler');
}

return $this->tagHandler;
$this->cacheManager->invalidateTags($tags);
}
}
11 changes: 5 additions & 6 deletions DependencyInjection/Compiler/LoggerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Attach Symfony2 logger to cache manager.
* Announce the log listener to the symfony event system.
*/
class LoggerPass implements CompilerPassInterface
{
Expand All @@ -28,10 +28,9 @@ public function process(ContainerBuilder $container)
return;
}

$logListener = $container->getDefinition('fos_http_cache.event_listener.log')
->setAbstract(false);

$container->getDefinition('fos_http_cache.cache_manager')
->addMethodCall('addSubscriber', array($logListener));
$container->getDefinition('fos_http_cache.event_listener.log')
->setAbstract(false)
->addTag('kernel.event_subscriber')
;
}
}
161 changes: 41 additions & 120 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
Expand Down Expand Up @@ -93,36 +94,6 @@ public function getConfigTreeBuilder()
throw new InvalidConfigurationException('You need to configure a proxy_client to get the cache_manager needed for invalidation handling.');
})
->end()
->validate()
->ifTrue(function ($v) {
return isset($v['test'])
&& $v['test']['client']['varnish']['enabled']
&& !isset($v['proxy_client']['varnish']);
})
->then(function ($v) {
if ('auto' === $v['test']['client']['varnish']['enabled']) {
$v['test']['client']['varnish']['enabled'] = false;

return $v;
}
throw new InvalidConfigurationException('You need to configure the Varnish proxy_client to use the Varnish test client');
})
->end()
->validate()
->ifTrue(function ($v) {
if (isset($v['test'])) {
return $v['test']['client']['nginx']['enabled'] && !isset($v['proxy_client']['nginx']);
}
})
->then(function ($v) {
if ('auto' === $v['test']['client']['nginx']['enabled']) {
$v['test']['client']['nginx']['enabled'] = false;

return $v;
}
throw new InvalidConfigurationException('You need to configure the Nginx proxy_client to use the Nginx test client');
})
->end()
->validate()
->ifTrue(
function ($v) {
Expand Down Expand Up @@ -314,81 +285,27 @@ private function addProxyClientSection(ArrayNodeDefinition $rootNode)
->children()
->enumNode('default')
->values(array('varnish', 'nginx', 'symfony'))
->info('If you configure more than one proxy client, specify which client is the default.')
->info('If you configure more than one proxy client, you need to specify which client is the default.')
->end()
->arrayNode('varnish')
->fixXmlConfig('server')
->children()
->arrayNode('servers')
->beforeNormalization()->ifString()->then(function ($v) {
return preg_split('/\s*,\s*/', $v);
})->end()
->useAttributeAsKey('name')
->isRequired()
->requiresAtLeastOneElement()
->prototype('scalar')->end()
->info('Addresses of the hosts Varnish is running on. May be hostname or ip, and with :port if not the default port 80.')
->end()
->scalarNode('base_url')
->defaultNull()
->info('Default host name and optional path for path based invalidation.')
->end()
->scalarNode('guzzle_client')
->defaultNull()
->info('Guzzle service to use for customizing the requests.')
->end()
->append($this->getHttpDispatcherNode())
->end()
->end()

->arrayNode('nginx')
->fixXmlConfig('server')
->children()
->arrayNode('servers')
->beforeNormalization()->ifString()->then(function ($v) {
return preg_split('/\s*,\s*/', $v);
})->end()
->useAttributeAsKey('name')
->isRequired()
->requiresAtLeastOneElement()
->prototype('scalar')->end()
->info('Addresses of the hosts Nginx is running on. May be hostname or ip, and with :port if not the default port 80.')
->end()
->scalarNode('base_url')
->defaultNull()
->info('Default host name and optional path for path based invalidation.')
->end()
->scalarNode('guzzle_client')
->defaultNull()
->info('Guzzle service to use for customizing the requests.')
->end()
->scalarNode('purge_location')
->defaultValue('')
->defaultValue(false)
->info('Path to trigger the purge on Nginx for different location purge.')
->end()
->append($this->getHttpDispatcherNode())
->end()
->end()

->arrayNode('symfony')
->fixXmlConfig('server')
->children()
->arrayNode('servers')
->beforeNormalization()->ifString()->then(function ($v) {
return preg_split('/\s*,\s*/', $v);
})->end()
->useAttributeAsKey('name')
->isRequired()
->requiresAtLeastOneElement()
->prototype('scalar')->end()
->info('Addresses of the hosts Symfony HttpCache is running on. May be hostname or ip, and with :port if not the default port 80.')
->end()
->scalarNode('base_url')
->defaultNull()
->info('Default host name and optional path for path based invalidation.')
->end()
->scalarNode('guzzle_client')
->defaultNull()
->info('Guzzle service to use for customizing the requests.')
->end()
->append($this->getHttpDispatcherNode())
->end()
->end()

Expand All @@ -397,6 +314,41 @@ private function addProxyClientSection(ArrayNodeDefinition $rootNode)
->end();
}

/**
* Get the configuration node for a HTTP dispatcher in a proxy client.
*
* @return NodeDefinition
*/
private function getHttpDispatcherNode()
{
$treeBuilder = new TreeBuilder();
$node = $treeBuilder->root('http');

$node
->fixXmlConfig('server')
->isRequired()
->children()
->arrayNode('servers')
->info('Addresses of the hosts the caching proxy is running on. May be hostname or ip, and with :port if not the default port 80.')
->useAttributeAsKey('name')
->isRequired()
->requiresAtLeastOneElement()
->prototype('scalar')->end()
->end()
->scalarNode('base_url')
->defaultNull()
->info('Default host name and optional path for path based invalidation.')
->end()
->scalarNode('http_client')
->defaultNull()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need any reference to the httplug-bundle. by default, we pass no client to HttpDispatcher, which will prompt that to do a discovery. if discovery is not desired, a client can be configured.

->info('Httplug async client service name to use for sending the requests.')
->end()
->end()
;

return $node;
}

private function addTestSection(ArrayNodeDefinition $rootNode)
{
$rootNode
Expand Down Expand Up @@ -432,37 +384,6 @@ private function addTestSection(ArrayNodeDefinition $rootNode)
->end()
->end()
->end()
->arrayNode('client')
->addDefaultsIfNotSet()
->children()
->enumNode('default')
->values(array('varnish', 'nginx'))
->info('If you configure more than one proxy client, specify which client is the default.')
->end()
->arrayNode('varnish')
->addDefaultsIfNotSet()
->canBeEnabled()
->children()
->enumNode('enabled')
->values(array(true, false, 'auto'))
->defaultValue('auto')
->info('Whether to enable the Varnish test client.')
->end()
->end()
->end()
->arrayNode('nginx')
->addDefaultsIfNotSet()
->canBeEnabled()
->children()
->enumNode('enabled')
->values(array(true, false, 'auto'))
->defaultValue('auto')
->info('Whether to enable the Nginx test client.')
->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end();
Expand Down
Loading