/usr/share/php/Pimple.php is in php-pimple 1.1.1-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | <?php
/*
* This file is part of Pimple.
*
* Copyright (c) 2009 Fabien Potencier
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Pimple main class.
*
* @package pimple
* @author Fabien Potencier
*/
class Pimple implements ArrayAccess
{
protected $values = array();
/**
* Instantiate the container.
*
* Objects and parameters can be passed as argument to the constructor.
*
* @param array $values The parameters or objects.
*/
public function __construct(array $values = array())
{
$this->values = $values;
}
/**
* 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 a name as an existing parameter would break your container).
*
* @param string $id The unique identifier for the parameter or object
* @param mixed $value The value of the parameter or a closure to defined an object
*/
public function offsetSet($id, $value)
{
$this->values[$id] = $value;
}
/**
* Gets a parameter or an object.
*
* @param string $id The unique identifier for the parameter or object
*
* @return mixed The value of the parameter or an object
*
* @throws InvalidArgumentException if the identifier is not defined
*/
public function offsetGet($id)
{
if (!array_key_exists($id, $this->values)) {
throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
}
$isFactory = is_object($this->values[$id]) && method_exists($this->values[$id], '__invoke');
return $isFactory ? $this->values[$id]($this) : $this->values[$id];
}
/**
* Checks if a parameter or an object is set.
*
* @param string $id The unique identifier for the parameter or object
*
* @return Boolean
*/
public function offsetExists($id)
{
return array_key_exists($id, $this->values);
}
/**
* Unsets a parameter or an object.
*
* @param string $id The unique identifier for the parameter or object
*/
public function offsetUnset($id)
{
unset($this->values[$id]);
}
/**
* Returns a closure that stores the result of the given service definition
* for uniqueness in the scope of this instance of Pimple.
*
* @param callable $callable A service definition to wrap for uniqueness
*
* @return Closure The wrapped closure
*/
public static function share($callable)
{
if (!is_object($callable) || !method_exists($callable, '__invoke')) {
throw new InvalidArgumentException('Service definition is not a Closure or invokable object.');
}
return function ($c) use ($callable) {
static $object;
if (null === $object) {
$object = $callable($c);
}
return $object;
};
}
/**
* Protects a callable from being interpreted as a service.
*
* This is useful when you want to store a callable as a parameter.
*
* @param callable $callable A callable to protect from being evaluated
*
* @return Closure The protected closure
*/
public static function protect($callable)
{
if (!is_object($callable) || !method_exists($callable, '__invoke')) {
throw new InvalidArgumentException('Callable is not a Closure or invokable object.');
}
return function ($c) use ($callable) {
return $callable;
};
}
/**
* Gets a parameter or the closure defining an object.
*
* @param string $id The unique identifier for the parameter or object
*
* @return mixed The value of the parameter or the closure defining an object
*
* @throws InvalidArgumentException if the identifier is not defined
*/
public function raw($id)
{
if (!array_key_exists($id, $this->values)) {
throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
}
return $this->values[$id];
}
/**
* Extends an object definition.
*
* Useful when you want to extend an existing object definition,
* without necessarily loading that object.
*
* @param string $id The unique identifier for the object
* @param callable $callable A service definition to extend the original
*
* @return Closure The wrapped closure
*
* @throws InvalidArgumentException if the identifier is not defined or not a service definition
*/
public function extend($id, $callable)
{
if (!array_key_exists($id, $this->values)) {
throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
}
if (!is_object($this->values[$id]) || !method_exists($this->values[$id], '__invoke')) {
throw new InvalidArgumentException(sprintf('Identifier "%s" does not contain an object definition.', $id));
}
if (!is_object($callable) || !method_exists($callable, '__invoke')) {
throw new InvalidArgumentException('Extension service definition is not a Closure or invokable object.');
}
$factory = $this->values[$id];
return $this->values[$id] = function ($c) use ($callable, $factory) {
return $callable($factory($c), $c);
};
}
/**
* Returns all defined value names.
*
* @return array An array of value names
*/
public function keys()
{
return array_keys($this->values);
}
}
|