|
38 | 38 | use Symfony\Component\DependencyInjection\ContainerInterface;
|
39 | 39 | use Symfony\Component\DependencyInjection\Definition;
|
40 | 40 | use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
|
| 41 | +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
41 | 42 | use Symfony\Component\DependencyInjection\Exception\LogicException;
|
42 | 43 | use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
43 | 44 | use Symfony\Component\DependencyInjection\Reference;
|
|
54 | 55 | use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
|
55 | 56 | use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
|
56 | 57 | use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
| 58 | +use Symfony\Component\Lock\Factory; |
| 59 | +use Symfony\Component\Lock\Lock; |
| 60 | +use Symfony\Component\Lock\LockInterface; |
| 61 | +use Symfony\Component\Lock\Store\StoreFactory; |
| 62 | +use Symfony\Component\Lock\StoreInterface; |
57 | 63 | use Symfony\Component\PropertyAccess\PropertyAccessor;
|
58 | 64 | use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
|
59 | 65 | use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
|
@@ -298,6 +304,10 @@ public function load(array $configs, ContainerBuilder $container)
|
298 | 304 | $this->registerPropertyInfoConfiguration($config['property_info'], $container, $loader);
|
299 | 305 | }
|
300 | 306 |
|
| 307 | + if ($this->isConfigEnabled($container, $config['lock'])) { |
| 308 | + $this->registerLockConfiguration($config['lock'], $container, $loader); |
| 309 | + } |
| 310 | + |
301 | 311 | if ($this->isConfigEnabled($container, $config['web_link'])) {
|
302 | 312 | if (!class_exists(HttpHeaderSerializer::class)) {
|
303 | 313 | throw new LogicException('WebLink support cannot be enabled as the WebLink component is not installed.');
|
@@ -1672,6 +1682,84 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild
|
1672 | 1682 | }
|
1673 | 1683 | }
|
1674 | 1684 |
|
| 1685 | + private function registerLockConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader) |
| 1686 | + { |
| 1687 | + $loader->load('lock.xml'); |
| 1688 | + |
| 1689 | + foreach ($config['resources'] as $resourceName => $resourceStores) { |
| 1690 | + if (0 === count($resourceStores)) { |
| 1691 | + continue; |
| 1692 | + } |
| 1693 | + |
| 1694 | + // Generate stores |
| 1695 | + $storeDefinitions = array(); |
| 1696 | + foreach ($resourceStores as $storeDsn) { |
| 1697 | + $storeDsn = $container->resolveEnvPlaceholders($storeDsn, null, $usedEnvs); |
| 1698 | + switch (true) { |
| 1699 | + case 'flock' === $storeDsn: |
| 1700 | + $storeDefinition = new Reference('lock.store.flock'); |
| 1701 | + break; |
| 1702 | + case 'semaphore' === $storeDsn: |
| 1703 | + $storeDefinition = new Reference('lock.store.semaphore'); |
| 1704 | + break; |
| 1705 | + case $usedEnvs || preg_match('#^[a-z]++://#', $storeDsn): |
| 1706 | + if (!$container->hasDefinition($connectionDefinitionId = $container->hash($storeDsn))) { |
| 1707 | + $connectionDefinition = new Definition(\stdClass::class); |
| 1708 | + $connectionDefinition->setPublic(false); |
| 1709 | + $connectionDefinition->setFactory(array(StoreFactory::class, 'createConnection')); |
| 1710 | + $connectionDefinition->setArguments(array($storeDsn)); |
| 1711 | + $container->setDefinition($connectionDefinitionId, $connectionDefinition); |
| 1712 | + } |
| 1713 | + |
| 1714 | + $storeDefinition = new Definition(StoreInterface::class); |
| 1715 | + $storeDefinition->setPublic(false); |
| 1716 | + $storeDefinition->setFactory(array(StoreFactory::class, 'createStore')); |
| 1717 | + $storeDefinition->setArguments(array(new Reference($connectionDefinitionId))); |
| 1718 | + |
| 1719 | + $container->setDefinition($storeDefinitionId = 'lock.'.$resourceName.'.store.'.$container->hash($storeDsn), $storeDefinition); |
| 1720 | + |
| 1721 | + $storeDefinition = new Reference($storeDefinitionId); |
| 1722 | + break; |
| 1723 | + default: |
| 1724 | + throw new InvalidArgumentException(sprintf('Lock store DSN "%s" is not valid in resource "%s"', $storeDsn, $resourceName)); |
| 1725 | + } |
| 1726 | + |
| 1727 | + $storeDefinitions[] = $storeDefinition; |
| 1728 | + } |
| 1729 | + |
| 1730 | + // Wrap array of stores with CombinedStore |
| 1731 | + if (count($storeDefinitions) > 1) { |
| 1732 | + $combinedDefinition = new ChildDefinition('lock.store.combined.abstract'); |
| 1733 | + $combinedDefinition->replaceArgument(0, $storeDefinitions); |
| 1734 | + $container->setDefinition('lock.'.$resourceName.'.store', $combinedDefinition); |
| 1735 | + } else { |
| 1736 | + $container->setAlias('lock.'.$resourceName.'.store', new Alias((string) $storeDefinitions[0], false)); |
| 1737 | + } |
| 1738 | + |
| 1739 | + // Generate factories for each resource |
| 1740 | + $factoryDefinition = new ChildDefinition('lock.factory.abstract'); |
| 1741 | + $factoryDefinition->replaceArgument(0, new Reference('lock.'.$resourceName.'.store')); |
| 1742 | + $container->setDefinition('lock.'.$resourceName.'.factory', $factoryDefinition); |
| 1743 | + |
| 1744 | + // Generate services for lock instances |
| 1745 | + $lockDefinition = new Definition(Lock::class); |
| 1746 | + $lockDefinition->setPublic(false); |
| 1747 | + $lockDefinition->setFactory(array(new Reference('lock.'.$resourceName.'.factory'), 'createLock')); |
| 1748 | + $lockDefinition->setArguments(array($resourceName)); |
| 1749 | + $container->setDefinition('lock.'.$resourceName, $lockDefinition); |
| 1750 | + |
| 1751 | + // provide alias for default resource |
| 1752 | + if ('default' === $resourceName) { |
| 1753 | + $container->setAlias('lock.store', new Alias('lock.'.$resourceName.'.store', false)); |
| 1754 | + $container->setAlias('lock.factory', new Alias('lock.'.$resourceName.'.factory', false)); |
| 1755 | + $container->setAlias('lock', new Alias('lock.'.$resourceName, false)); |
| 1756 | + $container->setAlias(StoreInterface::class, new Alias('lock.store', false)); |
| 1757 | + $container->setAlias(Factory::class, new Alias('lock.factory', false)); |
| 1758 | + $container->setAlias(LockInterface::class, new Alias('lock', false)); |
| 1759 | + } |
| 1760 | + } |
| 1761 | + } |
| 1762 | + |
1675 | 1763 | private function registerCacheConfiguration(array $config, ContainerBuilder $container)
|
1676 | 1764 | {
|
1677 | 1765 | $version = substr(str_replace('/', '-', base64_encode(hash('sha256', uniqid(mt_rand(), true), true))), 0, 22);
|
|
0 commit comments