first commit
This commit is contained in:
		
							
								
								
									
										47
									
								
								vendor/doctrine/instantiator/.doctrine-project.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								vendor/doctrine/instantiator/.doctrine-project.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,47 @@
 | 
			
		||||
{
 | 
			
		||||
    "active": true,
 | 
			
		||||
    "name": "Instantiator",
 | 
			
		||||
    "slug": "instantiator",
 | 
			
		||||
    "docsSlug": "doctrine-instantiator",
 | 
			
		||||
    "codePath": "/src",
 | 
			
		||||
    "versions": [
 | 
			
		||||
        {
 | 
			
		||||
            "name": "1.5",
 | 
			
		||||
            "branchName": "1.5.x",
 | 
			
		||||
            "slug": "latest",
 | 
			
		||||
            "upcoming": true
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            "name": "1.4",
 | 
			
		||||
            "branchName": "1.4.x",
 | 
			
		||||
            "slug": "1.4",
 | 
			
		||||
            "aliases": [
 | 
			
		||||
                "current",
 | 
			
		||||
                "stable"
 | 
			
		||||
            ],
 | 
			
		||||
            "maintained": true,
 | 
			
		||||
            "current": true
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            "name": "1.3",
 | 
			
		||||
            "branchName": "1.3.x",
 | 
			
		||||
            "slug": "1.3",
 | 
			
		||||
            "maintained": false
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            "name": "1.2",
 | 
			
		||||
            "branchName": "1.2.x",
 | 
			
		||||
            "slug": "1.2"
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            "name": "1.1",
 | 
			
		||||
            "branchName": "1.1.x",
 | 
			
		||||
            "slug": "1.1"
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            "name": "1.0",
 | 
			
		||||
            "branchName": "1.0.x",
 | 
			
		||||
            "slug": "1.0"
 | 
			
		||||
        }
 | 
			
		||||
    ]
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										35
									
								
								vendor/doctrine/instantiator/CONTRIBUTING.md
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								vendor/doctrine/instantiator/CONTRIBUTING.md
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,35 @@
 | 
			
		||||
# Contributing
 | 
			
		||||
 | 
			
		||||
 * Follow the [Doctrine Coding Standard](https://github.com/doctrine/coding-standard)
 | 
			
		||||
 * The project will follow strict [object calisthenics](http://www.slideshare.net/guilhermeblanco/object-calisthenics-applied-to-php)
 | 
			
		||||
 * Any contribution must provide tests for additional introduced conditions
 | 
			
		||||
 * Any un-confirmed issue needs a failing test case before being accepted
 | 
			
		||||
 * Pull requests must be sent from a new hotfix/feature branch, not from `master`.
 | 
			
		||||
 | 
			
		||||
## Installation
 | 
			
		||||
 | 
			
		||||
To install the project and run the tests, you need to clone it first:
 | 
			
		||||
 | 
			
		||||
```sh
 | 
			
		||||
$ git clone git://github.com/doctrine/instantiator.git
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
You will then need to run a composer installation:
 | 
			
		||||
 | 
			
		||||
```sh
 | 
			
		||||
$ cd Instantiator
 | 
			
		||||
$ curl -s https://getcomposer.org/installer | php
 | 
			
		||||
$ php composer.phar update
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Testing
 | 
			
		||||
 | 
			
		||||
The PHPUnit version to be used is the one installed as a dev- dependency via composer:
 | 
			
		||||
 | 
			
		||||
```sh
 | 
			
		||||
$ ./vendor/bin/phpunit
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Accepted coverage for new contributions is 80%. Any contribution not satisfying this requirement 
 | 
			
		||||
won't be merged.
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										19
									
								
								vendor/doctrine/instantiator/LICENSE
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								vendor/doctrine/instantiator/LICENSE
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
			
		||||
Copyright (c) 2014 Doctrine Project
 | 
			
		||||
 | 
			
		||||
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.
 | 
			
		||||
							
								
								
									
										38
									
								
								vendor/doctrine/instantiator/README.md
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								vendor/doctrine/instantiator/README.md
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,38 @@
 | 
			
		||||
# Instantiator
 | 
			
		||||
 | 
			
		||||
This library provides a way of avoiding usage of constructors when instantiating PHP classes.
 | 
			
		||||
 | 
			
		||||
[](https://travis-ci.org/doctrine/instantiator)
 | 
			
		||||
[](https://codecov.io/gh/doctrine/instantiator/branch/master)
 | 
			
		||||
[](https://www.versioneye.com/package/php--doctrine--instantiator)
 | 
			
		||||
 | 
			
		||||
[](https://packagist.org/packages/doctrine/instantiator)
 | 
			
		||||
[](https://packagist.org/packages/doctrine/instantiator)
 | 
			
		||||
 | 
			
		||||
## Installation
 | 
			
		||||
 | 
			
		||||
The suggested installation method is via [composer](https://getcomposer.org/):
 | 
			
		||||
 | 
			
		||||
```sh
 | 
			
		||||
php composer.phar require "doctrine/instantiator:~1.0.3"
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Usage
 | 
			
		||||
 | 
			
		||||
The instantiator is able to create new instances of any class without using the constructor or any API of the class
 | 
			
		||||
itself:
 | 
			
		||||
 | 
			
		||||
```php
 | 
			
		||||
$instantiator = new \Doctrine\Instantiator\Instantiator();
 | 
			
		||||
 | 
			
		||||
$instance = $instantiator->instantiate(\My\ClassName\Here::class);
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Contributing
 | 
			
		||||
 | 
			
		||||
Please read the [CONTRIBUTING.md](CONTRIBUTING.md) contents if you wish to help out!
 | 
			
		||||
 | 
			
		||||
## Credits
 | 
			
		||||
 | 
			
		||||
This library was migrated from [ocramius/instantiator](https://github.com/Ocramius/Instantiator), which
 | 
			
		||||
has been donated to the doctrine organization, and which is now deprecated in favour of this package.
 | 
			
		||||
							
								
								
									
										48
									
								
								vendor/doctrine/instantiator/composer.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								vendor/doctrine/instantiator/composer.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,48 @@
 | 
			
		||||
{
 | 
			
		||||
    "name":              "doctrine/instantiator",
 | 
			
		||||
    "description":       "A small, lightweight utility to instantiate objects in PHP without invoking their constructors",
 | 
			
		||||
    "type":              "library",
 | 
			
		||||
    "license":           "MIT",
 | 
			
		||||
    "homepage":          "https://www.doctrine-project.org/projects/instantiator.html",
 | 
			
		||||
    "keywords":          [
 | 
			
		||||
        "instantiate",
 | 
			
		||||
        "constructor"
 | 
			
		||||
    ],
 | 
			
		||||
    "authors": [
 | 
			
		||||
        {
 | 
			
		||||
            "name":     "Marco Pivetta",
 | 
			
		||||
            "email":    "ocramius@gmail.com",
 | 
			
		||||
            "homepage": "https://ocramius.github.io/"
 | 
			
		||||
        }
 | 
			
		||||
    ],
 | 
			
		||||
    "require": {
 | 
			
		||||
        "php": "^7.1 || ^8.0"
 | 
			
		||||
    },
 | 
			
		||||
    "require-dev": {
 | 
			
		||||
        "ext-phar":                  "*",
 | 
			
		||||
        "ext-pdo":                   "*",
 | 
			
		||||
        "doctrine/coding-standard":  "^9",
 | 
			
		||||
        "phpbench/phpbench":         "^0.16 || ^1",
 | 
			
		||||
        "phpstan/phpstan":           "^1.4",
 | 
			
		||||
        "phpstan/phpstan-phpunit":   "^1",
 | 
			
		||||
        "phpunit/phpunit":           "^7.5 || ^8.5 || ^9.5",
 | 
			
		||||
        "vimeo/psalm": "^4.22"
 | 
			
		||||
    },
 | 
			
		||||
    "autoload": {
 | 
			
		||||
        "psr-4": {
 | 
			
		||||
            "Doctrine\\Instantiator\\": "src/Doctrine/Instantiator/"
 | 
			
		||||
        }
 | 
			
		||||
    },
 | 
			
		||||
    "autoload-dev": {
 | 
			
		||||
        "psr-0": {
 | 
			
		||||
            "DoctrineTest\\InstantiatorPerformance\\": "tests",
 | 
			
		||||
            "DoctrineTest\\InstantiatorTest\\": "tests",
 | 
			
		||||
            "DoctrineTest\\InstantiatorTestAsset\\": "tests"
 | 
			
		||||
        }
 | 
			
		||||
    },
 | 
			
		||||
    "config": {
 | 
			
		||||
        "allow-plugins": {
 | 
			
		||||
            "dealerdirect/phpcodesniffer-composer-installer": true
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										68
									
								
								vendor/doctrine/instantiator/docs/en/index.rst
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								vendor/doctrine/instantiator/docs/en/index.rst
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,68 @@
 | 
			
		||||
Introduction
 | 
			
		||||
============
 | 
			
		||||
 | 
			
		||||
This library provides a way of avoiding usage of constructors when instantiating PHP classes.
 | 
			
		||||
 | 
			
		||||
Installation
 | 
			
		||||
============
 | 
			
		||||
 | 
			
		||||
The suggested installation method is via `composer`_:
 | 
			
		||||
 | 
			
		||||
.. code-block:: console
 | 
			
		||||
 | 
			
		||||
   $ composer require doctrine/instantiator
 | 
			
		||||
 | 
			
		||||
Usage
 | 
			
		||||
=====
 | 
			
		||||
 | 
			
		||||
The instantiator is able to create new instances of any class without
 | 
			
		||||
using the constructor or any API of the class itself:
 | 
			
		||||
 | 
			
		||||
.. code-block:: php
 | 
			
		||||
 | 
			
		||||
    <?php
 | 
			
		||||
 | 
			
		||||
    use Doctrine\Instantiator\Instantiator;
 | 
			
		||||
    use App\Entities\User;
 | 
			
		||||
 | 
			
		||||
    $instantiator = new Instantiator();
 | 
			
		||||
 | 
			
		||||
    $user = $instantiator->instantiate(User::class);
 | 
			
		||||
 | 
			
		||||
Contributing
 | 
			
		||||
============
 | 
			
		||||
 | 
			
		||||
-  Follow the `Doctrine Coding Standard`_
 | 
			
		||||
-  The project will follow strict `object calisthenics`_
 | 
			
		||||
-  Any contribution must provide tests for additional introduced
 | 
			
		||||
   conditions
 | 
			
		||||
-  Any un-confirmed issue needs a failing test case before being
 | 
			
		||||
   accepted
 | 
			
		||||
-  Pull requests must be sent from a new hotfix/feature branch, not from
 | 
			
		||||
   ``master``.
 | 
			
		||||
 | 
			
		||||
Testing
 | 
			
		||||
=======
 | 
			
		||||
 | 
			
		||||
The PHPUnit version to be used is the one installed as a dev- dependency
 | 
			
		||||
via composer:
 | 
			
		||||
 | 
			
		||||
.. code-block:: console
 | 
			
		||||
 | 
			
		||||
   $ ./vendor/bin/phpunit
 | 
			
		||||
 | 
			
		||||
Accepted coverage for new contributions is 80%. Any contribution not
 | 
			
		||||
satisfying this requirement won’t be merged.
 | 
			
		||||
 | 
			
		||||
Credits
 | 
			
		||||
=======
 | 
			
		||||
 | 
			
		||||
This library was migrated from `ocramius/instantiator`_, which has been
 | 
			
		||||
donated to the doctrine organization, and which is now deprecated in
 | 
			
		||||
favour of this package.
 | 
			
		||||
 | 
			
		||||
.. _composer: https://getcomposer.org/
 | 
			
		||||
.. _CONTRIBUTING.md: CONTRIBUTING.md
 | 
			
		||||
.. _ocramius/instantiator: https://github.com/Ocramius/Instantiator
 | 
			
		||||
.. _Doctrine Coding Standard: https://github.com/doctrine/coding-standard
 | 
			
		||||
.. _object calisthenics: http://www.slideshare.net/guilhermeblanco/object-calisthenics-applied-to-php
 | 
			
		||||
							
								
								
									
										4
									
								
								vendor/doctrine/instantiator/docs/en/sidebar.rst
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								vendor/doctrine/instantiator/docs/en/sidebar.rst
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
			
		||||
.. toctree::
 | 
			
		||||
    :depth: 3
 | 
			
		||||
 | 
			
		||||
    index
 | 
			
		||||
							
								
								
									
										16
									
								
								vendor/doctrine/instantiator/psalm.xml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								vendor/doctrine/instantiator/psalm.xml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,16 @@
 | 
			
		||||
<?xml version="1.0"?>
 | 
			
		||||
<psalm
 | 
			
		||||
    errorLevel="7"
 | 
			
		||||
    phpVersion="8.1"
 | 
			
		||||
    resolveFromConfigFile="true"
 | 
			
		||||
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 | 
			
		||||
    xmlns="https://getpsalm.org/schema/config"
 | 
			
		||||
    xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
 | 
			
		||||
>
 | 
			
		||||
    <projectFiles>
 | 
			
		||||
        <directory name="src" />
 | 
			
		||||
        <ignoreFiles>
 | 
			
		||||
            <directory name="vendor" />
 | 
			
		||||
        </ignoreFiles>
 | 
			
		||||
    </projectFiles>
 | 
			
		||||
</psalm>
 | 
			
		||||
							
								
								
									
										12
									
								
								vendor/doctrine/instantiator/src/Doctrine/Instantiator/Exception/ExceptionInterface.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								vendor/doctrine/instantiator/src/Doctrine/Instantiator/Exception/ExceptionInterface.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,12 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Doctrine\Instantiator\Exception;
 | 
			
		||||
 | 
			
		||||
use Throwable;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Base exception marker interface for the instantiator component
 | 
			
		||||
 */
 | 
			
		||||
interface ExceptionInterface extends Throwable
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										50
									
								
								vendor/doctrine/instantiator/src/Doctrine/Instantiator/Exception/InvalidArgumentException.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								vendor/doctrine/instantiator/src/Doctrine/Instantiator/Exception/InvalidArgumentException.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,50 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Doctrine\Instantiator\Exception;
 | 
			
		||||
 | 
			
		||||
use InvalidArgumentException as BaseInvalidArgumentException;
 | 
			
		||||
use ReflectionClass;
 | 
			
		||||
 | 
			
		||||
use function interface_exists;
 | 
			
		||||
use function sprintf;
 | 
			
		||||
use function trait_exists;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Exception for invalid arguments provided to the instantiator
 | 
			
		||||
 */
 | 
			
		||||
class InvalidArgumentException extends BaseInvalidArgumentException implements ExceptionInterface
 | 
			
		||||
{
 | 
			
		||||
    public static function fromNonExistingClass(string $className): self
 | 
			
		||||
    {
 | 
			
		||||
        if (interface_exists($className)) {
 | 
			
		||||
            return new self(sprintf('The provided type "%s" is an interface, and cannot be instantiated', $className));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (trait_exists($className)) {
 | 
			
		||||
            return new self(sprintf('The provided type "%s" is a trait, and cannot be instantiated', $className));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return new self(sprintf('The provided class "%s" does not exist', $className));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @phpstan-param ReflectionClass<T> $reflectionClass
 | 
			
		||||
     *
 | 
			
		||||
     * @template T of object
 | 
			
		||||
     */
 | 
			
		||||
    public static function fromAbstractClass(ReflectionClass $reflectionClass): self
 | 
			
		||||
    {
 | 
			
		||||
        return new self(sprintf(
 | 
			
		||||
            'The provided class "%s" is abstract, and cannot be instantiated',
 | 
			
		||||
            $reflectionClass->getName()
 | 
			
		||||
        ));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static function fromEnum(string $className): self
 | 
			
		||||
    {
 | 
			
		||||
        return new self(sprintf(
 | 
			
		||||
            'The provided class "%s" is an enum, and cannot be instantiated',
 | 
			
		||||
            $className
 | 
			
		||||
        ));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										59
									
								
								vendor/doctrine/instantiator/src/Doctrine/Instantiator/Exception/UnexpectedValueException.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								vendor/doctrine/instantiator/src/Doctrine/Instantiator/Exception/UnexpectedValueException.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,59 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Doctrine\Instantiator\Exception;
 | 
			
		||||
 | 
			
		||||
use Exception;
 | 
			
		||||
use ReflectionClass;
 | 
			
		||||
use UnexpectedValueException as BaseUnexpectedValueException;
 | 
			
		||||
 | 
			
		||||
use function sprintf;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Exception for given parameters causing invalid/unexpected state on instantiation
 | 
			
		||||
 */
 | 
			
		||||
class UnexpectedValueException extends BaseUnexpectedValueException implements ExceptionInterface
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @phpstan-param ReflectionClass<T> $reflectionClass
 | 
			
		||||
     *
 | 
			
		||||
     * @template T of object
 | 
			
		||||
     */
 | 
			
		||||
    public static function fromSerializationTriggeredException(
 | 
			
		||||
        ReflectionClass $reflectionClass,
 | 
			
		||||
        Exception $exception
 | 
			
		||||
    ): self {
 | 
			
		||||
        return new self(
 | 
			
		||||
            sprintf(
 | 
			
		||||
                'An exception was raised while trying to instantiate an instance of "%s" via un-serialization',
 | 
			
		||||
                $reflectionClass->getName()
 | 
			
		||||
            ),
 | 
			
		||||
            0,
 | 
			
		||||
            $exception
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @phpstan-param ReflectionClass<T> $reflectionClass
 | 
			
		||||
     *
 | 
			
		||||
     * @template T of object
 | 
			
		||||
     */
 | 
			
		||||
    public static function fromUncleanUnSerialization(
 | 
			
		||||
        ReflectionClass $reflectionClass,
 | 
			
		||||
        string $errorString,
 | 
			
		||||
        int $errorCode,
 | 
			
		||||
        string $errorFile,
 | 
			
		||||
        int $errorLine
 | 
			
		||||
    ): self {
 | 
			
		||||
        return new self(
 | 
			
		||||
            sprintf(
 | 
			
		||||
                'Could not produce an instance of "%s" via un-serialization, since an error was triggered '
 | 
			
		||||
                . 'in file "%s" at line "%d"',
 | 
			
		||||
                $reflectionClass->getName(),
 | 
			
		||||
                $errorFile,
 | 
			
		||||
                $errorLine
 | 
			
		||||
            ),
 | 
			
		||||
            0,
 | 
			
		||||
            new Exception($errorString, $errorCode)
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										260
									
								
								vendor/doctrine/instantiator/src/Doctrine/Instantiator/Instantiator.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										260
									
								
								vendor/doctrine/instantiator/src/Doctrine/Instantiator/Instantiator.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,260 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Doctrine\Instantiator;
 | 
			
		||||
 | 
			
		||||
use ArrayIterator;
 | 
			
		||||
use Doctrine\Instantiator\Exception\ExceptionInterface;
 | 
			
		||||
use Doctrine\Instantiator\Exception\InvalidArgumentException;
 | 
			
		||||
use Doctrine\Instantiator\Exception\UnexpectedValueException;
 | 
			
		||||
use Exception;
 | 
			
		||||
use ReflectionClass;
 | 
			
		||||
use ReflectionException;
 | 
			
		||||
use Serializable;
 | 
			
		||||
 | 
			
		||||
use function class_exists;
 | 
			
		||||
use function enum_exists;
 | 
			
		||||
use function is_subclass_of;
 | 
			
		||||
use function restore_error_handler;
 | 
			
		||||
use function set_error_handler;
 | 
			
		||||
use function sprintf;
 | 
			
		||||
use function strlen;
 | 
			
		||||
use function unserialize;
 | 
			
		||||
 | 
			
		||||
use const PHP_VERSION_ID;
 | 
			
		||||
 | 
			
		||||
final class Instantiator implements InstantiatorInterface
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * Markers used internally by PHP to define whether {@see \unserialize} should invoke
 | 
			
		||||
     * the method {@see \Serializable::unserialize()} when dealing with classes implementing
 | 
			
		||||
     * the {@see \Serializable} interface.
 | 
			
		||||
     */
 | 
			
		||||
    public const SERIALIZATION_FORMAT_USE_UNSERIALIZER   = 'C';
 | 
			
		||||
    public const SERIALIZATION_FORMAT_AVOID_UNSERIALIZER = 'O';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Used to instantiate specific classes, indexed by class name.
 | 
			
		||||
     *
 | 
			
		||||
     * @var callable[]
 | 
			
		||||
     */
 | 
			
		||||
    private static $cachedInstantiators = [];
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Array of objects that can directly be cloned, indexed by class name.
 | 
			
		||||
     *
 | 
			
		||||
     * @var object[]
 | 
			
		||||
     */
 | 
			
		||||
    private static $cachedCloneables = [];
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param string $className
 | 
			
		||||
     * @phpstan-param class-string<T> $className
 | 
			
		||||
     *
 | 
			
		||||
     * @return object
 | 
			
		||||
     * @phpstan-return T
 | 
			
		||||
     *
 | 
			
		||||
     * @throws ExceptionInterface
 | 
			
		||||
     *
 | 
			
		||||
     * @template T of object
 | 
			
		||||
     */
 | 
			
		||||
    public function instantiate($className)
 | 
			
		||||
    {
 | 
			
		||||
        if (isset(self::$cachedCloneables[$className])) {
 | 
			
		||||
            /**
 | 
			
		||||
             * @phpstan-var T
 | 
			
		||||
             */
 | 
			
		||||
            $cachedCloneable = self::$cachedCloneables[$className];
 | 
			
		||||
 | 
			
		||||
            return clone $cachedCloneable;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (isset(self::$cachedInstantiators[$className])) {
 | 
			
		||||
            $factory = self::$cachedInstantiators[$className];
 | 
			
		||||
 | 
			
		||||
            return $factory();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $this->buildAndCacheFromFactory($className);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Builds the requested object and caches it in static properties for performance
 | 
			
		||||
     *
 | 
			
		||||
     * @phpstan-param class-string<T> $className
 | 
			
		||||
     *
 | 
			
		||||
     * @return object
 | 
			
		||||
     * @phpstan-return T
 | 
			
		||||
     *
 | 
			
		||||
     * @template T of object
 | 
			
		||||
     */
 | 
			
		||||
    private function buildAndCacheFromFactory(string $className)
 | 
			
		||||
    {
 | 
			
		||||
        $factory  = self::$cachedInstantiators[$className] = $this->buildFactory($className);
 | 
			
		||||
        $instance = $factory();
 | 
			
		||||
 | 
			
		||||
        if ($this->isSafeToClone(new ReflectionClass($instance))) {
 | 
			
		||||
            self::$cachedCloneables[$className] = clone $instance;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $instance;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Builds a callable capable of instantiating the given $className without
 | 
			
		||||
     * invoking its constructor.
 | 
			
		||||
     *
 | 
			
		||||
     * @phpstan-param class-string<T> $className
 | 
			
		||||
     *
 | 
			
		||||
     * @phpstan-return callable(): T
 | 
			
		||||
     *
 | 
			
		||||
     * @throws InvalidArgumentException
 | 
			
		||||
     * @throws UnexpectedValueException
 | 
			
		||||
     * @throws ReflectionException
 | 
			
		||||
     *
 | 
			
		||||
     * @template T of object
 | 
			
		||||
     */
 | 
			
		||||
    private function buildFactory(string $className): callable
 | 
			
		||||
    {
 | 
			
		||||
        $reflectionClass = $this->getReflectionClass($className);
 | 
			
		||||
 | 
			
		||||
        if ($this->isInstantiableViaReflection($reflectionClass)) {
 | 
			
		||||
            return [$reflectionClass, 'newInstanceWithoutConstructor'];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $serializedString = sprintf(
 | 
			
		||||
            '%s:%d:"%s":0:{}',
 | 
			
		||||
            is_subclass_of($className, Serializable::class) ? self::SERIALIZATION_FORMAT_USE_UNSERIALIZER : self::SERIALIZATION_FORMAT_AVOID_UNSERIALIZER,
 | 
			
		||||
            strlen($className),
 | 
			
		||||
            $className
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $this->checkIfUnSerializationIsSupported($reflectionClass, $serializedString);
 | 
			
		||||
 | 
			
		||||
        return static function () use ($serializedString) {
 | 
			
		||||
            return unserialize($serializedString);
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @phpstan-param class-string<T> $className
 | 
			
		||||
     *
 | 
			
		||||
     * @phpstan-return ReflectionClass<T>
 | 
			
		||||
     *
 | 
			
		||||
     * @throws InvalidArgumentException
 | 
			
		||||
     * @throws ReflectionException
 | 
			
		||||
     *
 | 
			
		||||
     * @template T of object
 | 
			
		||||
     */
 | 
			
		||||
    private function getReflectionClass(string $className): ReflectionClass
 | 
			
		||||
    {
 | 
			
		||||
        if (! class_exists($className)) {
 | 
			
		||||
            throw InvalidArgumentException::fromNonExistingClass($className);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (PHP_VERSION_ID >= 80100 && enum_exists($className, false)) {
 | 
			
		||||
            throw InvalidArgumentException::fromEnum($className);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $reflection = new ReflectionClass($className);
 | 
			
		||||
 | 
			
		||||
        if ($reflection->isAbstract()) {
 | 
			
		||||
            throw InvalidArgumentException::fromAbstractClass($reflection);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $reflection;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @phpstan-param ReflectionClass<T> $reflectionClass
 | 
			
		||||
     *
 | 
			
		||||
     * @throws UnexpectedValueException
 | 
			
		||||
     *
 | 
			
		||||
     * @template T of object
 | 
			
		||||
     */
 | 
			
		||||
    private function checkIfUnSerializationIsSupported(ReflectionClass $reflectionClass, string $serializedString): void
 | 
			
		||||
    {
 | 
			
		||||
        set_error_handler(static function (int $code, string $message, string $file, int $line) use ($reflectionClass, &$error): bool {
 | 
			
		||||
            $error = UnexpectedValueException::fromUncleanUnSerialization(
 | 
			
		||||
                $reflectionClass,
 | 
			
		||||
                $message,
 | 
			
		||||
                $code,
 | 
			
		||||
                $file,
 | 
			
		||||
                $line
 | 
			
		||||
            );
 | 
			
		||||
 | 
			
		||||
            return true;
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            $this->attemptInstantiationViaUnSerialization($reflectionClass, $serializedString);
 | 
			
		||||
        } finally {
 | 
			
		||||
            restore_error_handler();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ($error) {
 | 
			
		||||
            throw $error;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @phpstan-param ReflectionClass<T> $reflectionClass
 | 
			
		||||
     *
 | 
			
		||||
     * @throws UnexpectedValueException
 | 
			
		||||
     *
 | 
			
		||||
     * @template T of object
 | 
			
		||||
     */
 | 
			
		||||
    private function attemptInstantiationViaUnSerialization(ReflectionClass $reflectionClass, string $serializedString): void
 | 
			
		||||
    {
 | 
			
		||||
        try {
 | 
			
		||||
            unserialize($serializedString);
 | 
			
		||||
        } catch (Exception $exception) {
 | 
			
		||||
            throw UnexpectedValueException::fromSerializationTriggeredException($reflectionClass, $exception);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @phpstan-param ReflectionClass<T> $reflectionClass
 | 
			
		||||
     *
 | 
			
		||||
     * @template T of object
 | 
			
		||||
     */
 | 
			
		||||
    private function isInstantiableViaReflection(ReflectionClass $reflectionClass): bool
 | 
			
		||||
    {
 | 
			
		||||
        return ! ($this->hasInternalAncestors($reflectionClass) && $reflectionClass->isFinal());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Verifies whether the given class is to be considered internal
 | 
			
		||||
     *
 | 
			
		||||
     * @phpstan-param ReflectionClass<T> $reflectionClass
 | 
			
		||||
     *
 | 
			
		||||
     * @template T of object
 | 
			
		||||
     */
 | 
			
		||||
    private function hasInternalAncestors(ReflectionClass $reflectionClass): bool
 | 
			
		||||
    {
 | 
			
		||||
        do {
 | 
			
		||||
            if ($reflectionClass->isInternal()) {
 | 
			
		||||
                return true;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $reflectionClass = $reflectionClass->getParentClass();
 | 
			
		||||
        } while ($reflectionClass);
 | 
			
		||||
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks if a class is cloneable
 | 
			
		||||
     *
 | 
			
		||||
     * Classes implementing `__clone` cannot be safely cloned, as that may cause side-effects.
 | 
			
		||||
     *
 | 
			
		||||
     * @phpstan-param ReflectionClass<T> $reflectionClass
 | 
			
		||||
     *
 | 
			
		||||
     * @template T of object
 | 
			
		||||
     */
 | 
			
		||||
    private function isSafeToClone(ReflectionClass $reflectionClass): bool
 | 
			
		||||
    {
 | 
			
		||||
        return $reflectionClass->isCloneable()
 | 
			
		||||
            && ! $reflectionClass->hasMethod('__clone')
 | 
			
		||||
            && ! $reflectionClass->isSubclassOf(ArrayIterator::class);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										24
									
								
								vendor/doctrine/instantiator/src/Doctrine/Instantiator/InstantiatorInterface.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								vendor/doctrine/instantiator/src/Doctrine/Instantiator/InstantiatorInterface.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Doctrine\Instantiator;
 | 
			
		||||
 | 
			
		||||
use Doctrine\Instantiator\Exception\ExceptionInterface;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Instantiator provides utility methods to build objects without invoking their constructors
 | 
			
		||||
 */
 | 
			
		||||
interface InstantiatorInterface
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @param string $className
 | 
			
		||||
     * @phpstan-param class-string<T> $className
 | 
			
		||||
     *
 | 
			
		||||
     * @return object
 | 
			
		||||
     * @phpstan-return T
 | 
			
		||||
     *
 | 
			
		||||
     * @throws ExceptionInterface
 | 
			
		||||
     *
 | 
			
		||||
     * @template T of object
 | 
			
		||||
     */
 | 
			
		||||
    public function instantiate($className);
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user