Issue #3038040 by claudiu.cristea, bradjones1, swentel, bvoynick, longwave, grayle, catch, timwood, alexpott: Support priority for needs_destruction service tag
parent
43732e22b6
commit
25ad53d3df
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Drupal\Core\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
|
||||
|
@ -14,12 +15,14 @@ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
|||
*/
|
||||
class RegisterServicesForDestructionPass implements CompilerPassInterface {
|
||||
|
||||
use PriorityTaggedServiceTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function process(ContainerBuilder $container) {
|
||||
$services = $container->findTaggedServiceIds('needs_destruction');
|
||||
$container->setParameter('kernel.destructable_services', array_keys($services));
|
||||
$service_ids = array_values(array_map('strval', $this->findAndSortTaggedServices('needs_destruction', $container)));
|
||||
$container->setParameter('kernel.destructable_services', $service_ids);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,3 +6,28 @@ services:
|
|||
tags:
|
||||
- { name: needs_destruction }
|
||||
arguments: ['@state']
|
||||
# Used to test destructable service order.
|
||||
service_provider_test_class_1:
|
||||
parent: service_provider_test_class
|
||||
tags:
|
||||
- { name: needs_destruction, priority: 50 }
|
||||
service_provider_test_class_2:
|
||||
parent: service_provider_test_class
|
||||
tags:
|
||||
- { name: needs_destruction, priority: -10 }
|
||||
service_provider_test_class_3:
|
||||
parent: service_provider_test_class
|
||||
tags:
|
||||
- { name: needs_destruction, priority: 0 }
|
||||
service_provider_test_class_4:
|
||||
parent: service_provider_test_class
|
||||
tags:
|
||||
- { name: needs_destruction, priority: -100 }
|
||||
service_provider_test_class_5:
|
||||
parent: service_provider_test_class
|
||||
tags:
|
||||
- { name: needs_destruction, priority: 100 }
|
||||
service_provider_test_class_6:
|
||||
parent: service_provider_test_class
|
||||
tags:
|
||||
- { name: needs_destruction, priority: -50 }
|
||||
|
|
|
@ -122,14 +122,16 @@ class StandardPerformanceTest extends PerformanceTestBase {
|
|||
'DELETE FROM "semaphore" WHERE ("name" = "library_info:stark:Drupal\Core\Cache\CacheCollector") AND ("value" = "LOCK_ID")',
|
||||
'INSERT INTO "semaphore" ("name", "value", "expire") VALUES ("path_alias_whitelist:Drupal\Core\Cache\CacheCollector", "LOCK_ID", "EXPIRE")',
|
||||
'DELETE FROM "semaphore" WHERE ("name" = "path_alias_whitelist:Drupal\Core\Cache\CacheCollector") AND ("value" = "LOCK_ID")',
|
||||
'INSERT INTO "semaphore" ("name", "value", "expire") VALUES ("active-trail:route:view.frontpage.page_1:route_parameters:a:2:{s:10:"display_id";s:6:"page_1";s:7:"view_id";s:9:"frontpage";}:Drupal\Core\Cache\CacheCollector", "LOCK_ID", "EXPIRE")',
|
||||
'DELETE FROM "semaphore" WHERE ("name" = "active-trail:route:view.frontpage.page_1:route_parameters:a:2:{s:10:"display_id";s:6:"page_1";s:7:"view_id";s:9:"frontpage";}:Drupal\Core\Cache\CacheCollector") AND ("value" = "LOCK_ID")',
|
||||
];
|
||||
$recorded_queries = $performance_data->getQueries();
|
||||
$this->assertSame($expected_queries, $recorded_queries);
|
||||
$this->assertSame(34, $performance_data->getQueryCount());
|
||||
$this->assertSame(124, $performance_data->getCacheGetCount());
|
||||
$this->assertSame(45, $performance_data->getCacheSetCount());
|
||||
$this->assertSame(36, $performance_data->getQueryCount());
|
||||
$this->assertSame(125, $performance_data->getCacheGetCount());
|
||||
$this->assertSame(46, $performance_data->getCacheSetCount());
|
||||
$this->assertSame(0, $performance_data->getCacheDeleteCount());
|
||||
$this->assertSame(36, $performance_data->getCacheTagChecksumCount());
|
||||
$this->assertSame(37, $performance_data->getCacheTagChecksumCount());
|
||||
$this->assertSame(43, $performance_data->getCacheTagIsValidCount());
|
||||
$this->assertSame(0, $performance_data->getCacheTagInvalidationCount());
|
||||
|
||||
|
@ -150,14 +152,16 @@ class StandardPerformanceTest extends PerformanceTestBase {
|
|||
'SELECT "menu_tree"."menu_name" AS "menu_name", "menu_tree"."route_name" AS "route_name", "menu_tree"."route_parameters" AS "route_parameters", "menu_tree"."url" AS "url", "menu_tree"."title" AS "title", "menu_tree"."description" AS "description", "menu_tree"."parent" AS "parent", "menu_tree"."weight" AS "weight", "menu_tree"."options" AS "options", "menu_tree"."expanded" AS "expanded", "menu_tree"."enabled" AS "enabled", "menu_tree"."provider" AS "provider", "menu_tree"."metadata" AS "metadata", "menu_tree"."class" AS "class", "menu_tree"."form_class" AS "form_class", "menu_tree"."id" AS "id" FROM "menu_tree" "menu_tree" WHERE ("route_name" = "entity.node.canonical") AND ("route_param_key" = "node=1") AND ("menu_name" = "account") ORDER BY "depth" ASC, "weight" ASC, "id" ASC',
|
||||
'INSERT INTO "semaphore" ("name", "value", "expire") VALUES ("theme_registry:runtime:stark:Drupal\Core\Utility\ThemeRegistry", "LOCK_ID", "EXPIRE")',
|
||||
'DELETE FROM "semaphore" WHERE ("name" = "theme_registry:runtime:stark:Drupal\Core\Utility\ThemeRegistry") AND ("value" = "LOCK_ID")',
|
||||
'INSERT INTO "semaphore" ("name", "value", "expire") VALUES ("active-trail:route:entity.node.canonical:route_parameters:a:1:{s:4:"node";s:1:"1";}:Drupal\Core\Cache\CacheCollector", "LOCK_ID", "EXPIRE")',
|
||||
'DELETE FROM "semaphore" WHERE ("name" = "active-trail:route:entity.node.canonical:route_parameters:a:1:{s:4:"node";s:1:"1";}:Drupal\Core\Cache\CacheCollector") AND ("value" = "LOCK_ID")',
|
||||
];
|
||||
$recorded_queries = $performance_data->getQueries();
|
||||
$this->assertSame($expected_queries, $recorded_queries);
|
||||
$this->assertSame(8, $performance_data->getQueryCount());
|
||||
$this->assertSame(94, $performance_data->getCacheGetCount());
|
||||
$this->assertSame(16, $performance_data->getCacheSetCount());
|
||||
$this->assertSame(10, $performance_data->getQueryCount());
|
||||
$this->assertSame(95, $performance_data->getCacheGetCount());
|
||||
$this->assertSame(17, $performance_data->getCacheSetCount());
|
||||
$this->assertSame(0, $performance_data->getCacheDeleteCount());
|
||||
$this->assertCountBetween(23, 24, $performance_data->getCacheTagChecksumCount());
|
||||
$this->assertCountBetween(24, 25, $performance_data->getCacheTagChecksumCount());
|
||||
$this->assertCountBetween(39, 40, $performance_data->getCacheTagIsValidCount());
|
||||
$this->assertSame(0, $performance_data->getCacheTagInvalidationCount());
|
||||
|
||||
|
@ -183,14 +187,16 @@ class StandardPerformanceTest extends PerformanceTestBase {
|
|||
'SELECT "menu_tree"."menu_name" AS "menu_name", "menu_tree"."route_name" AS "route_name", "menu_tree"."route_parameters" AS "route_parameters", "menu_tree"."url" AS "url", "menu_tree"."title" AS "title", "menu_tree"."description" AS "description", "menu_tree"."parent" AS "parent", "menu_tree"."weight" AS "weight", "menu_tree"."options" AS "options", "menu_tree"."expanded" AS "expanded", "menu_tree"."enabled" AS "enabled", "menu_tree"."provider" AS "provider", "menu_tree"."metadata" AS "metadata", "menu_tree"."class" AS "class", "menu_tree"."form_class" AS "form_class", "menu_tree"."id" AS "id" FROM "menu_tree" "menu_tree" WHERE ("route_name" = "entity.user.canonical") AND ("route_param_key" = "user=2") AND ("menu_name" = "account") ORDER BY "depth" ASC, "weight" ASC, "id" ASC',
|
||||
'SELECT "ud".* FROM "users_data" "ud" WHERE ("module" = "contact") AND ("uid" = "2") AND ("name" = "enabled")',
|
||||
'SELECT "name", "data" FROM "config" WHERE "collection" = "" AND "name" IN ( "contact.settings" )',
|
||||
'INSERT INTO "semaphore" ("name", "value", "expire") VALUES ("active-trail:route:entity.user.canonical:route_parameters:a:1:{s:4:"user";s:1:"2";}:Drupal\Core\Cache\CacheCollector", "LOCK_ID", "EXPIRE")',
|
||||
'DELETE FROM "semaphore" WHERE ("name" = "active-trail:route:entity.user.canonical:route_parameters:a:1:{s:4:"user";s:1:"2";}:Drupal\Core\Cache\CacheCollector") AND ("value" = "LOCK_ID")',
|
||||
];
|
||||
$recorded_queries = $performance_data->getQueries();
|
||||
$this->assertSame($expected_queries, $recorded_queries);
|
||||
$this->assertSame(12, $performance_data->getQueryCount());
|
||||
$this->assertSame(78, $performance_data->getCacheGetCount());
|
||||
$this->assertSame(16, $performance_data->getCacheSetCount());
|
||||
$this->assertSame(14, $performance_data->getQueryCount());
|
||||
$this->assertSame(79, $performance_data->getCacheGetCount());
|
||||
$this->assertSame(17, $performance_data->getCacheSetCount());
|
||||
$this->assertSame(0, $performance_data->getCacheDeleteCount());
|
||||
$this->assertSame(22, $performance_data->getCacheTagChecksumCount());
|
||||
$this->assertSame(23, $performance_data->getCacheTagChecksumCount());
|
||||
$this->assertSame(32, $performance_data->getCacheTagIsValidCount());
|
||||
$this->assertSame(0, $performance_data->getCacheTagInvalidationCount());
|
||||
}
|
||||
|
|
|
@ -57,4 +57,34 @@ class ServiceDestructionTest extends KernelTestBase {
|
|||
$this->assertNull(\Drupal::state()->get('service_provider_test.destructed'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Drupal\Core\DependencyInjection\Compiler\RegisterServicesForDestructionPass::process
|
||||
*/
|
||||
public function testDestructableServicesOrder(): void {
|
||||
// Destructable services before the module is enabled.
|
||||
$core_services = $this->container->getParameter('kernel.destructable_services');
|
||||
|
||||
$this->enableModules(['service_provider_test']);
|
||||
$services = $this->container->getParameter('kernel.destructable_services');
|
||||
// Remove the core registered services for clarity.
|
||||
$testable_services = array_values(array_diff($services, $core_services));
|
||||
|
||||
$this->assertSame([
|
||||
// Priority 100.
|
||||
'service_provider_test_class_5',
|
||||
// Priority 50.
|
||||
'service_provider_test_class_1',
|
||||
// The following two services are both with priority 0 and their order is
|
||||
// determined by the order they were registered.
|
||||
'service_provider_test_class',
|
||||
'service_provider_test_class_3',
|
||||
// Priority -10.
|
||||
'service_provider_test_class_2',
|
||||
// Priority -50.
|
||||
'service_provider_test_class_6',
|
||||
// Priority -100.
|
||||
'service_provider_test_class_4',
|
||||
], $testable_services);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue