@@ -9,19 +9,19 @@ |
||
| 9 | 9 | interface LoaderDecoratorInterface |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * @param string $fqcn |
|
| 14 | - * @param array $arguments |
|
| 15 | - * @return mixed |
|
| 16 | - */ |
|
| 17 | - public function load($fqcn, $arguments = array()); |
|
| 12 | + /** |
|
| 13 | + * @param string $fqcn |
|
| 14 | + * @param array $arguments |
|
| 15 | + * @return mixed |
|
| 16 | + */ |
|
| 17 | + public function load($fqcn, $arguments = array()); |
|
| 18 | 18 | |
| 19 | 19 | |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * calls reset() on loader if method exists |
|
| 23 | - */ |
|
| 24 | - public function reset(); |
|
| 21 | + /** |
|
| 22 | + * calls reset() on loader if method exists |
|
| 23 | + */ |
|
| 24 | + public function reset(); |
|
| 25 | 25 | |
| 26 | 26 | } |
| 27 | 27 | // End of file LoaderInterface.php |
@@ -9,39 +9,39 @@ |
||
| 9 | 9 | interface LoaderInterface |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * Can be for instantiating a new instance of a class, |
|
| 14 | - * or for getting a shared instance of a class (default) |
|
| 15 | - * |
|
| 16 | - * @param string $fqcn |
|
| 17 | - * @param array $arguments |
|
| 18 | - * @param bool $shared |
|
| 19 | - * @return mixed |
|
| 20 | - */ |
|
| 21 | - public function load($fqcn, $arguments = array(), $shared = true); |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Used for instantiating a new instance of a class |
|
| 25 | - * |
|
| 26 | - * @param string $fqcn |
|
| 27 | - * @param array $arguments |
|
| 28 | - * @return mixed |
|
| 29 | - */ |
|
| 30 | - public function getNew($fqcn, $arguments = array()); |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Used for getting a shared instance of a class |
|
| 34 | - * |
|
| 35 | - * @param string $fqcn |
|
| 36 | - * @param array $arguments |
|
| 37 | - * @return mixed |
|
| 38 | - */ |
|
| 39 | - public function getShared($fqcn, $arguments = array()); |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * calls reset() on loader if method exists |
|
| 43 | - */ |
|
| 44 | - public function reset(); |
|
| 12 | + /** |
|
| 13 | + * Can be for instantiating a new instance of a class, |
|
| 14 | + * or for getting a shared instance of a class (default) |
|
| 15 | + * |
|
| 16 | + * @param string $fqcn |
|
| 17 | + * @param array $arguments |
|
| 18 | + * @param bool $shared |
|
| 19 | + * @return mixed |
|
| 20 | + */ |
|
| 21 | + public function load($fqcn, $arguments = array(), $shared = true); |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Used for instantiating a new instance of a class |
|
| 25 | + * |
|
| 26 | + * @param string $fqcn |
|
| 27 | + * @param array $arguments |
|
| 28 | + * @return mixed |
|
| 29 | + */ |
|
| 30 | + public function getNew($fqcn, $arguments = array()); |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Used for getting a shared instance of a class |
|
| 34 | + * |
|
| 35 | + * @param string $fqcn |
|
| 36 | + * @param array $arguments |
|
| 37 | + * @return mixed |
|
| 38 | + */ |
|
| 39 | + public function getShared($fqcn, $arguments = array()); |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * calls reset() on loader if method exists |
|
| 43 | + */ |
|
| 44 | + public function reset(); |
|
| 45 | 45 | |
| 46 | 46 | } |
| 47 | 47 | // End of file LoaderInterface.php |
@@ -22,114 +22,114 @@ |
||
| 22 | 22 | class CachingLoader extends LoaderDecorator |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var CollectionInterface $cache |
|
| 27 | - */ |
|
| 28 | - protected $cache; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var string $identifier |
|
| 32 | - */ |
|
| 33 | - protected $identifier; |
|
| 34 | - |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * CachingLoader constructor. |
|
| 39 | - * |
|
| 40 | - * @param LoaderDecoratorInterface $loader |
|
| 41 | - * @param CollectionInterface $cache |
|
| 42 | - * @param string $identifier |
|
| 43 | - * @throws InvalidDataTypeException |
|
| 44 | - */ |
|
| 45 | - public function __construct(LoaderDecoratorInterface $loader, CollectionInterface $cache, $identifier = '') |
|
| 46 | - { |
|
| 47 | - parent::__construct($loader); |
|
| 48 | - $this->cache = $cache; |
|
| 49 | - $this->setIdentifier($identifier); |
|
| 50 | - if ($this->identifier !== '') { |
|
| 51 | - // to only clear this cache, and assuming an identifier has been set, simply do the following: |
|
| 52 | - // do_action('AHEE__EventEspresso\core\services\loaders\CachingLoader__resetCache__IDENTIFIER'); |
|
| 53 | - // where "IDENTIFIER" = the string that was set during construction |
|
| 54 | - add_action( |
|
| 55 | - "AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__{$identifier}", |
|
| 56 | - array($this, 'reset') |
|
| 57 | - ); |
|
| 58 | - } |
|
| 59 | - // to clear ALL caches, simply do the following: |
|
| 60 | - // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache'); |
|
| 61 | - add_action( |
|
| 62 | - 'AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache', |
|
| 63 | - array($this, 'reset') |
|
| 64 | - ); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - public function identifier() |
|
| 73 | - { |
|
| 74 | - return $this->identifier; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @param string $identifier |
|
| 81 | - * @throws InvalidDataTypeException |
|
| 82 | - */ |
|
| 83 | - private function setIdentifier($identifier) |
|
| 84 | - { |
|
| 85 | - if ( ! is_string($identifier)) { |
|
| 86 | - throw new InvalidDataTypeException('$identifier', $identifier, 'string'); |
|
| 87 | - } |
|
| 88 | - $this->identifier = $identifier; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @param string $fqcn |
|
| 95 | - * @param array $arguments |
|
| 96 | - * @return mixed |
|
| 97 | - * @throws InvalidEntityException |
|
| 98 | - * @throws ServiceNotFoundException |
|
| 99 | - */ |
|
| 100 | - public function load($fqcn, $arguments = array()) |
|
| 101 | - { |
|
| 102 | - $fqcn = ltrim($fqcn, '\\'); |
|
| 103 | - // caching can be turned off via the following code: |
|
| 104 | - // add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true'); |
|
| 105 | - if( |
|
| 106 | - apply_filters( |
|
| 107 | - 'FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', |
|
| 108 | - false, |
|
| 109 | - $this |
|
| 110 | - ) |
|
| 111 | - ){ |
|
| 112 | - return $this->loader->load($fqcn, $arguments); |
|
| 113 | - } |
|
| 114 | - $identifier = md5($fqcn . serialize($arguments)); |
|
| 115 | - if($this->cache->has($identifier)){ |
|
| 116 | - return $this->cache->get($identifier); |
|
| 117 | - } |
|
| 118 | - $object = $this->loader->load($fqcn, $arguments); |
|
| 119 | - $this->cache->add($object, $identifier); |
|
| 120 | - return $object; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * empties cache and calls reset() on loader if method exists |
|
| 127 | - */ |
|
| 128 | - public function reset() |
|
| 129 | - { |
|
| 130 | - $this->cache->detachAll(); |
|
| 131 | - $this->loader->reset(); |
|
| 132 | - } |
|
| 25 | + /** |
|
| 26 | + * @var CollectionInterface $cache |
|
| 27 | + */ |
|
| 28 | + protected $cache; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var string $identifier |
|
| 32 | + */ |
|
| 33 | + protected $identifier; |
|
| 34 | + |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * CachingLoader constructor. |
|
| 39 | + * |
|
| 40 | + * @param LoaderDecoratorInterface $loader |
|
| 41 | + * @param CollectionInterface $cache |
|
| 42 | + * @param string $identifier |
|
| 43 | + * @throws InvalidDataTypeException |
|
| 44 | + */ |
|
| 45 | + public function __construct(LoaderDecoratorInterface $loader, CollectionInterface $cache, $identifier = '') |
|
| 46 | + { |
|
| 47 | + parent::__construct($loader); |
|
| 48 | + $this->cache = $cache; |
|
| 49 | + $this->setIdentifier($identifier); |
|
| 50 | + if ($this->identifier !== '') { |
|
| 51 | + // to only clear this cache, and assuming an identifier has been set, simply do the following: |
|
| 52 | + // do_action('AHEE__EventEspresso\core\services\loaders\CachingLoader__resetCache__IDENTIFIER'); |
|
| 53 | + // where "IDENTIFIER" = the string that was set during construction |
|
| 54 | + add_action( |
|
| 55 | + "AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__{$identifier}", |
|
| 56 | + array($this, 'reset') |
|
| 57 | + ); |
|
| 58 | + } |
|
| 59 | + // to clear ALL caches, simply do the following: |
|
| 60 | + // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache'); |
|
| 61 | + add_action( |
|
| 62 | + 'AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache', |
|
| 63 | + array($this, 'reset') |
|
| 64 | + ); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + public function identifier() |
|
| 73 | + { |
|
| 74 | + return $this->identifier; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @param string $identifier |
|
| 81 | + * @throws InvalidDataTypeException |
|
| 82 | + */ |
|
| 83 | + private function setIdentifier($identifier) |
|
| 84 | + { |
|
| 85 | + if ( ! is_string($identifier)) { |
|
| 86 | + throw new InvalidDataTypeException('$identifier', $identifier, 'string'); |
|
| 87 | + } |
|
| 88 | + $this->identifier = $identifier; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @param string $fqcn |
|
| 95 | + * @param array $arguments |
|
| 96 | + * @return mixed |
|
| 97 | + * @throws InvalidEntityException |
|
| 98 | + * @throws ServiceNotFoundException |
|
| 99 | + */ |
|
| 100 | + public function load($fqcn, $arguments = array()) |
|
| 101 | + { |
|
| 102 | + $fqcn = ltrim($fqcn, '\\'); |
|
| 103 | + // caching can be turned off via the following code: |
|
| 104 | + // add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true'); |
|
| 105 | + if( |
|
| 106 | + apply_filters( |
|
| 107 | + 'FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', |
|
| 108 | + false, |
|
| 109 | + $this |
|
| 110 | + ) |
|
| 111 | + ){ |
|
| 112 | + return $this->loader->load($fqcn, $arguments); |
|
| 113 | + } |
|
| 114 | + $identifier = md5($fqcn . serialize($arguments)); |
|
| 115 | + if($this->cache->has($identifier)){ |
|
| 116 | + return $this->cache->get($identifier); |
|
| 117 | + } |
|
| 118 | + $object = $this->loader->load($fqcn, $arguments); |
|
| 119 | + $this->cache->add($object, $identifier); |
|
| 120 | + return $object; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * empties cache and calls reset() on loader if method exists |
|
| 127 | + */ |
|
| 128 | + public function reset() |
|
| 129 | + { |
|
| 130 | + $this->cache->detachAll(); |
|
| 131 | + $this->loader->reset(); |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | 134 | |
| 135 | 135 | } |
@@ -19,22 +19,22 @@ |
||
| 19 | 19 | { |
| 20 | 20 | |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @var LoaderDecoratorInterface $loader |
|
| 24 | - */ |
|
| 25 | - protected $loader; |
|
| 26 | - |
|
| 27 | - |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * LoaderDecorator constructor. |
|
| 31 | - * |
|
| 32 | - * @param LoaderDecoratorInterface $loader |
|
| 33 | - */ |
|
| 34 | - public function __construct(LoaderDecoratorInterface $loader) |
|
| 35 | - { |
|
| 36 | - $this->loader = $loader; |
|
| 37 | - } |
|
| 22 | + /** |
|
| 23 | + * @var LoaderDecoratorInterface $loader |
|
| 24 | + */ |
|
| 25 | + protected $loader; |
|
| 26 | + |
|
| 27 | + |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * LoaderDecorator constructor. |
|
| 31 | + * |
|
| 32 | + * @param LoaderDecoratorInterface $loader |
|
| 33 | + */ |
|
| 34 | + public function __construct(LoaderDecoratorInterface $loader) |
|
| 35 | + { |
|
| 36 | + $this->loader = $loader; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | 39 | |
| 40 | 40 | |
@@ -23,58 +23,58 @@ |
||
| 23 | 23 | class CoreLoader implements LoaderDecoratorInterface |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @var EE_Registry|CoffeeShop $generator |
|
| 28 | - */ |
|
| 29 | - private $generator; |
|
| 30 | - |
|
| 31 | - |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * CoreLoader constructor. |
|
| 35 | - * |
|
| 36 | - * @param EE_Registry|CoffeeShop $generator |
|
| 37 | - * @throws InvalidArgumentException |
|
| 38 | - */ |
|
| 39 | - public function __construct($generator) |
|
| 40 | - { |
|
| 41 | - if(!($generator instanceof EE_Registry || $generator instanceof CoffeeShop)) { |
|
| 42 | - throw new InvalidArgumentException( |
|
| 43 | - esc_html__( |
|
| 44 | - 'The CoreLoader class must receive an instance of EE_Registry or the CoffeeShop DI container.', |
|
| 45 | - 'event_espresso' |
|
| 46 | - ) |
|
| 47 | - ); |
|
| 48 | - } |
|
| 49 | - $this->generator = $generator; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @param string $fqcn |
|
| 56 | - * @param array $arguments |
|
| 57 | - * @return mixed |
|
| 58 | - * @throws ServiceNotFoundException |
|
| 59 | - */ |
|
| 60 | - public function load($fqcn, $arguments = array()) |
|
| 61 | - { |
|
| 62 | - return $this->generator instanceof EE_Registry |
|
| 63 | - ? $this->generator->create($fqcn, $arguments) |
|
| 64 | - : $this->generator->brew($fqcn, $arguments); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * calls reset() on generator if method exists |
|
| 71 | - */ |
|
| 72 | - public function reset() |
|
| 73 | - { |
|
| 74 | - if (method_exists($this->generator, 'reset')) { |
|
| 75 | - $this->generator->reset(); |
|
| 76 | - } |
|
| 77 | - } |
|
| 26 | + /** |
|
| 27 | + * @var EE_Registry|CoffeeShop $generator |
|
| 28 | + */ |
|
| 29 | + private $generator; |
|
| 30 | + |
|
| 31 | + |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * CoreLoader constructor. |
|
| 35 | + * |
|
| 36 | + * @param EE_Registry|CoffeeShop $generator |
|
| 37 | + * @throws InvalidArgumentException |
|
| 38 | + */ |
|
| 39 | + public function __construct($generator) |
|
| 40 | + { |
|
| 41 | + if(!($generator instanceof EE_Registry || $generator instanceof CoffeeShop)) { |
|
| 42 | + throw new InvalidArgumentException( |
|
| 43 | + esc_html__( |
|
| 44 | + 'The CoreLoader class must receive an instance of EE_Registry or the CoffeeShop DI container.', |
|
| 45 | + 'event_espresso' |
|
| 46 | + ) |
|
| 47 | + ); |
|
| 48 | + } |
|
| 49 | + $this->generator = $generator; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @param string $fqcn |
|
| 56 | + * @param array $arguments |
|
| 57 | + * @return mixed |
|
| 58 | + * @throws ServiceNotFoundException |
|
| 59 | + */ |
|
| 60 | + public function load($fqcn, $arguments = array()) |
|
| 61 | + { |
|
| 62 | + return $this->generator instanceof EE_Registry |
|
| 63 | + ? $this->generator->create($fqcn, $arguments) |
|
| 64 | + : $this->generator->brew($fqcn, $arguments); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * calls reset() on generator if method exists |
|
| 71 | + */ |
|
| 72 | + public function reset() |
|
| 73 | + { |
|
| 74 | + if (method_exists($this->generator, 'reset')) { |
|
| 75 | + $this->generator->reset(); |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | 79 | } |
| 80 | 80 | // End of file CoreLoader.php |