TYPO3 CMS  TYPO3_7-6
LockFactory.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Locking;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
24 {
28  protected $lockingStrategy = [
29  SemaphoreLockStrategy::class => true,
30  FileLockStrategy::class => true,
31  SimpleLockStrategy::class => true
32  ];
33 
40  public function addLockingStrategy($className)
41  {
42  $interfaces = class_implements($className);
43  if (isset($interfaces[LockingStrategyInterface::class])) {
44  $this->lockingStrategy[$className] = true;
45  } else {
46  throw new \InvalidArgumentException('The given class name ' . $className . ' does not implement the required LockingStrategyInterface interface.', 1425990198);
47  }
48  }
49 
55  public function removeLockingStrategy($className)
56  {
57  unset($this->lockingStrategy[$className]);
58  }
59 
68  public function createLocker($id, $capabilities = LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE)
69  {
70  $queue = new \SplPriorityQueue();
71 
73  foreach ($this->lockingStrategy as $method => $_) {
74  $supportedCapabilities = $capabilities & $method::getCapabilities();
75  if ($supportedCapabilities === $capabilities) {
76  $queue->insert($method, $method::getPriority());
77  }
78  }
79  if ($queue->count() > 0) {
80  $className = $queue->top();
81  // We use 'new' here on purpose!
82  // Locking might be used very early in the bootstrap process, where makeInstance() does not work
83  return new $className($id);
84  }
85  throw new LockCreateException('Could not find a matching locking method with requested capabilities.', 1425990190);
86  }
87 }