[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Part of the Joomla Framework DI Package 4 * 5 * @copyright Copyright (C) 2013 - 2018 Open Source Matters, Inc. All rights reserved. 6 * @license GNU General Public License version 2 or later; see LICENSE 7 */ 8 9 namespace Joomla\DI; 10 11 /** 12 * Defines the representation of a resource. 13 * 14 * @since 2.0.0 15 * @internal 16 */ 17 final class ContainerResource 18 { 19 /** 20 * Defines the resource as non-shared 21 * 22 * @const integer 23 * @since 2.0.0 24 */ 25 public const NO_SHARE = 0; 26 27 /** 28 * Defines the resource as shared 29 * 30 * @const integer 31 * @since 2.0.0 32 */ 33 public const SHARE = 1; 34 35 /** 36 * Defines the resource as non-protected 37 * 38 * @const integer 39 * @since 2.0.0 40 */ 41 public const NO_PROTECT = 0; 42 43 /** 44 * Defines the resource as protected 45 * 46 * @const integer 47 * @since 2.0.0 48 */ 49 public const PROTECT = 2; 50 51 /** 52 * The container the resource is assigned to 53 * 54 * @var Container 55 * @since 2.0.0 56 */ 57 private $container; 58 59 /** 60 * The object instance for a shared object 61 * 62 * @var mixed 63 * @since 2.0.0 64 */ 65 private $instance; 66 67 /** 68 * The factory object 69 * 70 * @var callable 71 * @since 2.0.0 72 */ 73 private $factory; 74 75 /** 76 * Flag if the resource is shared 77 * 78 * @var boolean 79 * @since 2.0.0 80 */ 81 private $shared = false; 82 83 /** 84 * Flag if the resource is protected 85 * 86 * @var boolean 87 * @since 2.0.0 88 */ 89 private $protected = false; 90 91 /** 92 * Create a resource representation 93 * 94 * @param Container $container The container 95 * @param mixed $value The resource or its factory closure 96 * @param integer $mode Resource mode, defaults to Resource::NO_SHARE | Resource::NO_PROTECT 97 * 98 * @since 2.0.0 99 */ 100 public function __construct(Container $container, $value, int $mode = 0) 101 { 102 $this->container = $container; 103 $this->shared = ($mode & self::SHARE) === self::SHARE; 104 $this->protected = ($mode & self::PROTECT) === self::PROTECT; 105 106 if (\is_callable($value)) 107 { 108 $this->factory = $value; 109 } 110 else 111 { 112 if ($this->shared) 113 { 114 $this->instance = $value; 115 } 116 117 if (\is_object($value)) 118 { 119 $this->factory = function () use ($value) 120 { 121 return clone $value; 122 }; 123 } 124 else 125 { 126 $this->factory = function () use ($value) 127 { 128 return $value; 129 }; 130 } 131 } 132 } 133 134 /** 135 * Check whether the resource is shared 136 * 137 * @return boolean 138 * 139 * @since 2.0.0 140 */ 141 public function isShared(): bool 142 { 143 return $this->shared; 144 } 145 146 /** 147 * Check whether the resource is protected 148 * 149 * @return boolean 150 * 151 * @since 2.0.0 152 */ 153 public function isProtected(): bool 154 { 155 return $this->protected; 156 } 157 158 /** 159 * Get an instance of the resource 160 * 161 * If a factory was provided, the resource is created and - if it is a shared resource - cached internally. 162 * If the resource was provided directly, that resource is returned. 163 * 164 * @return mixed 165 * 166 * @since 2.0.0 167 */ 168 public function getInstance() 169 { 170 $callable = $this->factory; 171 172 if ($this->isShared()) 173 { 174 if ($this->instance === null) 175 { 176 $this->instance = $callable($this->container); 177 } 178 179 return $this->instance; 180 } 181 182 return $callable($this->container); 183 } 184 185 /** 186 * Get the factory 187 * 188 * @return callable 189 * 190 * @since 2.0.0 191 */ 192 public function getFactory(): callable 193 { 194 return $this->factory; 195 } 196 197 /** 198 * Reset the resource 199 * 200 * The instance cache is cleared, so that the next call to get() returns a new instance. 201 * This has an effect on shared, non-protected resources only. 202 * 203 * @return boolean True if the resource was reset, false otherwise 204 * 205 * @since 2.0.0 206 */ 207 public function reset(): bool 208 { 209 if ($this->isShared() && !$this->isProtected()) 210 { 211 $this->instance = null; 212 213 return true; 214 } 215 216 return false; 217 } 218 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |