Skip to content

Commit

Permalink
IBX-8470: Upgraded codebase to Symfony 6 (#54)
Browse files Browse the repository at this point in the history
* [Composer] Dropped ci-scripts from dev requirements
* [Composer] Bumped Symfony packages requirements to ^6.4
* [Composer] Bumped 3rd party packages to Symfony 6 compatible version
* Aligned Symfony Bundle extension points code with Symfony 6
* Aligned Symfony HttpCache extension point code with Symfony 6
* Aligned Twig extension points code with Symfony 6
* Fixed DI prepend configuration
* Improved codebase quality
* Dropped handling obsolete service IDs from KernelPass
* [Tests] Dropped obsolete KernelPassSpec
* [Tests] Aligned TagHandlerSpec with Symfony 6
* [Tests] Fixed incorrect ParameterBag being used in HttpCacheResponseSubscriberSpec
* Disabled tag by annotations as they are dependent on deprecated sensio framework extra bundle

---------

Co-authored-by: Dawid Parafinski <dawid.parafinski@ibexa.co>
  • Loading branch information
alongosz and ViniTou authored Feb 4, 2025
1 parent 0190130 commit d3cab7b
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 198 deletions.
19 changes: 9 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,22 @@
"ibexa/rest": "~5.0.x-dev",
"php-http/curl-client": "^2.1",
"psr/http-client": "^1.0",
"symfony/config": "^5.0",
"symfony/dependency-injection": "^5.0",
"symfony/event-dispatcher": "^5.0",
"symfony/http-foundation": "^5.0",
"symfony/http-kernel": "^5.0",
"symfony/routing": "^5.0",
"symfony/yaml": "^5.0",
"toflar/psr6-symfony-http-cache-store": "^2.2 || ^3.0"
"symfony/config": "^6.4",
"symfony/dependency-injection": "^6.4",
"symfony/event-dispatcher": "^6.4",
"symfony/http-foundation": "^6.4",
"symfony/http-kernel": "^6.4",
"symfony/routing": "^6.4",
"symfony/yaml": "^6.4",
"toflar/psr6-symfony-http-cache-store": "^4.2"
},
"require-dev": {
"ibexa/ci-scripts": "^0.2@dev",
"ibexa/code-style": "~2.0.0",
"ibexa/doctrine-schema": "~5.0.x-dev",
"matthiasnoback/symfony-dependency-injection-test": "^4.3",
"phpspec/phpspec": "^7.1",
"phpunit/phpunit": "^9.6",
"symfony/phpunit-bridge": "^5.1"
"symfony/phpunit-bridge": "^6.4"
},
"autoload": {
"psr-4": {
Expand Down
62 changes: 0 additions & 62 deletions spec/DependencyInjection/Compiler/KernelPassSpec.php

This file was deleted.

2 changes: 1 addition & 1 deletion spec/EventSubscriber/HttpCacheResponseSubscriberSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Ibexa\HttpCache\ResponseConfigurator\ResponseCacheConfigurator;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
Expand Down
2 changes: 1 addition & 1 deletion spec/Handler/TagHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function it_ignores_too_long_tag_header_and_reduces_ttl(Response $respons
$length += $tagLength;
}
$responseHeaderBag->getCacheControlDirective('s-maxage')->shouldBeCalled()->willReturn(500);
$response->setSharedMaxAge(300)->shouldBeCalled();
$response->setSharedMaxAge(300)->shouldBeCalled()->willReturn($response);
$responseHeaderBag->set('xkey', Argument::exact($underLimitTags))->shouldBeCalled();

$this->addTags(explode(' ', $underLimitTags));
Expand Down
24 changes: 7 additions & 17 deletions src/bundle/AppCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Toflar\Psr6HttpCacheStore\Psr6Store;
Expand All @@ -39,26 +40,20 @@ public function __construct(KernelInterface $kernel, $cacheDir = null)
$this->addSubscriber(new PurgeListener(['client_ips' => $this->getInternalAllowedIPs()]));
}

public function fetch(Request $request, $catch = false)
public function fetch(Request $request, $catch = false): Response
{
return parent::fetch($request, $catch);
}

/**
* {@inheritdoc}
*/
protected function createStore()
protected function createStore(): StoreInterface
{
return new Psr6Store([
'cache_tags_header' => TagHeaderFormatter::DEFAULT_HEADER_NAME,
'cache_directory' => $this->cacheDir ?: $this->kernel->getCacheDir() . '/http_cache',
]);
}

/**
* {@inheritdoc}
*/
public function handle(Request $request, $type = HttpKernelInterface::MAIN_REQUEST, $catch = true)
public function handle(Request $request, $type = HttpKernelInterface::MAIN_REQUEST, $catch = true): Response
{
$response = $this->baseHandle($request, $type, $catch);

Expand All @@ -72,19 +67,14 @@ public function handle(Request $request, $type = HttpKernelInterface::MAIN_REQUE
/**
* Returns an array of allowed IPs for Http PURGE requests.
*
* @return array
* @return string[]
*/
protected function getInternalAllowedIPs()
protected function getInternalAllowedIPs(): array
{
return ['127.0.0.1', '::1'];
}

/**
* Perform cleanup of reponse.
*
* @param \Symfony\Component\HttpFoundation\Response $response
*/
protected function cleanupHeadersForProd(Response $response)
protected function cleanupHeadersForProd(Response $response): void
{
// remove headers that identify the content or internal digest info
$response->headers->remove(TagHeaderFormatter::DEFAULT_HEADER_NAME);
Expand Down
13 changes: 5 additions & 8 deletions src/bundle/DependencyInjection/Compiler/DriverPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class DriverPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
$container->removeAlias('ezpublish.http_cache.purge_client');

Expand All @@ -42,24 +42,21 @@ public function process(ContainerBuilder $container)
$container->setAlias(ContentTagInterface::class, 'fos_http_cache.http.symfony_response_tagger');
}

public static function getTaggedService(ContainerBuilder $container, $tag)
public static function getTaggedService(ContainerBuilder $container, $tag): string|null
{
$purgeType = $container->getParameter('ibexa.http_cache.purge_type');
$configuredTagHandlerServiceId = null;

$tagHandlerServiceIds = $container->findTaggedServiceIds($tag);
foreach ($tagHandlerServiceIds as $tagHandlerServiceId => $attributes) {
$currentPurgeTypeId = null;
$currentTagHandlerServiceId = null;
foreach ($attributes as $attribute) {
if (\array_key_exists('purge_type', $attribute)) {
$currentPurgeTypeId = $attribute['purge_type'];
}
if ($currentPurgeTypeId !== null) {
if ($purgeType === $attribute['purge_type']) {
$configuredTagHandlerServiceId = $tagHandlerServiceId;
break 2;
}
if (($currentPurgeTypeId !== null) && $purgeType === $attribute['purge_type']) {
$configuredTagHandlerServiceId = $tagHandlerServiceId;
break 2;
}
}
if ($currentPurgeTypeId === null) {
Expand Down
73 changes: 2 additions & 71 deletions src/bundle/DependencyInjection/Compiler/KernelPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,87 +9,18 @@

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* Disables some of the http-cache services declared by the kernel so that
* they can be replaced with this bundle's.
*/
class KernelPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
foreach ($container->getDefinitions() as $id => $definition) {
if ($this->isSmartCacheListener($id) ||
$this->isResponseCacheListener($id) ||
$this->isCachePurger($id)
) {
$container->removeDefinition($id);
}
}

if ($container->hasAlias('ezpublish.http_cache.purger')) {
$container->removeAlias('ezpublish.http_cache.purger');
}

$this->removeKernelRoleIdContextProvider($container);
$container->removeDefinition('ezpublish.view.cache_response_listener');

// Let's re-export purge_type setting so that driver's don't have to depend on kernel in order to acquire it
$container->setParameter('ibexa.http_cache.purge_type', $container->getParameter('ibexa.http_cache.purge_type'));
}

/**
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
*/
protected function removeKernelRoleIdContextProvider(ContainerBuilder $container)
{
if (!$container->hasDefinition('ezpublish.user.identity_definer.role_id')) {
return;
}

// As we set role identify ourselves here we remove varant from kernel if it is there.
// We don't touch ezpublish.user.hash_generator, as it's deprecated extension point by kernel
$container->removeDefinition('ezpublish.user.identity_definer.role_id');

// Also remove from arguments already passed to FOSHttpCache via compiler pass there.
$arguments = $container->getDefinition('fos_http_cache.user_context.hash_generator')->getArguments();
$arguments[0] = array_values(array_filter($arguments[0], static function (Reference $argument) {
if ((string)$argument === 'ezpublish.user.identity_definer.role_id') {
return false;
}

return true;
}));
$container->getDefinition('fos_http_cache.user_context.hash_generator')->setArguments($arguments);
}

/**
* @param string $id
*
* @return bool
*/
protected function isSmartCacheListener($id)
{
return preg_match('/^ezpublish\.cache_clear\.content.[a-z_]+_listener/', $id);
}

/**
* @param string $id
*
* @return bool
*/
protected function isResponseCacheListener($id)
{
return $id === 'ezpublish.view.cache_response_listener';
}

/**
* @param string $id
*
* @return bool
*/
protected function isCachePurger($id)
{
return strpos($id, 'ezpublish.http_cache.purger.') === 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
class ResponseTaggersPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition(DispatcherTagger::class)) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/bundle/DependencyInjection/Compiler/VarnishCachePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

class VarnishCachePass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
$this->processVarnishProxyClientSettings($container);
}

private function processVarnishProxyClientSettings(ContainerBuilder $container)
private function processVarnishProxyClientSettings(ContainerBuilder $container): void
{
if (!$container->hasDefinition('fos_http_cache.proxy_client.varnish')) {
throw new InvalidArgumentException('Varnish proxy client must be enabled in FOSHttpCacheBundle');
Expand Down
4 changes: 2 additions & 2 deletions src/bundle/DependencyInjection/IbexaHttpCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ public function prepend(ContainerBuilder $container)
$loader->load('default_settings.yml');

// Override default settings for FOSHttpCacheBundle
$configFile = __DIR__ . '/../Resources/config/fos_http_cache.yml';
$configFile = __DIR__ . '/../Resources/config/prepend/fos_http_cache.yml';
$config = Yaml::parse(file_get_contents($configFile));
$container->prependExtensionConfig('fos_http_cache', $config);
$container->addResource(new FileResource($configFile));

// Override Core views
$coreExtensionConfigFile = realpath(__DIR__ . '/../Resources/config/prepend/ezpublish.yml');
$coreExtensionConfigFile = dirname(__DIR__) . '/Resources/config/prepend/ibexa.yml';
$container->prependExtensionConfig('ibexa', Yaml::parseFile($coreExtensionConfigFile));
$container->addResource(new FileResource($coreExtensionConfigFile));
}
Expand Down
6 changes: 3 additions & 3 deletions src/bundle/IbexaHttpCacheBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class IbexaHttpCacheBundle extends Bundle
{
public function build(ContainerBuilder $container)
public function build(ContainerBuilder $container): void
{
parent::build($container);

Expand All @@ -31,7 +31,7 @@ public function build(ContainerBuilder $container)
$this->registerConfigParser($container);
}

public function getContainerExtensionClass()
public function getContainerExtensionClass(): string
{
return IbexaHttpCacheExtension::class;
}
Expand All @@ -56,7 +56,7 @@ public function getContainerExtension(): ?ExtensionInterface
}
}

public function registerConfigParser(ContainerBuilder $container)
public function registerConfigParser(ContainerBuilder $container): void
{
/** @var \Ibexa\Bundle\Core\DependencyInjection\IbexaCoreExtension $eZExtension */
$eZExtension = $container->getExtension('ibexa');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ user_context:
hash_cache_ttl: 600
# NOTE: These are also defined/used in AppCache, in Varnish VCL, and Fastly VCL
session_name_prefix: IBX_SESSION_ID

tags:
annotations:
enabled: false
Loading

0 comments on commit d3cab7b

Please sign in to comment.