ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Pimple\Container Class Reference

Container main class. More...

+ Inheritance diagram for Pimple\Container:
+ Collaboration diagram for Pimple\Container:

Public Member Functions

 __construct (array $values=array())
 Instantiate the container. More...
 
 offsetSet ($id, $value)
 Sets a parameter or an object. More...
 
 offsetGet ($id)
 Gets a parameter or an object. More...
 
 offsetExists ($id)
 Checks if a parameter or an object is set. More...
 
 offsetUnset ($id)
 Unsets a parameter or an object. More...
 
 factory ($callable)
 Marks a callable as being a factory service. More...
 
 protect ($callable)
 Protects a callable from being interpreted as a service. More...
 
 raw ($id)
 Gets a parameter or the closure defining an object. More...
 
 extend ($id, $callable)
 Extends an object definition. More...
 
 keys ()
 Returns all defined value names. More...
 
 register (ServiceProviderInterface $provider, array $values=array())
 Registers a service provider. More...
 

Private Attributes

 $values = array()
 
 $factories
 
 $protected
 
 $frozen = array()
 
 $raw = array()
 
 $keys = array()
 

Detailed Description

Container main class.

Author
Fabien Potencier

Definition at line 34 of file Container.php.

Constructor & Destructor Documentation

◆ __construct()

Pimple\Container::__construct ( array  $values = array())

Instantiate the container.

Objects and parameters can be passed as argument to the constructor.

Parameters
array$valuesThe parameters or objects.

Reimplemented in Slim\Container.

Definition at line 50 of file Container.php.

51 {
52 $this->factories = new \SplObjectStorage();
53 $this->protected = new \SplObjectStorage();
54
55 foreach ($values as $key => $value) {
56 $this->offsetSet($key, $value);
57 }
58 }
offsetSet($id, $value)
Sets a parameter or an object.
Definition: Container.php:74
$key
Definition: croninfo.php:18

References $key.

Member Function Documentation

◆ extend()

Pimple\Container::extend (   $id,
  $callable 
)

Extends an object definition.

Useful when you want to extend an existing object definition, without necessarily loading that object.

Parameters
string$idThe unique identifier for the object
callable$callableA service definition to extend the original
Returns
callable The wrapped callable
Exceptions

InvalidArgumentException if the identifier is not defined or not a service definition

Definition at line 226 of file Container.php.

227 {
228 if (!isset($this->keys[$id])) {
229 throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
230 }
231
232 if (!is_object($this->values[$id]) || !method_exists($this->values[$id], '__invoke')) {
233 throw new \InvalidArgumentException(sprintf('Identifier "%s" does not contain an object definition.', $id));
234 }
235
236 if (!is_object($callable) || !method_exists($callable, '__invoke')) {
237 throw new \InvalidArgumentException('Extension service definition is not a Closure or invokable object.');
238 }
239
240 $factory = $this->values[$id];
241
242 $extended = function ($c) use ($callable, $factory) {
243 return $callable($factory($c), $c);
244 };
245
246 if (isset($this->factories[$factory])) {
247 $this->factories->detach($factory);
248 $this->factories->attach($extended);
249 }
250
251 return $this[$id] = $extended;
252 }
sprintf('%.4f', $callTime)
$factory
Definition: metadata.php:47
keys()
Returns all defined value names.
Definition: Container.php:259
if(!array_key_exists('StateId', $_REQUEST)) $id

References $factory, $id, and sprintf.

◆ factory()

Pimple\Container::factory (   $callable)

Marks a callable as being a factory service.

Parameters
callable$callableA service definition to be used as a factory
Returns
callable The passed callable
Exceptions

InvalidArgumentException Service definition has to be a closure of an invokable object

Definition at line 158 of file Container.php.

159 {
160 if (!method_exists($callable, '__invoke')) {
161 throw new \InvalidArgumentException('Service definition is not a Closure or invokable object.');
162 }
163
164 $this->factories->attach($callable);
165
166 return $callable;
167 }

◆ keys()

Pimple\Container::keys ( )

Returns all defined value names.

Returns
array An array of value names

Definition at line 259 of file Container.php.

260 {
261 return array_keys($this->values);
262 }

◆ offsetExists()

Pimple\Container::offsetExists (   $id)

Checks if a parameter or an object is set.

Parameters
string$idThe unique identifier for the parameter or object
Returns
bool

Definition at line 128 of file Container.php.

129 {
130 return isset($this->keys[$id]);
131 }

References $id.

◆ offsetGet()

Pimple\Container::offsetGet (   $id)

Gets a parameter or an object.

