3
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
* This software consists of voluntary contributions made by many individuals
16
* and is licensed under the MIT license. For more information, see
17
* <http://www.doctrine-project.org>.
20
namespace Doctrine\Instantiator;
23
use Doctrine\Instantiator\Exception\InvalidArgumentException;
24
use Doctrine\Instantiator\Exception\UnexpectedValueException;
31
* @author Marco Pivetta <ocramius@gmail.com>
33
final class Instantiator implements InstantiatorInterface
36
* Markers used internally by PHP to define whether {@see \unserialize} should invoke
37
* the method {@see \Serializable::unserialize()} when dealing with classes implementing
38
* the {@see \Serializable} interface.
40
const SERIALIZATION_FORMAT_USE_UNSERIALIZER = 'C';
41
const SERIALIZATION_FORMAT_AVOID_UNSERIALIZER = 'O';
44
* @var \Closure[] of {@see \Closure} instances used to instantiate specific classes
46
private static $cachedInstantiators = array();
49
* @var object[] of objects that can directly be cloned
51
private static $cachedCloneables = array();
56
public function instantiate($className)
58
if (isset(self::$cachedCloneables[$className])) {
59
return clone self::$cachedCloneables[$className];
62
if (isset(self::$cachedInstantiators[$className])) {
63
$factory = self::$cachedInstantiators[$className];
68
$factory = self::$cachedInstantiators[$className] = $this->buildFactory($className);
69
$instance = $factory();
70
$reflection = new ReflectionClass($instance);
72
if ($this->isSafeToClone($reflection)) {
73
self::$cachedCloneables[$className] = clone $instance;
83
* Builds a {@see \Closure} capable of instantiating the given $className without
84
* invoking its constructor.
85
* This method is only exposed as public because of PHP 5.3 compatibility. Do not
86
* use this method in your own code
88
* @param string $className
92
public function buildFactory($className)
94
$reflectionClass = $this->getReflectionClass($className);
96
if ($this->isInstantiableViaReflection($reflectionClass)) {
97
return function () use ($reflectionClass) {
98
return $reflectionClass->newInstanceWithoutConstructor();
102
$serializedString = sprintf(
104
$this->getSerializationFormat($reflectionClass),
109
$this->attemptInstantiationViaUnSerialization($reflectionClass, $serializedString);
111
return function () use ($serializedString) {
112
return unserialize($serializedString);
117
* @param string $className
119
* @return ReflectionClass
121
* @throws InvalidArgumentException
123
private function getReflectionClass($className)
125
if (! class_exists($className)) {
126
throw InvalidArgumentException::fromNonExistingClass($className);
129
$reflection = new ReflectionClass($className);
131
if ($reflection->isAbstract()) {
132
throw InvalidArgumentException::fromAbstractClass($reflection);
139
* @param ReflectionClass $reflectionClass
140
* @param string $serializedString
142
* @throws UnexpectedValueException
146
private function attemptInstantiationViaUnSerialization(ReflectionClass $reflectionClass, $serializedString)
148
set_error_handler(function ($code, $message, $file, $line) use ($reflectionClass, & $error) {
149
$error = UnexpectedValueException::fromUncleanUnSerialization(
159
unserialize($serializedString);
160
} catch (Exception $exception) {
161
restore_error_handler();
163
throw UnexpectedValueException::fromSerializationTriggeredException($reflectionClass, $exception);
166
restore_error_handler();
174
* @param ReflectionClass $reflectionClass
178
private function isInstantiableViaReflection(ReflectionClass $reflectionClass)
180
if (\PHP_VERSION_ID >= 50600) {
181
return ! ($reflectionClass->isInternal() && $reflectionClass->isFinal());
184
return \PHP_VERSION_ID >= 50400 && ! $this->hasInternalAncestors($reflectionClass);
188
* Verifies whether the given class is to be considered internal
190
* @param ReflectionClass $reflectionClass
194
private function hasInternalAncestors(ReflectionClass $reflectionClass)
197
if ($reflectionClass->isInternal()) {
200
} while ($reflectionClass = $reflectionClass->getParentClass());
206
* Verifies if the given PHP version implements the `Serializable` interface serialization
207
* with an incompatible serialization format. If that's the case, use serialization marker
208
* "C" instead of "O".
210
* @link http://news.php.net/php.internals/74654
212
* @param ReflectionClass $reflectionClass
214
* @return string the serialization format marker, either self::SERIALIZATION_FORMAT_USE_UNSERIALIZER
215
* or self::SERIALIZATION_FORMAT_AVOID_UNSERIALIZER
217
private function getSerializationFormat(ReflectionClass $reflectionClass)
219
if ($this->isPhpVersionWithBrokenSerializationFormat()
220
&& $reflectionClass->implementsInterface('Serializable')
222
return self::SERIALIZATION_FORMAT_USE_UNSERIALIZER;
225
return self::SERIALIZATION_FORMAT_AVOID_UNSERIALIZER;
229
* Checks whether the current PHP runtime uses an incompatible serialization format
233
private function isPhpVersionWithBrokenSerializationFormat()
235
return PHP_VERSION_ID === 50429 || PHP_VERSION_ID === 50513;
239
* Checks if a class is cloneable
241
* @param ReflectionClass $reflection
245
private function isSafeToClone(ReflectionClass $reflection)
247
if (method_exists($reflection, 'isCloneable') && ! $reflection->isCloneable()) {
251
// not cloneable if it implements `__clone`, as we want to avoid calling it
252
return ! $reflection->hasMethod('__clone');