| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | namespace Civi\Core\Lock; |
| 28 | |
| 29 | use Civi\Core\Resolver; |
| 30 | |
| 31 | /** |
| 32 | * Class LockManager |
| 33 | * @package Civi\Core\Lock |
| 34 | * |
| 35 | * The lock-manager allows one to define the lock policy -- i.e. given a |
| 36 | * specific lock, how does one acquire the lock? |
| 37 | */ |
| 38 | class LockManager { |
| 39 | |
| 40 | private $rules = array(); |
| 41 | |
| 42 | /** |
| 43 | * @param string $name |
| 44 | * Symbolic name for the lock. Names generally look like |
| 45 | * "worker.mailing.EmailProcessor" ("{category}.{component}.{AdhocName}"). |
| 46 | * |
| 47 | * Categories: worker|data|cache|... |
| 48 | * Component: core|mailing|member|contribute|... |
| 49 | * @return LockInterface |
| 50 | * @throws \CRM_Core_Exception |
| 51 | */ |
| 52 | public function create($name) { |
| 53 | $factory = $this->getFactory($name); |
| 54 | if ($factory) { |
| 55 | /** @var LockInterface $lock */ |
| 56 | $lock = call_user_func_array($factory, array($name)); |
| 57 | return $lock; |
| 58 | } |
| 59 | else { |
| 60 | throw new \CRM_Core_Exception("Lock \"$name\" does not match any rules. Use register() to add more rules."); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Create and attempt to acquire a lock. |
| 66 | * |
| 67 | * Note: Be sure to check $lock->isAcquired() to determine whether |
| 68 | * acquisition was successful. |
| 69 | * |
| 70 | * @param string $name |
| 71 | * Symbolic name for the lock. Names generally look like |
| 72 | * "worker.mailing.EmailProcessor" ("{category}.{component}.{AdhocName}"). |
| 73 | * |
| 74 | * Categories: worker|data|cache|... |
| 75 | * Component: core|mailing|member|contribute|... |
| 76 | * @param int|NULL $timeout |
| 77 | * The number of seconds to wait to get the lock. |
| 78 | * For a default value, use NULL. |
| 79 | * @return LockInterface |
| 80 | * @throws \CRM_Core_Exception |
| 81 | */ |
| 82 | public function acquire($name, $timeout = NULL) { |
| 83 | $lock = $this->create($name); |
| 84 | $lock->acquire($timeout); |
| 85 | return $lock; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param string $name |
| 90 | * Symbolic name for the lock. |
| 91 | * @return callable|NULL |
| 92 | */ |
| 93 | public function getFactory($name) { |
| 94 | foreach ($this->rules as $rule) { |
| 95 | if (preg_match($rule['pattern'], $name)) { |
| 96 | return Resolver::singleton()->get($rule['factory']); |
| 97 | } |
| 98 | } |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Register the lock-factory to use for specific lock-names. |
| 104 | * |
| 105 | * @param string $pattern |
| 106 | * A regex to match against the lock name. |
| 107 | * @param string|array $factory |
| 108 | * A callback. The callback should accept a $name parameter. |
| 109 | * Callbacks will be located using the resolver. |
| 110 | * @return $this |
| 111 | * @see Resolver |
| 112 | */ |
| 113 | public function register($pattern, $factory) { |
| 114 | $this->rules[] = array( |
| 115 | 'pattern' => $pattern, |
| 116 | 'factory' => $factory, |
| 117 | ); |
| 118 | return $this; |
| 119 | } |
| 120 | |
| 121 | } |