Parameters
string$idThe unique identifier for the parameter or object
Returns
mixed The value of the parameter or an object
Exceptions

InvalidArgumentException if the identifier is not defined

Definition at line 93 of file Container.php.

94 {
95 if (!isset($this->keys[$id])) {
96 throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
97 }
98
99 if (
100 isset($this->raw[$id])
101 || !is_object($this->values[$id])
102 || isset($this->protected[$this->values[$id]])
103 || !method_exists($this->values[$id], '__invoke')
104 ) {
105 return $this->values[$id];
106 }
107
108 if (isset($this->factories[$this->values[$id]])) {
109 return $this->values[$id]($this);
110 }
111
112 $raw = $this->values[$id];
113 $val = $this->values[$id] = $raw($this);
114 $this->raw[$id] = $raw;
115
116 $this->frozen[$id] = true;
117
118 return $val;
119 }
raw($id)
Gets a parameter or the closure defining an object.
Definition: Container.php:200

References $id, and sprintf.

◆ offsetSet()

Pimple\Container::offsetSet (   $id,
  $value 
)

Sets a parameter or an object.

Objects must be defined as Closures.

Allowing any PHP callable leads to difficult to debug problems as function names (strings) are callable (creating a function with the same name as an existing parameter would break your container).

Parameters
string$idThe unique identifier for the parameter or object
mixed$valueThe value of the parameter or a closure to define an object
Exceptions

RuntimeException Prevent override of a frozen service

Definition at line 74 of file Container.php.

75 {
76 if (isset($this->frozen[$id])) {
77 throw new \RuntimeException(sprintf('Cannot override frozen service "%s".', $id));
78 }
79
80 $this->values[$id] = $value;
81 $this->keys[$id] = true;
82 }

References $id, and sprintf.

◆ offsetUnset()

Pimple\Container::offsetUnset (   $id)

Unsets a parameter or an object.

Parameters
string$idThe unique identifier for the parameter or object

Definition at line 138 of file Container.php.

139 {
140 if (isset($this->keys[$id])) {
141 if (is_object($this->values[$id])) {
142 unset($this->factories[$this->values[$id]], $this->protected[$this->values[$id]]);
143 }
144
145 unset($this->values[$id], $this->frozen[$id], $this->raw[$id], $this->keys[$id]);
146 }
147 }

References $id.

◆ protect()

Pimple\Container::protect (   $callable)

Protects a callable from being interpreted as a service.

This is useful when you want to store a callable as a parameter.

Parameters
callable$callableA callable to protect from being evaluated
Returns
callable The passed callable
Exceptions

InvalidArgumentException Service definition has to be a closure of an invokable object

Definition at line 180 of file Container.php.

181 {
182 if (!method_exists($callable, '__invoke')) {
183 throw new \InvalidArgumentException('Callable is not a Closure or invokable object.');
184 }
185
186 $this->protected->attach($callable);
187
188 return $callable;
189 }

◆ raw()

Pimple\Container::raw (   $id)

Gets a parameter or the closure defining an object.

Parameters
string$idThe unique identifier for the parameter or object
Returns
mixed The value of the parameter or the closure defining an object
Exceptions

InvalidArgumentException if the identifier is not defined

Definition at line 200 of file Container.php.

201 {
202 if (!isset($this->keys[$id])) {
203 throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
204 }
205
206 if (isset($this->raw[$id])) {
207 return $this->raw[$id];
208 }
209
210 return $this->values[$id];
211 }

References $id, and sprintf.

◆ register()

Pimple\Container::register ( ServiceProviderInterface  $provider,
array  $values = array() 
)

Registers a service provider.

Parameters
ServiceProviderInterface$providerA ServiceProviderInterface instance
array$valuesAn array of values that customizes the provider
Returns
static

Definition at line 272 of file Container.php.

273 {
274 $provider->register($this);
275
276 foreach ($values as $key => $value) {
277 $this[$key] = $value;
278 }
279
280 return $this;
281 }

References $key, and League\OAuth2\Client\Provider\$provider.

Field Documentation

◆ $factories

Pimple\Container::$factories
private

Definition at line 37 of file Container.php.

◆ $frozen

Pimple\Container::$frozen = array()
private

Definition at line 39 of file Container.php.

◆ $keys

Pimple\Container::$keys = array()
private

Definition at line 41 of file Container.php.

◆ $protected

Pimple\Container::$protected
private

Definition at line 38 of file Container.php.

◆ $raw

Pimple\Container::$raw = array()
private

Definition at line 40 of file Container.php.

◆ $values

Pimple\Container::$values = array()
private

Definition at line 36 of file Container.php.


The documentation for this class was generated from the following file: