~pvigo/+junk/owncloud-14.04

« back to all changes in this revision

Viewing changes to share/owncloud/3rdparty/Pimple/Pimple.php

  • Committer: Pablo Vigo
  • Date: 2014-12-15 13:36:46 UTC
  • Revision ID: pvigo@xtec.cat-20141215133646-7d6it90e1dbsijc2
2

Show diffs side-by-side

added added

removed removed

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