ajout de deux routes avec des requetes PUT (pas encore fonctionnelle) et test de documentation avec swagger
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
parent
f8caddc10d
commit
f10b2e4736
@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"require": {
|
"require": {
|
||||||
"slim/slim": "4.*",
|
"slim/slim": "4.*",
|
||||||
"slim/psr7": "^1.6"
|
"slim/psr7": "^1.6",
|
||||||
|
"zircote/swagger-php": "^4.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,6 @@
|
|||||||
|
<?php
|
||||||
|
require(__DIR__."/../vendor/autoload.php");
|
||||||
|
$openapi = \OpenApi\Generator::scan([__DIR__.'/../routes/Inscrit.php']);
|
||||||
|
//header('Content-Type: application/json');
|
||||||
|
echo $openapi->toJSON();
|
||||||
|
?>
|
@ -0,0 +1,19 @@
|
|||||||
|
Copyright (c) 2006-2013 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.
|
@ -0,0 +1,24 @@
|
|||||||
|
⚠️ PHP 8 introduced
|
||||||
|
[attributes](https://www.php.net/manual/en/language.attributes.overview.php),
|
||||||
|
which are a native replacement for annotations. As such, this library is
|
||||||
|
considered feature complete, and should receive exclusively bugfixes and
|
||||||
|
security fixes.
|
||||||
|
|
||||||
|
# Doctrine Annotations
|
||||||
|
|
||||||
|
[![Build Status](https://github.com/doctrine/annotations/workflows/Continuous%20Integration/badge.svg?label=build)](https://github.com/doctrine/persistence/actions)
|
||||||
|
[![Dependency Status](https://www.versioneye.com/package/php--doctrine--annotations/badge.png)](https://www.versioneye.com/package/php--doctrine--annotations)
|
||||||
|
[![Reference Status](https://www.versioneye.com/php/doctrine:annotations/reference_badge.svg)](https://www.versioneye.com/php/doctrine:annotations/references)
|
||||||
|
[![Total Downloads](https://poser.pugx.org/doctrine/annotations/downloads.png)](https://packagist.org/packages/doctrine/annotations)
|
||||||
|
[![Latest Stable Version](https://img.shields.io/packagist/v/doctrine/annotations.svg?label=stable)](https://packagist.org/packages/doctrine/annotations)
|
||||||
|
|
||||||
|
Docblock Annotations Parser library (extracted from [Doctrine Common](https://github.com/doctrine/common)).
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
See the [doctrine-project website](https://www.doctrine-project.org/projects/doctrine-annotations/en/latest/index.html).
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
When making a pull request, make sure your changes follow the
|
||||||
|
[Coding Standard Guidelines](https://www.doctrine-project.org/projects/doctrine-coding-standard/en/current/reference/index.html#introduction).
|
@ -0,0 +1,72 @@
|
|||||||
|
{
|
||||||
|
"name": "doctrine/annotations",
|
||||||
|
"description": "Docblock Annotations Parser",
|
||||||
|
"license": "MIT",
|
||||||
|
"type": "library",
|
||||||
|
"keywords": [
|
||||||
|
"annotations",
|
||||||
|
"docblock",
|
||||||
|
"parser"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Guilherme Blanco",
|
||||||
|
"email": "guilhermeblanco@gmail.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Roman Borschel",
|
||||||
|
"email": "roman@code-factory.org"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Benjamin Eberlei",
|
||||||
|
"email": "kontakt@beberlei.de"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Jonathan Wage",
|
||||||
|
"email": "jonwage@gmail.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Johannes Schmitt",
|
||||||
|
"email": "schmittjoh@gmail.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"homepage": "https://www.doctrine-project.org/projects/annotations.html",
|
||||||
|
"require": {
|
||||||
|
"php": "^7.1 || ^8.0",
|
||||||
|
"ext-tokenizer": "*",
|
||||||
|
"doctrine/lexer": "^1 || ^2",
|
||||||
|
"psr/cache": "^1 || ^2 || ^3"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"doctrine/cache": "^1.11 || ^2.0",
|
||||||
|
"doctrine/coding-standard": "^9 || ^10",
|
||||||
|
"phpstan/phpstan": "~1.4.10 || ^1.8.0",
|
||||||
|
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.5",
|
||||||
|
"symfony/cache": "^4.4 || ^5.4 || ^6",
|
||||||
|
"vimeo/psalm": "^4.10"
|
||||||
|
},
|
||||||
|
"suggest": {
|
||||||
|
"php": "PHP 8.0 or higher comes with attributes, a native replacement for annotations"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Doctrine\\Common\\Annotations\\": "lib/Doctrine/Common/Annotations"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload-dev": {
|
||||||
|
"psr-4": {
|
||||||
|
"Doctrine\\Performance\\Common\\Annotations\\": "tests/Doctrine/Performance/Common/Annotations",
|
||||||
|
"Doctrine\\Tests\\Common\\Annotations\\": "tests/Doctrine/Tests/Common/Annotations"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"tests/Doctrine/Tests/Common/Annotations/Fixtures/functions.php",
|
||||||
|
"tests/Doctrine/Tests/Common/Annotations/Fixtures/SingleClassLOC1000.php"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"allow-plugins": {
|
||||||
|
"dealerdirect/phpcodesniffer-composer-installer": true
|
||||||
|
},
|
||||||
|
"sort-packages": true
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,252 @@
|
|||||||
|
Handling Annotations
|
||||||
|
====================
|
||||||
|
|
||||||
|
There are several different approaches to handling annotations in PHP.
|
||||||
|
Doctrine Annotations maps docblock annotations to PHP classes. Because
|
||||||
|
not all docblock annotations are used for metadata purposes a filter is
|
||||||
|
applied to ignore or skip classes that are not Doctrine annotations.
|
||||||
|
|
||||||
|
Take a look at the following code snippet:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
namespace MyProject\Entities;
|
||||||
|
|
||||||
|
use Doctrine\ORM\Mapping AS ORM;
|
||||||
|
use Symfony\Component\Validator\Constraints AS Assert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Benjamin Eberlei
|
||||||
|
* @ORM\Entity
|
||||||
|
* @MyProject\Annotations\Foobarable
|
||||||
|
*/
|
||||||
|
class User
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @ORM\Id @ORM\Column @ORM\GeneratedValue
|
||||||
|
* @dummy
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="string")
|
||||||
|
* @Assert\NotEmpty
|
||||||
|
* @Assert\Email
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $email;
|
||||||
|
}
|
||||||
|
|
||||||
|
In this snippet you can see a variety of different docblock annotations:
|
||||||
|
|
||||||
|
- Documentation annotations such as ``@var`` and ``@author``. These
|
||||||
|
annotations are ignored and never considered for throwing an
|
||||||
|
exception due to wrongly used annotations.
|
||||||
|
- Annotations imported through use statements. The statement ``use
|
||||||
|
Doctrine\ORM\Mapping AS ORM`` makes all classes under that namespace
|
||||||
|
available as ``@ORM\ClassName``. Same goes for the import of
|
||||||
|
``@Assert``.
|
||||||
|
- The ``@dummy`` annotation. It is not a documentation annotation and
|
||||||
|
not ignored. For Doctrine Annotations it is not entirely clear how
|
||||||
|
to handle this annotation. Depending on the configuration an exception
|
||||||
|
(unknown annotation) will be thrown when parsing this annotation.
|
||||||
|
- The fully qualified annotation ``@MyProject\Annotations\Foobarable``.
|
||||||
|
This is transformed directly into the given class name.
|
||||||
|
|
||||||
|
How are these annotations loaded? From looking at the code you could
|
||||||
|
guess that the ORM Mapping, Assert Validation and the fully qualified
|
||||||
|
annotation can just be loaded using
|
||||||
|
the defined PHP autoloaders. This is not the case however: For error
|
||||||
|
handling reasons every check for class existence inside the
|
||||||
|
``AnnotationReader`` sets the second parameter $autoload
|
||||||
|
of ``class_exists($name, $autoload)`` to false. To work flawlessly the
|
||||||
|
``AnnotationReader`` requires silent autoloaders which many autoloaders are
|
||||||
|
not. Silent autoloading is NOT part of the `PSR-0 specification
|
||||||
|
<https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md>`_
|
||||||
|
for autoloading.
|
||||||
|
|
||||||
|
This is why Doctrine Annotations uses its own autoloading mechanism
|
||||||
|
through a global registry. If you are wondering about the annotation
|
||||||
|
registry being global, there is no other way to solve the architectural
|
||||||
|
problems of autoloading annotation classes in a straightforward fashion.
|
||||||
|
Additionally if you think about PHP autoloading then you recognize it is
|
||||||
|
a global as well.
|
||||||
|
|
||||||
|
To anticipate the configuration section, making the above PHP class work
|
||||||
|
with Doctrine Annotations requires this setup:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
use Doctrine\Common\Annotations\AnnotationReader;
|
||||||
|
use Doctrine\Common\Annotations\AnnotationRegistry;
|
||||||
|
|
||||||
|
AnnotationRegistry::registerFile("/path/to/doctrine/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php");
|
||||||
|
AnnotationRegistry::registerAutoloadNamespace("Symfony\Component\Validator\Constraint", "/path/to/symfony/src");
|
||||||
|
AnnotationRegistry::registerAutoloadNamespace("MyProject\Annotations", "/path/to/myproject/src");
|
||||||
|
|
||||||
|
$reader = new AnnotationReader();
|
||||||
|
AnnotationReader::addGlobalIgnoredName('dummy');
|
||||||
|
|
||||||
|
The second block with the annotation registry calls registers all the
|
||||||
|
three different annotation namespaces that are used.
|
||||||
|
Doctrine Annotations saves all its annotations in a single file, that is
|
||||||
|
why ``AnnotationRegistry#registerFile`` is used in contrast to
|
||||||
|
``AnnotationRegistry#registerAutoloadNamespace`` which creates a PSR-0
|
||||||
|
compatible loading mechanism for class to file names.
|
||||||
|
|
||||||
|
In the third block, we create the actual ``AnnotationReader`` instance.
|
||||||
|
Note that we also add ``dummy`` to the global list of ignored
|
||||||
|
annotations for which we do not throw exceptions. Setting this is
|
||||||
|
necessary in our example case, otherwise ``@dummy`` would trigger an
|
||||||
|
exception to be thrown during the parsing of the docblock of
|
||||||
|
``MyProject\Entities\User#id``.
|
||||||
|
|
||||||
|
Setup and Configuration
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
To use the annotations library is simple, you just need to create a new
|
||||||
|
``AnnotationReader`` instance:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
$reader = new \Doctrine\Common\Annotations\AnnotationReader();
|
||||||
|
|
||||||
|
This creates a simple annotation reader with no caching other than in
|
||||||
|
memory (in php arrays). Since parsing docblocks can be expensive you
|
||||||
|
should cache this process by using a caching reader.
|
||||||
|
|
||||||
|
To cache annotations, you can create a ``Doctrine\Common\Annotations\PsrCachedReader``.
|
||||||
|
This reader decorates the original reader and stores all annotations in a PSR-6
|
||||||
|
cache:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
use Doctrine\Common\Annotations\AnnotationReader;
|
||||||
|
use Doctrine\Common\Annotations\PsrCachedReader;
|
||||||
|
|
||||||
|
$cache = ... // instantiate a PSR-6 Cache pool
|
||||||
|
|
||||||
|
$reader = new PsrCachedReader(
|
||||||
|
new AnnotationReader(),
|
||||||
|
$cache,
|
||||||
|
$debug = true
|
||||||
|
);
|
||||||
|
|
||||||
|
The ``debug`` flag is used here as well to invalidate the cache files
|
||||||
|
when the PHP class with annotations changed and should be used during
|
||||||
|
development.
|
||||||
|
|
||||||
|
.. warning ::
|
||||||
|
|
||||||
|
The ``AnnotationReader`` works and caches under the
|
||||||
|
assumption that all annotations of a doc-block are processed at
|
||||||
|
once. That means that annotation classes that do not exist and
|
||||||
|
aren't loaded and cannot be autoloaded (using the
|
||||||
|
AnnotationRegistry) would never be visible and not accessible if a
|
||||||
|
cache is used unless the cache is cleared and the annotations
|
||||||
|
requested again, this time with all annotations defined.
|
||||||
|
|
||||||
|
By default the annotation reader returns a list of annotations with
|
||||||
|
numeric indexes. If you want your annotations to be indexed by their
|
||||||
|
class name you can wrap the reader in an ``IndexedReader``:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
use Doctrine\Common\Annotations\AnnotationReader;
|
||||||
|
use Doctrine\Common\Annotations\IndexedReader;
|
||||||
|
|
||||||
|
$reader = new IndexedReader(new AnnotationReader());
|
||||||
|
|
||||||
|
.. warning::
|
||||||
|
|
||||||
|
You should never wrap the indexed reader inside a cached reader,
|
||||||
|
only the other way around. This way you can re-use the cache with
|
||||||
|
indexed or numeric keys, otherwise your code may experience failures
|
||||||
|
due to caching in a numerical or indexed format.
|
||||||
|
|
||||||
|
Registering Annotations
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
As explained in the introduction, Doctrine Annotations uses its own
|
||||||
|
autoloading mechanism to determine if a given annotation has a
|
||||||
|
corresponding PHP class that can be autoloaded. For annotation
|
||||||
|
autoloading you have to configure the
|
||||||
|
``Doctrine\Common\Annotations\AnnotationRegistry``. There are three
|
||||||
|
different mechanisms to configure annotation autoloading:
|
||||||
|
|
||||||
|
- Calling ``AnnotationRegistry#registerFile($file)`` to register a file
|
||||||
|
that contains one or more annotation classes.
|
||||||
|
- Calling ``AnnotationRegistry#registerNamespace($namespace, $dirs =
|
||||||
|
null)`` to register that the given namespace contains annotations and
|
||||||
|
that their base directory is located at the given $dirs or in the
|
||||||
|
include path if ``NULL`` is passed. The given directories should *NOT*
|
||||||
|
be the directory where classes of the namespace are in, but the base
|
||||||
|
directory of the root namespace. The AnnotationRegistry uses a
|
||||||
|
namespace to directory separator approach to resolve the correct path.
|
||||||
|
- Calling ``AnnotationRegistry#registerLoader($callable)`` to register
|
||||||
|
an autoloader callback. The callback accepts the class as first and
|
||||||
|
only parameter and has to return ``true`` if the corresponding file
|
||||||
|
was found and included.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
Loaders have to fail silently, if a class is not found even if it
|
||||||
|
matches for example the namespace prefix of that loader. Never is a
|
||||||
|
loader to throw a warning or exception if the loading failed
|
||||||
|
otherwise parsing doc block annotations will become a huge pain.
|
||||||
|
|
||||||
|
A sample loader callback could look like:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
use Doctrine\Common\Annotations\AnnotationRegistry;
|
||||||
|
use Symfony\Component\ClassLoader\UniversalClassLoader;
|
||||||
|
|
||||||
|
AnnotationRegistry::registerLoader(function($class) {
|
||||||
|
$file = str_replace("\\", DIRECTORY_SEPARATOR, $class) . ".php";
|
||||||
|
|
||||||
|
if (file_exists("/my/base/path/" . $file)) {
|
||||||
|
// file_exists() makes sure that the loader fails silently
|
||||||
|
require "/my/base/path/" . $file;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$loader = new UniversalClassLoader();
|
||||||
|
AnnotationRegistry::registerLoader(array($loader, "loadClass"));
|
||||||
|
|
||||||
|
|
||||||
|
Ignoring missing exceptions
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
By default an exception is thrown from the ``AnnotationReader`` if an
|
||||||
|
annotation was found that:
|
||||||
|
|
||||||
|
- is not part of the list of ignored "documentation annotations";
|
||||||
|
- was not imported through a use statement;
|
||||||
|
- is not a fully qualified class that exists.
|
||||||
|
|
||||||
|
You can disable this behavior for specific names if your docblocks do
|
||||||
|
not follow strict requirements:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
$reader = new \Doctrine\Common\Annotations\AnnotationReader();
|
||||||
|
AnnotationReader::addGlobalIgnoredName('foo');
|
||||||
|
|
||||||
|
PHP Imports
|
||||||
|
~~~~~~~~~~~
|
||||||
|
|
||||||
|
By default the annotation reader parses the use-statement of a php file
|
||||||
|
to gain access to the import rules and register them for the annotation
|
||||||
|
processing. Only if you are using PHP Imports can you validate the
|
||||||
|
correct usage of annotations and throw exceptions if you misspelled an
|
||||||
|
annotation. This mechanism is enabled by default.
|
||||||
|
|
||||||
|
To ease the upgrade path, we still allow you to disable this mechanism.
|
||||||
|
Note however that we will remove this in future versions:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
$reader = new \Doctrine\Common\Annotations\AnnotationReader();
|
||||||
|
$reader->setEnabledPhpImports(false);
|
@ -0,0 +1,443 @@
|
|||||||
|
Custom Annotation Classes
|
||||||
|
=========================
|
||||||
|
|
||||||
|
If you want to define your own annotations, you just have to group them
|
||||||
|
in a namespace and register this namespace in the ``AnnotationRegistry``.
|
||||||
|
Annotation classes have to contain a class-level docblock with the text
|
||||||
|
``@Annotation``:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
namespace MyCompany\Annotations;
|
||||||
|
|
||||||
|
/** @Annotation */
|
||||||
|
class Bar
|
||||||
|
{
|
||||||
|
// some code
|
||||||
|
}
|
||||||
|
|
||||||
|
Inject annotation values
|
||||||
|
------------------------
|
||||||
|
|
||||||
|
The annotation parser checks if the annotation constructor has arguments,
|
||||||
|
if so then it will pass the value array, otherwise it will try to inject
|
||||||
|
values into public properties directly:
|
||||||
|
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
namespace MyCompany\Annotations;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Annotation
|
||||||
|
*
|
||||||
|
* Some Annotation using a constructor
|
||||||
|
*/
|
||||||
|
class Bar
|
||||||
|
{
|
||||||
|
private $foo;
|
||||||
|
|
||||||
|
public function __construct(array $values)
|
||||||
|
{
|
||||||
|
$this->foo = $values['foo'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Annotation
|
||||||
|
*
|
||||||
|
* Some Annotation without a constructor
|
||||||
|
*/
|
||||||
|
class Foo
|
||||||
|
{
|
||||||
|
public $bar;
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional: Constructors with Named Parameters
|
||||||
|
--------------------------------------------
|
||||||
|
|
||||||
|
Starting with Annotations v1.11 a new annotation instantiation strategy
|
||||||
|
is available that aims at compatibility of Annotation classes with the PHP 8
|
||||||
|
attribute feature. You need to declare a constructor with regular parameter
|
||||||
|
names that match the named arguments in the annotation syntax.
|
||||||
|
|
||||||
|
To enable this feature, you can tag your annotation class with
|
||||||
|
``@NamedArgumentConstructor`` (available from v1.12) or implement the
|
||||||
|
``Doctrine\Common\Annotations\NamedArgumentConstructorAnnotation`` interface
|
||||||
|
(available from v1.11 and deprecated as of v1.12).
|
||||||
|
When using the ``@NamedArgumentConstructor`` tag, the first argument of the
|
||||||
|
constructor is considered as the default one.
|
||||||
|
|
||||||
|
|
||||||
|
Usage with the ``@NamedArgumentConstructor`` tag
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
namespace MyCompany\Annotations;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Annotation
|
||||||
|
* @NamedArgumentConstructor
|
||||||
|
*/
|
||||||
|
class Bar implements NamedArgumentConstructorAnnotation
|
||||||
|
{
|
||||||
|
private $foo;
|
||||||
|
|
||||||
|
public function __construct(string $foo)
|
||||||
|
{
|
||||||
|
$this->foo = $foo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Usable with @Bar(foo="baz") */
|
||||||
|
/** Usable with @Bar("baz") */
|
||||||
|
|
||||||
|
In combination with PHP 8's constructor property promotion feature
|
||||||
|
you can simplify this to:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
namespace MyCompany\Annotations;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Annotation
|
||||||
|
* @NamedArgumentConstructor
|
||||||
|
*/
|
||||||
|
class Bar implements NamedArgumentConstructorAnnotation
|
||||||
|
{
|
||||||
|
public function __construct(private string $foo) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Usage with the
|
||||||
|
``Doctrine\Common\Annotations\NamedArgumentConstructorAnnotation``
|
||||||
|
interface (v1.11, deprecated as of v1.12):
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
namespace MyCompany\Annotations;
|
||||||
|
|
||||||
|
use Doctrine\Common\Annotations\NamedArgumentConstructorAnnotation;
|
||||||
|
|
||||||
|
/** @Annotation */
|
||||||
|
class Bar implements NamedArgumentConstructorAnnotation
|
||||||
|
{
|
||||||
|
private $foo;
|
||||||
|
|
||||||
|
public function __construct(private string $foo) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Usable with @Bar(foo="baz") */
|
||||||
|
|
||||||
|
Annotation Target
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
``@Target`` indicates the kinds of class elements to which an annotation
|
||||||
|
type is applicable. Then you could define one or more targets:
|
||||||
|
|
||||||
|
- ``CLASS`` Allowed in class docblocks
|
||||||
|
- ``PROPERTY`` Allowed in property docblocks
|
||||||
|
- ``METHOD`` Allowed in the method docblocks
|
||||||
|
- ``FUNCTION`` Allowed in function dockblocks
|
||||||
|
- ``ALL`` Allowed in class, property, method and function docblocks
|
||||||
|
- ``ANNOTATION`` Allowed inside other annotations
|
||||||
|
|
||||||
|
If the annotations is not allowed in the current context, an
|
||||||
|
``AnnotationException`` is thrown.
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
namespace MyCompany\Annotations;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Annotation
|
||||||
|
* @Target({"METHOD","PROPERTY"})
|
||||||
|
*/
|
||||||
|
class Bar
|
||||||
|
{
|
||||||
|
// some code
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Annotation
|
||||||
|
* @Target("CLASS")
|
||||||
|
*/
|
||||||
|
class Foo
|
||||||
|
{
|
||||||
|
// some code
|
||||||
|
}
|
||||||
|
|
||||||
|
Attribute types
|
||||||
|
---------------
|
||||||
|
|
||||||
|
The annotation parser checks the given parameters using the phpdoc
|
||||||
|
annotation ``@var``, The data type could be validated using the ``@var``
|
||||||
|
annotation on the annotation properties or using the ``@Attributes`` and
|
||||||
|
``@Attribute`` annotations.
|
||||||
|
|
||||||
|
If the data type does not match you get an ``AnnotationException``
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
namespace MyCompany\Annotations;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Annotation
|
||||||
|
* @Target({"METHOD","PROPERTY"})
|
||||||
|
*/
|
||||||
|
class Bar
|
||||||
|
{
|
||||||
|
/** @var mixed */
|
||||||
|
public $mixed;
|
||||||
|
|
||||||
|
/** @var boolean */
|
||||||
|
public $boolean;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
public $bool;
|
||||||
|
|
||||||
|
/** @var float */
|
||||||
|
public $float;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
public $string;
|
||||||
|
|
||||||
|
/** @var integer */
|
||||||
|
public $integer;
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
public $array;
|
||||||
|
|
||||||
|
/** @var SomeAnnotationClass */
|
||||||
|
public $annotation;
|
||||||
|
|
||||||
|
/** @var array<integer> */
|
||||||
|
public $arrayOfIntegers;
|
||||||
|
|
||||||
|
/** @var array<SomeAnnotationClass> */
|
||||||
|
public $arrayOfAnnotations;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Annotation
|
||||||
|
* @Target({"METHOD","PROPERTY"})
|
||||||
|
* @Attributes({
|
||||||
|
* @Attribute("stringProperty", type = "string"),
|
||||||
|
* @Attribute("annotProperty", type = "SomeAnnotationClass"),
|
||||||
|
* })
|
||||||
|
*/
|
||||||
|
class Foo
|
||||||
|
{
|
||||||
|
public function __construct(array $values)
|
||||||
|
{
|
||||||
|
$this->stringProperty = $values['stringProperty'];
|
||||||
|
$this->annotProperty = $values['annotProperty'];
|
||||||
|
}
|
||||||
|
|
||||||
|
// some code
|
||||||
|
}
|
||||||
|
|
||||||
|
Annotation Required
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
``@Required`` indicates that the field must be specified when the
|
||||||
|
annotation is used. If it is not used you get an ``AnnotationException``
|
||||||
|
stating that this value can not be null.
|
||||||
|
|
||||||
|
Declaring a required field:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Annotation
|
||||||
|
* @Target("ALL")
|
||||||
|
*/
|
||||||
|
class Foo
|
||||||
|
{
|
||||||
|
/** @Required */
|
||||||
|
public $requiredField;
|
||||||
|
}
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
/** @Foo(requiredField="value") */
|
||||||
|
public $direction; // Valid
|
||||||
|
|
||||||
|
/** @Foo */
|
||||||
|
public $direction; // Required field missing, throws an AnnotationException
|
||||||
|
|
||||||
|
|
||||||
|
Enumerated values
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
- An annotation property marked with ``@Enum`` is a field that accepts a
|
||||||
|
fixed set of scalar values.
|
||||||
|
- You should use ``@Enum`` fields any time you need to represent fixed
|
||||||
|
values.
|
||||||
|
- The annotation parser checks the given value and throws an
|
||||||
|
``AnnotationException`` if the value does not match.
|
||||||
|
|
||||||
|
|
||||||
|
Declaring an enumerated property:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Annotation
|
||||||
|
* @Target("ALL")
|
||||||
|
*/
|
||||||
|
class Direction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @Enum({"NORTH", "SOUTH", "EAST", "WEST"})
|
||||||
|
*/
|
||||||
|
public $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
Annotation usage:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
/** @Direction("NORTH") */
|
||||||
|
public $direction; // Valid value
|
||||||
|
|
||||||
|
/** @Direction("NORTHEAST") */
|
||||||
|
public $direction; // Invalid value, throws an AnnotationException
|
||||||
|
|
||||||
|
|
||||||
|
Constants
|
||||||
|
---------
|
||||||
|
|
||||||
|
The use of constants and class constants is available on the annotations
|
||||||
|
parser.
|
||||||
|
|
||||||
|
The following usages are allowed:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
namespace MyCompany\Entity;
|
||||||
|
|
||||||
|
use MyCompany\Annotations\Foo;
|
||||||
|
use MyCompany\Annotations\Bar;
|
||||||
|
use MyCompany\Entity\SomeClass;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Foo(PHP_EOL)
|
||||||
|
* @Bar(Bar::FOO)
|
||||||
|
* @Foo({SomeClass::FOO, SomeClass::BAR})
|
||||||
|
* @Bar({SomeClass::FOO_KEY = SomeClass::BAR_VALUE})
|
||||||
|
*/
|
||||||
|
class User
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Be careful with constants and the cache !
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
The cached reader will not re-evaluate each time an annotation is
|
||||||
|
loaded from cache. When a constant is changed the cache must be
|
||||||
|
cleaned.
|
||||||
|
|
||||||
|
|
||||||
|
Usage
|
||||||
|
-----
|
||||||
|
|
||||||
|
Using the library API is simple. Using the annotations described in the
|
||||||
|
previous section, you can now annotate other classes with your
|
||||||
|
annotations:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
namespace MyCompany\Entity;
|
||||||
|
|
||||||
|
use MyCompany\Annotations\Foo;
|
||||||
|
use MyCompany\Annotations\Bar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Foo(bar="foo")
|
||||||
|
* @Bar(foo="bar")
|
||||||
|
*/
|
||||||
|
class User
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Now we can write a script to get the annotations above:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
$reflClass = new ReflectionClass('MyCompany\Entity\User');
|
||||||
|
$classAnnotations = $reader->getClassAnnotations($reflClass);
|
||||||
|
|
||||||
|
foreach ($classAnnotations AS $annot) {
|
||||||
|
if ($annot instanceof \MyCompany\Annotations\Foo) {
|
||||||
|
echo $annot->bar; // prints "foo";
|
||||||
|
} else if ($annot instanceof \MyCompany\Annotations\Bar) {
|
||||||
|
echo $annot->foo; // prints "bar";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
You have a complete API for retrieving annotation class instances from a
|
||||||
|
class, property or method docblock:
|
||||||
|
|
||||||
|
|
||||||
|
Reader API
|
||||||
|
~~~~~~~~~~
|
||||||
|
|
||||||
|
Access all annotations of a class
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
public function getClassAnnotations(\ReflectionClass $class);
|
||||||
|
|
||||||
|
Access one annotation of a class
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
public function getClassAnnotation(\ReflectionClass $class, $annotationName);
|
||||||
|
|
||||||
|
Access all annotations of a method
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
public function getMethodAnnotations(\ReflectionMethod $method);
|
||||||
|
|
||||||
|
Access one annotation of a method
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
public function getMethodAnnotation(\ReflectionMethod $method, $annotationName);
|
||||||
|
|
||||||
|
Access all annotations of a property
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
public function getPropertyAnnotations(\ReflectionProperty $property);
|
||||||
|
|
||||||
|
Access one annotation of a property
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName);
|
||||||
|
|
||||||
|
Access all annotations of a function
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
public function getFunctionAnnotations(\ReflectionFunction $property);
|
||||||
|
|
||||||
|
Access one annotation of a function
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
public function getFunctionAnnotation(\ReflectionFunction $property, $annotationName);
|
@ -0,0 +1,6 @@
|
|||||||
|
.. toctree::
|
||||||
|
:depth: 3
|
||||||
|
|
||||||
|
index
|
||||||
|
annotations
|
||||||
|
custom
|
@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations;
|
||||||
|
|
||||||
|
use BadMethodCallException;
|
||||||
|
|
||||||
|
use function sprintf;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Annotations class.
|
||||||
|
*/
|
||||||
|
class Annotation
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Value property. Common among all derived classes.
|
||||||
|
*
|
||||||
|
* @var mixed
|
||||||
|
*/
|
||||||
|
public $value;
|
||||||
|
|
||||||
|
/** @param array<string, mixed> $data Key-value for properties to be defined in this class. */
|
||||||
|
final public function __construct(array $data)
|
||||||
|
{
|
||||||
|
foreach ($data as $key => $value) {
|
||||||
|
$this->$key = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error handler for unknown property accessor in Annotation class.
|
||||||
|
*
|
||||||
|
* @param string $name Unknown property name.
|
||||||
|
*
|
||||||
|
* @throws BadMethodCallException
|
||||||
|
*/
|
||||||
|
public function __get($name)
|
||||||
|
{
|
||||||
|
throw new BadMethodCallException(
|
||||||
|
sprintf("Unknown property '%s' on annotation '%s'.", $name, static::class)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error handler for unknown property mutator in Annotation class.
|
||||||
|
*
|
||||||
|
* @param string $name Unknown property name.
|
||||||
|
* @param mixed $value Property value.
|
||||||
|
*
|
||||||
|
* @throws BadMethodCallException
|
||||||
|
*/
|
||||||
|
public function __set($name, $value)
|
||||||
|
{
|
||||||
|
throw new BadMethodCallException(
|
||||||
|
sprintf("Unknown property '%s' on annotation '%s'.", $name, static::class)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations\Annotation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Annotation that can be used to signal to the parser
|
||||||
|
* to check the attribute type during the parsing process.
|
||||||
|
*
|
||||||
|
* @Annotation
|
||||||
|
*/
|
||||||
|
final class Attribute
|
||||||
|
{
|
||||||
|
/** @var string */
|
||||||
|
public $name;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
public $type;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
public $required = false;
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations\Annotation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Annotation that can be used to signal to the parser
|
||||||
|
* to check the types of all declared attributes during the parsing process.
|
||||||
|
*
|
||||||
|
* @Annotation
|
||||||
|
*/
|
||||||
|
final class Attributes
|
||||||
|
{
|
||||||
|
/** @var array<Attribute> */
|
||||||
|
public $value;
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations\Annotation;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
|
use function get_class;
|
||||||
|
use function gettype;
|
||||||
|
use function in_array;
|
||||||
|
use function is_object;
|
||||||
|
use function is_scalar;
|
||||||
|
use function sprintf;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Annotation that can be used to signal to the parser
|
||||||
|
* to check the available values during the parsing process.
|
||||||
|
*
|
||||||
|
* @Annotation
|
||||||
|
* @Attributes({
|
||||||
|
* @Attribute("value", required = true, type = "array"),
|
||||||
|
* @Attribute("literal", required = false, type = "array")
|
||||||
|
* })
|
||||||
|
*/
|
||||||
|
final class Enum
|
||||||
|
{
|
||||||
|
/** @phpstan-var list<scalar> */
|
||||||
|
public $value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Literal target declaration.
|
||||||
|
*
|
||||||
|
* @var mixed[]
|
||||||
|
*/
|
||||||
|
public $literal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phpstan-param array{literal?: mixed[], value: list<scalar>} $values
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function __construct(array $values)
|
||||||
|
{
|
||||||
|
if (! isset($values['literal'])) {
|
||||||
|
$values['literal'] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($values['value'] as $var) {
|
||||||
|
if (! is_scalar($var)) {
|
||||||
|
throw new InvalidArgumentException(sprintf(
|
||||||
|
'@Enum supports only scalar values "%s" given.',
|
||||||
|
is_object($var) ? get_class($var) : gettype($var)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($values['literal'] as $key => $var) {
|
||||||
|
if (! in_array($key, $values['value'])) {
|
||||||
|
throw new InvalidArgumentException(sprintf(
|
||||||
|
'Undefined enumerator value "%s" for literal "%s".',
|
||||||
|
$key,
|
||||||
|
$var
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->value = $values['value'];
|
||||||
|
$this->literal = $values['literal'];
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations\Annotation;
|
||||||
|
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
use function is_array;
|
||||||
|
use function is_string;
|
||||||
|
use function json_encode;
|
||||||
|
use function sprintf;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Annotation that can be used to signal to the parser to ignore specific
|
||||||
|
* annotations during the parsing process.
|
||||||
|
*
|
||||||
|
* @Annotation
|
||||||
|
*/
|
||||||
|
final class IgnoreAnnotation
|
||||||
|
{
|
||||||
|
/** @phpstan-var list<string> */
|
||||||
|
public $names;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phpstan-param array{value: string|list<string>} $values
|
||||||
|
*
|
||||||
|
* @throws RuntimeException
|
||||||
|
*/
|
||||||
|
public function __construct(array $values)
|
||||||
|
{
|
||||||
|
if (is_string($values['value'])) {
|
||||||
|
$values['value'] = [$values['value']];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! is_array($values['value'])) {
|
||||||
|
throw new RuntimeException(sprintf(
|
||||||
|
'@IgnoreAnnotation expects either a string name, or an array of strings, but got %s.',
|
||||||
|
json_encode($values['value'])
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->names = $values['value'];
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations\Annotation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Annotation that indicates that the annotated class should be constructed with a named argument call.
|
||||||
|
*
|
||||||
|
* @Annotation
|
||||||
|
* @Target("CLASS")
|
||||||
|
*/
|
||||||
|
final class NamedArgumentConstructor
|
||||||
|
{
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations\Annotation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Annotation that can be used to signal to the parser
|
||||||
|
* to check if that attribute is required during the parsing process.
|
||||||
|
*
|
||||||
|
* @Annotation
|
||||||
|
*/
|
||||||
|
final class Required
|
||||||
|
{
|
||||||
|
}
|
@ -0,0 +1,101 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations\Annotation;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
|
use function array_keys;
|
||||||
|
use function get_class;
|
||||||
|
use function gettype;
|
||||||
|
use function implode;
|
||||||
|
use function is_array;
|
||||||
|
use function is_object;
|
||||||
|
use function is_string;
|
||||||
|
use function sprintf;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Annotation that can be used to signal to the parser
|
||||||
|
* to check the annotation target during the parsing process.
|
||||||
|
*
|
||||||
|
* @Annotation
|
||||||
|
*/
|
||||||
|
final class Target
|
||||||
|
{
|
||||||
|
public const TARGET_CLASS = 1;
|
||||||
|
public const TARGET_METHOD = 2;
|
||||||
|
public const TARGET_PROPERTY = 4;
|
||||||
|
public const TARGET_ANNOTATION = 8;
|
||||||
|
public const TARGET_FUNCTION = 16;
|
||||||
|
public const TARGET_ALL = 31;
|
||||||
|
|
||||||
|
/** @var array<string, int> */
|
||||||
|
private static $map = [
|
||||||
|
'ALL' => self::TARGET_ALL,
|
||||||
|
'CLASS' => self::TARGET_CLASS,
|
||||||
|
'METHOD' => self::TARGET_METHOD,
|
||||||
|
'PROPERTY' => self::TARGET_PROPERTY,
|
||||||
|
'FUNCTION' => self::TARGET_FUNCTION,
|
||||||
|
'ANNOTATION' => self::TARGET_ANNOTATION,
|
||||||
|
];
|
||||||
|
|
||||||
|
/** @phpstan-var list<string> */
|
||||||
|
public $value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Targets as bitmask.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
public $targets;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Literal target declaration.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $literal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phpstan-param array{value?: string|list<string>} $values
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function __construct(array $values)
|
||||||
|
{
|
||||||
|
if (! isset($values['value'])) {
|
||||||
|
$values['value'] = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_string($values['value'])) {
|
||||||
|
$values['value'] = [$values['value']];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! is_array($values['value'])) {
|
||||||
|
throw new InvalidArgumentException(
|
||||||
|
sprintf(
|
||||||
|
'@Target expects either a string value, or an array of strings, "%s" given.',
|
||||||
|
is_object($values['value']) ? get_class($values['value']) : gettype($values['value'])
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$bitmask = 0;
|
||||||
|
foreach ($values['value'] as $literal) {
|
||||||
|
if (! isset(self::$map[$literal])) {
|
||||||
|
throw new InvalidArgumentException(
|
||||||
|
sprintf(
|
||||||
|
'Invalid Target "%s". Available targets: [%s]',
|
||||||
|
$literal,
|
||||||
|
implode(', ', array_keys(self::$map))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$bitmask |= self::$map[$literal];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->targets = $bitmask;
|
||||||
|
$this->value = $values['value'];
|
||||||
|
$this->literal = implode(', ', $this->value);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,167 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
use function get_class;
|
||||||
|
use function gettype;
|
||||||
|
use function implode;
|
||||||
|
use function is_object;
|
||||||
|
use function sprintf;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description of AnnotationException
|
||||||
|
*/
|
||||||
|
class AnnotationException extends Exception
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Creates a new AnnotationException describing a Syntax error.
|
||||||
|
*
|
||||||
|
* @param string $message Exception message
|
||||||
|
*
|
||||||
|
* @return AnnotationException
|
||||||
|
*/
|
||||||
|
public static function syntaxError($message)
|
||||||
|
{
|
||||||
|
return new self('[Syntax Error] ' . $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new AnnotationException describing a Semantical error.
|
||||||
|
*
|
||||||
|
* @param string $message Exception message
|
||||||
|
*
|
||||||
|
* @return AnnotationException
|
||||||
|
*/
|
||||||
|
public static function semanticalError($message)
|
||||||
|
{
|
||||||
|
return new self('[Semantical Error] ' . $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new AnnotationException describing an error which occurred during
|
||||||
|
* the creation of the annotation.
|
||||||
|
*
|
||||||
|
* @param string $message
|
||||||
|
*
|
||||||
|
* @return AnnotationException
|
||||||
|
*/
|
||||||
|
public static function creationError($message, ?Throwable $previous = null)
|
||||||
|
{
|
||||||
|
return new self('[Creation Error] ' . $message, 0, $previous);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new AnnotationException describing a type error.
|
||||||
|
*
|
||||||
|
* @param string $message
|
||||||
|
*
|
||||||
|
* @return AnnotationException
|
||||||
|
*/
|
||||||
|
public static function typeError($message)
|
||||||
|
{
|
||||||
|
return new self('[Type Error] ' . $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new AnnotationException describing a constant semantical error.
|
||||||
|
*
|
||||||
|
* @param string $identifier
|
||||||
|
* @param string $context
|
||||||
|
*
|
||||||
|
* @return AnnotationException
|
||||||
|
*/
|
||||||
|
public static function semanticalErrorConstants($identifier, $context = null)
|
||||||
|
{
|
||||||
|
return self::semanticalError(sprintf(
|
||||||
|
"Couldn't find constant %s%s.",
|
||||||
|
$identifier,
|
||||||
|
$context ? ', ' . $context : ''
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new AnnotationException describing an type error of an attribute.
|
||||||
|
*
|
||||||
|
* @param string $attributeName
|
||||||
|
* @param string $annotationName
|
||||||
|
* @param string $context
|
||||||
|
* @param string $expected
|
||||||
|
* @param mixed $actual
|
||||||
|
*
|
||||||
|
* @return AnnotationException
|
||||||
|
*/
|
||||||
|
public static function attributeTypeError($attributeName, $annotationName, $context, $expected, $actual)
|
||||||
|
{
|
||||||
|
return self::typeError(sprintf(
|
||||||
|
'Attribute "%s" of @%s declared on %s expects %s, but got %s.',
|
||||||
|
$attributeName,
|
||||||
|
$annotationName,
|
||||||
|
$context,
|
||||||
|
$expected,
|
||||||
|
is_object($actual) ? 'an instance of ' . get_class($actual) : gettype($actual)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new AnnotationException describing an required error of an attribute.
|
||||||
|
*
|
||||||
|
* @param string $attributeName
|
||||||
|
* @param string $annotationName
|
||||||
|
* @param string $context
|
||||||
|
* @param string $expected
|
||||||
|
*
|
||||||
|
* @return AnnotationException
|
||||||
|
*/
|
||||||
|
public static function requiredError($attributeName, $annotationName, $context, $expected)
|
||||||
|
{
|
||||||
|
return self::typeError(sprintf(
|
||||||
|
'Attribute "%s" of @%s declared on %s expects %s. This value should not be null.',
|
||||||
|
$attributeName,
|
||||||
|
$annotationName,
|
||||||
|
$context,
|
||||||
|
$expected
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new AnnotationException describing a invalid enummerator.
|
||||||
|
*
|
||||||
|
* @param string $attributeName
|
||||||
|
* @param string $annotationName
|
||||||
|
* @param string $context
|
||||||
|
* @param mixed $given
|
||||||
|
* @phpstan-param list<string> $available
|
||||||
|
*
|
||||||
|
* @return AnnotationException
|
||||||
|
*/
|
||||||
|
public static function enumeratorError($attributeName, $annotationName, $context, $available, $given)
|
||||||
|
{
|
||||||
|
return new self(sprintf(
|
||||||
|
'[Enum Error] Attribute "%s" of @%s declared on %s accepts only [%s], but got %s.',
|
||||||
|
$attributeName,
|
||||||
|
$annotationName,
|
||||||
|
$context,
|
||||||
|
implode(', ', $available),
|
||||||
|
is_object($given) ? get_class($given) : $given
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return AnnotationException */
|
||||||
|
public static function optimizerPlusSaveComments()
|
||||||
|
{
|
||||||
|
return new self(
|
||||||
|
'You have to enable opcache.save_comments=1 or zend_optimizerplus.save_comments=1.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return AnnotationException */
|
||||||
|
public static function optimizerPlusLoadComments()
|
||||||
|
{
|
||||||
|
return new self(
|
||||||
|
'You have to enable opcache.load_comments=1 or zend_optimizerplus.load_comments=1.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,389 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations;
|
||||||
|
|
||||||
|
use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
|
||||||
|
use Doctrine\Common\Annotations\Annotation\Target;
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionFunction;
|
||||||
|
use ReflectionMethod;
|
||||||
|
use ReflectionProperty;
|
||||||
|
|
||||||
|
use function array_merge;
|
||||||
|
use function class_exists;
|
||||||
|
use function extension_loaded;
|
||||||
|
use function ini_get;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A reader for docblock annotations.
|
||||||
|
*/
|
||||||
|
class AnnotationReader implements Reader
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Global map for imports.
|
||||||
|
*
|
||||||
|
* @var array<string, class-string>
|
||||||
|
*/
|
||||||
|
private static $globalImports = [
|
||||||
|
'ignoreannotation' => Annotation\IgnoreAnnotation::class,
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A list with annotations that are not causing exceptions when not resolved to an annotation class.
|
||||||
|
*
|
||||||
|
* The names are case sensitive.
|
||||||
|
*
|
||||||
|
* @var array<string, true>
|
||||||
|
*/
|
||||||
|
private static $globalIgnoredNames = ImplicitlyIgnoredAnnotationNames::LIST;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A list with annotations that are not causing exceptions when not resolved to an annotation class.
|
||||||
|
*
|
||||||
|
* The names are case sensitive.
|
||||||
|
*
|
||||||
|
* @var array<string, true>
|
||||||
|
*/
|
||||||
|
private static $globalIgnoredNamespaces = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a new annotation to the globally ignored annotation names with regard to exception handling.
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
*/
|
||||||
|
public static function addGlobalIgnoredName($name)
|
||||||
|
{
|
||||||
|
self::$globalIgnoredNames[$name] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a new annotation to the globally ignored annotation namespaces with regard to exception handling.
|
||||||
|
*
|
||||||
|
* @param string $namespace
|
||||||
|
*/
|
||||||
|
public static function addGlobalIgnoredNamespace($namespace)
|
||||||
|
{
|
||||||
|
self::$globalIgnoredNamespaces[$namespace] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Annotations parser.
|
||||||
|
*
|
||||||
|
* @var DocParser
|
||||||
|
*/
|
||||||
|
private $parser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Annotations parser used to collect parsing metadata.
|
||||||
|
*
|
||||||
|
* @var DocParser
|
||||||
|
*/
|
||||||
|
private $preParser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PHP parser used to collect imports.
|
||||||
|
*
|
||||||
|
* @var PhpParser
|
||||||
|
*/
|
||||||
|
private $phpParser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* In-memory cache mechanism to store imported annotations per class.
|
||||||
|
*
|
||||||
|
* @psalm-var array<'class'|'function', array<string, array<string, class-string>>>
|
||||||
|
*/
|
||||||
|
private $imports = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* In-memory cache mechanism to store ignored annotations per class.
|
||||||
|
*
|
||||||
|
* @psalm-var array<'class'|'function', array<string, array<string, true>>>
|
||||||
|
*/
|
||||||
|
private $ignoredAnnotationNames = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new AnnotationReader.
|
||||||
|
*
|
||||||
|
* @throws AnnotationException
|
||||||
|
*/
|
||||||
|
public function __construct(?DocParser $parser = null)
|
||||||
|
{
|
||||||
|
if (
|
||||||
|
extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.save_comments') === '0' ||
|
||||||
|
ini_get('opcache.save_comments') === '0')
|
||||||
|
) {
|
||||||
|
throw AnnotationException::optimizerPlusSaveComments();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extension_loaded('Zend OPcache') && ini_get('opcache.save_comments') === 0) {
|
||||||
|
throw AnnotationException::optimizerPlusSaveComments();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure that the IgnoreAnnotation annotation is loaded
|
||||||
|
class_exists(IgnoreAnnotation::class);
|
||||||
|
|
||||||
|
$this->parser = $parser ?: new DocParser();
|
||||||
|
|
||||||
|
$this->preParser = new DocParser();
|
||||||
|
|
||||||
|
$this->preParser->setImports(self::$globalImports);
|
||||||
|
$this->preParser->setIgnoreNotImportedAnnotations(true);
|
||||||
|
$this->preParser->setIgnoredAnnotationNames(self::$globalIgnoredNames);
|
||||||
|
|
||||||
|
$this->phpParser = new PhpParser();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getClassAnnotations(ReflectionClass $class)
|
||||||
|
{
|
||||||
|
$this->parser->setTarget(Target::TARGET_CLASS);
|
||||||
|
$this->parser->setImports($this->getImports($class));
|
||||||
|
$this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
|
||||||
|
$this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
|
||||||
|
|
||||||
|
return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getClassAnnotation(ReflectionClass $class, $annotationName)
|
||||||
|
{
|
||||||
|
$annotations = $this->getClassAnnotations($class);
|
||||||
|
|
||||||
|
foreach ($annotations as $annotation) {
|
||||||
|
if ($annotation instanceof $annotationName) {
|
||||||
|
return $annotation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getPropertyAnnotations(ReflectionProperty $property)
|
||||||
|
{
|
||||||
|
$class = $property->getDeclaringClass();
|
||||||
|
$context = 'property ' . $class->getName() . '::$' . $property->getName();
|
||||||
|
|
||||||
|
$this->parser->setTarget(Target::TARGET_PROPERTY);
|
||||||
|
$this->parser->setImports($this->getPropertyImports($property));
|
||||||
|
$this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
|
||||||
|
$this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
|
||||||
|
|
||||||
|
return $this->parser->parse($property->getDocComment(), $context);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
|
||||||
|
{
|
||||||
|
$annotations = $this->getPropertyAnnotations($property);
|
||||||
|
|
||||||
|
foreach ($annotations as $annotation) {
|
||||||
|
if ($annotation instanceof $annotationName) {
|
||||||
|
return $annotation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getMethodAnnotations(ReflectionMethod $method)
|
||||||
|
{
|
||||||
|
$class = $method->getDeclaringClass();
|
||||||
|
$context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
|
||||||
|
|
||||||
|
$this->parser->setTarget(Target::TARGET_METHOD);
|
||||||
|
$this->parser->setImports($this->getMethodImports($method));
|
||||||
|
$this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
|
||||||
|
$this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
|
||||||
|
|
||||||
|
return $this->parser->parse($method->getDocComment(), $context);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
|
||||||
|
{
|
||||||
|
$annotations = $this->getMethodAnnotations($method);
|
||||||
|
|
||||||
|
foreach ($annotations as $annotation) {
|
||||||
|
if ($annotation instanceof $annotationName) {
|
||||||
|
return $annotation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the annotations applied to a function.
|
||||||
|
*
|
||||||
|
* @phpstan-return list<object> An array of Annotations.
|
||||||
|
*/
|
||||||
|
public function getFunctionAnnotations(ReflectionFunction $function): array
|
||||||
|
{
|
||||||
|
$context = 'function ' . $function->getName();
|
||||||
|
|
||||||
|
$this->parser->setTarget(Target::TARGET_FUNCTION);
|
||||||
|
$this->parser->setImports($this->getImports($function));
|
||||||
|
$this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($function));
|
||||||
|
$this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
|
||||||
|
|
||||||
|
return $this->parser->parse($function->getDocComment(), $context);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a function annotation.
|
||||||
|
*
|
||||||
|
* @return object|null The Annotation or NULL, if the requested annotation does not exist.
|
||||||
|
*/
|
||||||
|
public function getFunctionAnnotation(ReflectionFunction $function, string $annotationName)
|
||||||
|
{
|
||||||
|
$annotations = $this->getFunctionAnnotations($function);
|
||||||
|
|
||||||
|
foreach ($annotations as $annotation) {
|
||||||
|
if ($annotation instanceof $annotationName) {
|
||||||
|
return $annotation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the ignored annotations for the given class or function.
|
||||||
|
*
|
||||||
|
* @param ReflectionClass|ReflectionFunction $reflection
|
||||||
|
*
|
||||||
|
* @return array<string, true>
|
||||||
|
*/
|
||||||
|
private function getIgnoredAnnotationNames($reflection): array
|
||||||
|
{
|
||||||
|
$type = $reflection instanceof ReflectionClass ? 'class' : 'function';
|
||||||
|
$name = $reflection->getName();
|
||||||
|
|
||||||
|
if (isset($this->ignoredAnnotationNames[$type][$name])) {
|
||||||
|
return $this->ignoredAnnotationNames[$type][$name];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->collectParsingMetadata($reflection);
|
||||||
|
|
||||||
|
return $this->ignoredAnnotationNames[$type][$name];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves imports for a class or a function.
|
||||||
|
*
|
||||||
|
* @param ReflectionClass|ReflectionFunction $reflection
|
||||||
|
*
|
||||||
|
* @return array<string, class-string>
|
||||||
|
*/
|
||||||
|
private function getImports($reflection): array
|
||||||
|
{
|
||||||
|
$type = $reflection instanceof ReflectionClass ? 'class' : 'function';
|
||||||
|
$name = $reflection->getName();
|
||||||
|
|
||||||
|
if (isset($this->imports[$type][$name])) {
|
||||||
|
return $this->imports[$type][$name];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->collectParsingMetadata($reflection);
|
||||||
|
|
||||||
|
return $this->imports[$type][$name];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves imports for methods.
|
||||||
|
*
|
||||||
|
* @return array<string, class-string>
|
||||||
|
*/
|
||||||
|
private function getMethodImports(ReflectionMethod $method)
|
||||||
|
{
|
||||||
|
$class = $method->getDeclaringClass();
|
||||||
|
$classImports = $this->getImports($class);
|
||||||
|
|
||||||
|
$traitImports = [];
|
||||||
|
|
||||||
|
foreach ($class->getTraits() as $trait) {
|
||||||
|
if (
|
||||||
|
! $trait->hasMethod($method->getName())
|
||||||
|
|| $trait->getFileName() !== $method->getFileName()
|
||||||
|
) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$traitImports = array_merge($traitImports, $this->phpParser->parseUseStatements($trait));
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_merge($classImports, $traitImports);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves imports for properties.
|
||||||
|
*
|
||||||
|
* @return array<string, class-string>
|
||||||
|
*/
|
||||||
|
private function getPropertyImports(ReflectionProperty $property)
|
||||||
|
{
|
||||||
|
$class = $property->getDeclaringClass();
|
||||||
|
$classImports = $this->getImports($class);
|
||||||
|
|
||||||
|
$traitImports = [];
|
||||||
|
|
||||||
|
foreach ($class->getTraits() as $trait) {
|
||||||
|
if (! $trait->hasProperty($property->getName())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$traitImports = array_merge($traitImports, $this->phpParser->parseUseStatements($trait));
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_merge($classImports, $traitImports);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collects parsing metadata for a given class or function.
|
||||||
|
*
|
||||||
|
* @param ReflectionClass|ReflectionFunction $reflection
|
||||||
|
*/
|
||||||
|
private function collectParsingMetadata($reflection): void
|
||||||
|
{
|
||||||
|
$type = $reflection instanceof ReflectionClass ? 'class' : 'function';
|
||||||
|
$name = $reflection->getName();
|
||||||
|
|
||||||
|
$ignoredAnnotationNames = self::$globalIgnoredNames;
|
||||||
|
$annotations = $this->preParser->parse($reflection->getDocComment(), $type . ' ' . $name);
|
||||||
|
|
||||||
|
foreach ($annotations as $annotation) {
|
||||||
|
if (! ($annotation instanceof IgnoreAnnotation)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($annotation->names as $annot) {
|
||||||
|
$ignoredAnnotationNames[$annot] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->imports[$type][$name] = array_merge(
|
||||||
|
self::$globalImports,
|
||||||
|
$this->phpParser->parseUseStatements($reflection),
|
||||||
|
[
|
||||||
|
'__NAMESPACE__' => $reflection->getNamespaceName(),
|
||||||
|
'self' => $name,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->ignoredAnnotationNames[$type][$name] = $ignoredAnnotationNames;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,190 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations;
|
||||||
|
|
||||||
|
use function array_key_exists;
|
||||||
|
use function array_merge;
|
||||||
|
use function class_exists;
|
||||||
|
use function in_array;
|
||||||
|
use function is_file;
|
||||||
|
use function str_replace;
|
||||||
|
use function stream_resolve_include_path;
|
||||||
|
use function strpos;
|
||||||
|
|
||||||
|
use const DIRECTORY_SEPARATOR;
|
||||||
|
|
||||||
|
final class AnnotationRegistry
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A map of namespaces to use for autoloading purposes based on a PSR-0 convention.
|
||||||
|
*
|
||||||
|
* Contains the namespace as key and an array of directories as value. If the value is NULL
|
||||||
|
* the include path is used for checking for the corresponding file.
|
||||||
|
*
|
||||||
|
* This autoloading mechanism does not utilize the PHP autoloading but implements autoloading on its own.
|
||||||
|
*
|
||||||
|
* @var string[][]|string[]|null[]
|
||||||
|
*/
|
||||||
|
private static $autoloadNamespaces = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A map of autoloader callables.
|
||||||
|
*
|
||||||
|
* @var callable[]
|
||||||
|
*/
|
||||||
|
private static $loaders = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An array of classes which cannot be found
|
||||||
|
*
|
||||||
|
* @var null[] indexed by class name
|
||||||
|
*/
|
||||||
|
private static $failedToAutoload = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whenever registerFile() was used. Disables use of standard autoloader.
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private static $registerFileUsed = false;
|
||||||
|
|
||||||
|
public static function reset(): void
|
||||||
|
{
|
||||||
|
self::$autoloadNamespaces = [];
|
||||||
|
self::$loaders = [];
|
||||||
|
self::$failedToAutoload = [];
|
||||||
|
self::$registerFileUsed = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers file.
|
||||||
|
*
|
||||||
|
* @deprecated This method is deprecated and will be removed in
|
||||||
|
* doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
|
||||||
|
*/
|
||||||
|
public static function registerFile(string $file): void
|
||||||
|
{
|
||||||
|
self::$registerFileUsed = true;
|
||||||
|
|
||||||
|
require_once $file;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a namespace with one or many directories to look for files or null for the include path.
|
||||||
|
*
|
||||||
|
* Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
|
||||||
|
*
|
||||||
|
* @deprecated This method is deprecated and will be removed in
|
||||||
|
* doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
|
||||||
|
*
|
||||||
|
* @phpstan-param string|list<string>|null $dirs
|
||||||
|
*/
|
||||||
|
public static function registerAutoloadNamespace(string $namespace, $dirs = null): void
|
||||||
|
{
|
||||||
|
self::$autoloadNamespaces[$namespace] = $dirs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers multiple namespaces.
|
||||||
|
*
|
||||||
|
* Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
|
||||||
|
*
|
||||||
|
* @deprecated This method is deprecated and will be removed in
|
||||||
|
* doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
|
||||||
|
*
|
||||||
|
* @param string[][]|string[]|null[] $namespaces indexed by namespace name
|
||||||
|
*/
|
||||||
|
public static function registerAutoloadNamespaces(array $namespaces): void
|
||||||
|
{
|
||||||
|
self::$autoloadNamespaces = array_merge(self::$autoloadNamespaces, $namespaces);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers an autoloading callable for annotations, much like spl_autoload_register().
|
||||||
|
*
|
||||||
|
* NOTE: These class loaders HAVE to be silent when a class was not found!
|
||||||
|
* IMPORTANT: Loaders have to return true if they loaded a class that could contain the searched annotation class.
|
||||||
|
*
|
||||||
|
* @deprecated This method is deprecated and will be removed in
|
||||||
|
* doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
|
||||||
|
*/
|
||||||
|
public static function registerLoader(callable $callable): void
|
||||||
|
{
|
||||||
|
// Reset our static cache now that we have a new loader to work with
|
||||||
|
self::$failedToAutoload = [];
|
||||||
|
self::$loaders[] = $callable;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers an autoloading callable for annotations, if it is not already registered
|
||||||
|
*
|
||||||
|
* @deprecated This method is deprecated and will be removed in
|
||||||
|
* doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
|
||||||
|
*/
|
||||||
|
public static function registerUniqueLoader(callable $callable): void
|
||||||
|
{
|
||||||
|
if (in_array($callable, self::$loaders, true)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self::registerLoader($callable);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Autoloads an annotation class silently.
|
||||||
|
*/
|
||||||
|
public static function loadAnnotationClass(string $class): bool
|
||||||
|
{
|
||||||
|
if (class_exists($class, false)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (array_key_exists($class, self::$failedToAutoload)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (self::$autoloadNamespaces as $namespace => $dirs) {
|
||||||
|
if (strpos($class, $namespace) !== 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$file = str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
|
||||||
|
|
||||||
|
if ($dirs === null) {
|
||||||
|
$path = stream_resolve_include_path($file);
|
||||||
|
if ($path) {
|
||||||
|
require $path;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
foreach ((array) $dirs as $dir) {
|
||||||
|
if (is_file($dir . DIRECTORY_SEPARATOR . $file)) {
|
||||||
|
require $dir . DIRECTORY_SEPARATOR . $file;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (self::$loaders as $loader) {
|
||||||
|
if ($loader($class) === true) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
self::$loaders === [] &&
|
||||||
|
self::$autoloadNamespaces === [] &&
|
||||||
|
self::$registerFileUsed === false &&
|
||||||
|
class_exists($class)
|
||||||
|
) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
self::$failedToAutoload[$class] = null;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,266 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations;
|
||||||
|
|
||||||
|
use Doctrine\Common\Cache\Cache;
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionMethod;
|
||||||
|
use ReflectionProperty;
|
||||||
|
|
||||||
|
use function array_map;
|
||||||
|
use function array_merge;
|
||||||
|
use function assert;
|
||||||
|
use function filemtime;
|
||||||
|
use function max;
|
||||||
|
use function time;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A cache aware annotation reader.
|
||||||
|
*
|
||||||
|
* @deprecated the CachedReader is deprecated and will be removed
|
||||||
|
* in version 2.0.0 of doctrine/annotations. Please use the
|
||||||
|
* {@see \Doctrine\Common\Annotations\PsrCachedReader} instead.
|
||||||
|
*/
|
||||||
|
final class CachedReader implements Reader
|
||||||
|
{
|
||||||
|
/** @var Reader */
|
||||||
|
private $delegate;
|
||||||
|
|
||||||
|
/** @var Cache */
|
||||||
|
private $cache;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
private $debug;
|
||||||
|
|
||||||
|
/** @var array<string, array<object>> */
|
||||||
|
private $loadedAnnotations = [];
|
||||||
|
|
||||||
|
/** @var int[] */
|
||||||
|
private $loadedFilemtimes = [];
|
||||||
|
|
||||||
|
/** @param bool $debug */
|
||||||
|
public function __construct(Reader $reader, Cache $cache, $debug = false)
|
||||||
|
{
|
||||||
|
$this->delegate = $reader;
|
||||||
|
$this->cache = $cache;
|
||||||
|
$this->debug = (bool) $debug;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getClassAnnotations(ReflectionClass $class)
|
||||||
|
{
|
||||||
|
$cacheKey = $class->getName();
|
||||||
|
|
||||||
|
if (isset($this->loadedAnnotations[$cacheKey])) {
|
||||||
|
return $this->loadedAnnotations[$cacheKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
$annots = $this->fetchFromCache($cacheKey, $class);
|
||||||
|
if ($annots === false) {
|
||||||
|
$annots = $this->delegate->getClassAnnotations($class);
|
||||||
|
$this->saveToCache($cacheKey, $annots);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->loadedAnnotations[$cacheKey] = $annots;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getClassAnnotation(ReflectionClass $class, $annotationName)
|
||||||
|
{
|
||||||
|
foreach ($this->getClassAnnotations($class) as $annot) {
|
||||||
|
if ($annot instanceof $annotationName) {
|
||||||
|
return $annot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getPropertyAnnotations(ReflectionProperty $property)
|
||||||
|
{
|
||||||
|
$class = $property->getDeclaringClass();
|
||||||
|
$cacheKey = $class->getName() . '$' . $property->getName();
|
||||||
|
|
||||||
|
if (isset($this->loadedAnnotations[$cacheKey])) {
|
||||||
|
return $this->loadedAnnotations[$cacheKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
$annots = $this->fetchFromCache($cacheKey, $class);
|
||||||
|
if ($annots === false) {
|
||||||
|
$annots = $this->delegate->getPropertyAnnotations($property);
|
||||||
|
$this->saveToCache($cacheKey, $annots);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->loadedAnnotations[$cacheKey] = $annots;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
|
||||||
|
{
|
||||||
|
foreach ($this->getPropertyAnnotations($property) as $annot) {
|
||||||
|
if ($annot instanceof $annotationName) {
|
||||||
|
return $annot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getMethodAnnotations(ReflectionMethod $method)
|
||||||
|
{
|
||||||
|
$class = $method->getDeclaringClass();
|
||||||
|
$cacheKey = $class->getName() . '#' . $method->getName();
|
||||||
|
|
||||||
|
if (isset($this->loadedAnnotations[$cacheKey])) {
|
||||||
|
return $this->loadedAnnotations[$cacheKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
$annots = $this->fetchFromCache($cacheKey, $class);
|
||||||
|
if ($annots === false) {
|
||||||
|
$annots = $this->delegate->getMethodAnnotations($method);
|
||||||
|
$this->saveToCache($cacheKey, $annots);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->loadedAnnotations[$cacheKey] = $annots;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
|
||||||
|
{
|
||||||
|
foreach ($this->getMethodAnnotations($method) as $annot) {
|
||||||
|
if ($annot instanceof $annotationName) {
|
||||||
|
return $annot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears loaded annotations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function clearLoadedAnnotations()
|
||||||
|
{
|
||||||
|
$this->loadedAnnotations = [];
|
||||||
|
$this->loadedFilemtimes = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches a value from the cache.
|
||||||
|
*
|
||||||
|
* @param string $cacheKey The cache key.
|
||||||
|
*
|
||||||
|
* @return mixed The cached value or false when the value is not in cache.
|
||||||
|
*/
|
||||||
|
private function fetchFromCache($cacheKey, ReflectionClass $class)
|
||||||
|
{
|
||||||
|
$data = $this->cache->fetch($cacheKey);
|
||||||
|
if ($data !== false) {
|
||||||
|
if (! $this->debug || $this->isCacheFresh($cacheKey, $class)) {
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves a value to the cache.
|
||||||
|
*
|
||||||
|
* @param string $cacheKey The cache key.
|
||||||
|
* @param mixed $value The value.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function saveToCache($cacheKey, $value)
|
||||||
|
{
|
||||||
|
$this->cache->save($cacheKey, $value);
|
||||||
|
if (! $this->debug) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->cache->save('[C]' . $cacheKey, time());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the cache is fresh.
|
||||||
|
*
|
||||||
|
* @param string $cacheKey
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function isCacheFresh($cacheKey, ReflectionClass $class)
|
||||||
|
{
|
||||||
|
$lastModification = $this->getLastModification($class);
|
||||||
|
if ($lastModification === 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->cache->fetch('[C]' . $cacheKey) >= $lastModification;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the time the class was last modified, testing traits and parents
|
||||||
|
*/
|
||||||
|
private function getLastModification(ReflectionClass $class): int
|
||||||
|
{
|
||||||
|
$filename = $class->getFileName();
|
||||||
|
|
||||||
|
if (isset($this->loadedFilemtimes[$filename])) {
|
||||||
|
return $this->loadedFilemtimes[$filename];
|
||||||
|
}
|
||||||
|
|
||||||
|
$parent = $class->getParentClass();
|
||||||
|
|
||||||
|
$lastModification = max(array_merge(
|
||||||
|
[$filename ? filemtime($filename) : 0],
|
||||||
|
array_map(function (ReflectionClass $reflectionTrait): int {
|
||||||
|
return $this->getTraitLastModificationTime($reflectionTrait);
|
||||||
|
}, $class->getTraits()),
|
||||||
|
array_map(function (ReflectionClass $class): int {
|
||||||
|
return $this->getLastModification($class);
|
||||||
|
}, $class->getInterfaces()),
|
||||||
|
$parent ? [$this->getLastModification($parent)] : []
|
||||||
|
));
|
||||||
|
|
||||||
|
assert($lastModification !== false);
|
||||||
|
|
||||||
|
return $this->loadedFilemtimes[$filename] = $lastModification;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getTraitLastModificationTime(ReflectionClass $reflectionTrait): int
|
||||||
|
{
|
||||||
|
$fileName = $reflectionTrait->getFileName();
|
||||||
|
|
||||||
|
if (isset($this->loadedFilemtimes[$fileName])) {
|
||||||
|
return $this->loadedFilemtimes[$fileName];
|
||||||
|
}
|
||||||
|
|
||||||
|
$lastModificationTime = max(array_merge(
|
||||||
|
[$fileName ? filemtime($fileName) : 0],
|
||||||
|
array_map(function (ReflectionClass $reflectionTrait): int {
|
||||||
|
return $this->getTraitLastModificationTime($reflectionTrait);
|
||||||
|
}, $reflectionTrait->getTraits())
|
||||||
|
));
|
||||||
|
|
||||||
|
assert($lastModificationTime !== false);
|
||||||
|
|
||||||
|
return $this->loadedFilemtimes[$fileName] = $lastModificationTime;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,139 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations;
|
||||||
|
|
||||||
|
use Doctrine\Common\Lexer\AbstractLexer;
|
||||||
|
|
||||||
|
use function ctype_alpha;
|
||||||
|
use function is_numeric;
|
||||||
|
use function str_replace;
|
||||||
|
use function stripos;
|
||||||
|
use function strlen;
|
||||||
|
use function strpos;
|
||||||
|
use function strtolower;
|
||||||
|
use function substr;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple lexer for docblock annotations.
|
||||||
|
*
|
||||||
|
* @template-extends AbstractLexer<DocLexer::T_*>
|
||||||
|
*/
|
||||||
|
final class DocLexer extends AbstractLexer
|
||||||
|
{
|
||||||
|
public const T_NONE = 1;
|
||||||
|
public const T_INTEGER = 2;
|
||||||
|
public const T_STRING = 3;
|
||||||
|
public const T_FLOAT = 4;
|
||||||
|
|
||||||
|
// All tokens that are also identifiers should be >= 100
|
||||||
|
public const T_IDENTIFIER = 100;
|
||||||
|
public const T_AT = 101;
|
||||||
|
public const T_CLOSE_CURLY_BRACES = 102;
|
||||||
|
public const T_CLOSE_PARENTHESIS = 103;
|
||||||
|
public const T_COMMA = 104;
|
||||||
|
public const T_EQUALS = 105;
|
||||||
|
public const T_FALSE = 106;
|
||||||
|
public const T_NAMESPACE_SEPARATOR = 107;
|
||||||
|
public const T_OPEN_CURLY_BRACES = 108;
|
||||||
|
public const T_OPEN_PARENTHESIS = 109;
|
||||||
|
public const T_TRUE = 110;
|
||||||
|
public const T_NULL = 111;
|
||||||
|
public const T_COLON = 112;
|
||||||
|
public const T_MINUS = 113;
|
||||||
|
|
||||||
|
/** @var array<string, self::T*> */
|
||||||
|
protected $noCase = [
|
||||||
|
'@' => self::T_AT,
|
||||||
|
',' => self::T_COMMA,
|
||||||
|
'(' => self::T_OPEN_PARENTHESIS,
|
||||||
|
')' => self::T_CLOSE_PARENTHESIS,
|
||||||
|
'{' => self::T_OPEN_CURLY_BRACES,
|
||||||
|
'}' => self::T_CLOSE_CURLY_BRACES,
|
||||||
|
'=' => self::T_EQUALS,
|
||||||
|
':' => self::T_COLON,
|
||||||
|
'-' => self::T_MINUS,
|
||||||
|
'\\' => self::T_NAMESPACE_SEPARATOR,
|
||||||
|
];
|
||||||
|
|
||||||
|
/** @var array<string, self::T*> */
|
||||||
|
protected $withCase = [
|
||||||
|
'true' => self::T_TRUE,
|
||||||
|
'false' => self::T_FALSE,
|
||||||
|
'null' => self::T_NULL,
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the next token starts immediately, or if there were
|
||||||
|
* non-captured symbols before that
|
||||||
|
*/
|
||||||
|
public function nextTokenIsAdjacent(): bool
|
||||||
|
{
|
||||||
|
return $this->token === null
|
||||||
|
|| ($this->lookahead !== null
|
||||||
|
&& ($this->lookahead['position'] - $this->token['position']) === strlen($this->token['value']));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function getCatchablePatterns()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'[a-z_\\\][a-z0-9_\:\\\]*[a-z_][a-z0-9_]*',
|
||||||
|
'(?:[+-]?[0-9]+(?:[\.][0-9]+)*)(?:[eE][+-]?[0-9]+)?',
|
||||||
|
'"(?:""|[^"])*+"',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function getNonCatchablePatterns()
|
||||||
|
{
|
||||||
|
return ['\s+', '\*+', '(.)'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function getType(&$value)
|
||||||
|
{
|
||||||
|
$type = self::T_NONE;
|
||||||
|
|
||||||
|
if ($value[0] === '"') {
|
||||||
|
$value = str_replace('""', '"', substr($value, 1, strlen($value) - 2));
|
||||||
|
|
||||||
|
return self::T_STRING;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($this->noCase[$value])) {
|
||||||
|
return $this->noCase[$value];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($value[0] === '_' || $value[0] === '\\' || ctype_alpha($value[0])) {
|
||||||
|
return self::T_IDENTIFIER;
|
||||||
|
}
|
||||||
|
|
||||||
|
$lowerValue = strtolower($value);
|
||||||
|
|
||||||
|
if (isset($this->withCase[$lowerValue])) {
|
||||||
|
return $this->withCase[$lowerValue];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checking numeric value
|
||||||
|
if (is_numeric($value)) {
|
||||||
|
return strpos($value, '.') !== false || stripos($value, 'e') !== false
|
||||||
|
? self::T_FLOAT : self::T_INTEGER;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return array{value: int|string, type:self::T_*|null, position:int} */
|
||||||
|
public function peek(): array
|
||||||
|
{
|
||||||
|
$token = parent::peek();
|
||||||
|
|
||||||
|
return (array) $token;
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,315 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionMethod;
|
||||||
|
use ReflectionProperty;
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
use function chmod;
|
||||||
|
use function file_put_contents;
|
||||||
|
use function filemtime;
|
||||||
|
use function gettype;
|
||||||
|
use function is_dir;
|
||||||
|
use function is_file;
|
||||||
|
use function is_int;
|
||||||
|
use function is_writable;
|
||||||
|
use function mkdir;
|
||||||
|
use function rename;
|
||||||
|
use function rtrim;
|
||||||
|
use function serialize;
|
||||||
|
use function sha1;
|
||||||
|
use function sprintf;
|
||||||
|
use function strtr;
|
||||||
|
use function tempnam;
|
||||||
|
use function uniqid;
|
||||||
|
use function unlink;
|
||||||
|
use function var_export;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* File cache reader for annotations.
|
||||||
|
*
|
||||||
|
* @deprecated the FileCacheReader is deprecated and will be removed
|
||||||
|
* in version 2.0.0 of doctrine/annotations. Please use the
|
||||||
|
* {@see \Doctrine\Common\Annotations\PsrCachedReader} instead.
|
||||||
|
*/
|
||||||
|
class FileCacheReader implements Reader
|
||||||
|
{
|
||||||
|
/** @var Reader */
|
||||||
|
private $reader;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
private $dir;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
private $debug;
|
||||||
|
|
||||||
|
/** @phpstan-var array<string, list<object>> */
|
||||||
|
private $loadedAnnotations = [];
|
||||||
|
|
||||||
|
/** @var array<string, string> */
|
||||||
|
private $classNameHashes = [];
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
private $umask;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $cacheDir
|
||||||
|
* @param bool $debug
|
||||||
|
* @param int $umask
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function __construct(Reader $reader, $cacheDir, $debug = false, $umask = 0002)
|
||||||
|
{
|
||||||
|
if (! is_int($umask)) {
|
||||||
|
throw new InvalidArgumentException(sprintf(
|
||||||
|
'The parameter umask must be an integer, was: %s',
|
||||||
|
gettype($umask)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->reader = $reader;
|
||||||
|
$this->umask = $umask;
|
||||||
|
|
||||||
|
if (! is_dir($cacheDir) && ! @mkdir($cacheDir, 0777 & (~$this->umask), true)) {
|
||||||
|
throw new InvalidArgumentException(sprintf(
|
||||||
|
'The directory "%s" does not exist and could not be created.',
|
||||||
|
$cacheDir
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->dir = rtrim($cacheDir, '\\/');
|
||||||
|
$this->debug = $debug;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getClassAnnotations(ReflectionClass $class)
|
||||||
|
{
|
||||||
|
if (! isset($this->classNameHashes[$class->name])) {
|
||||||
|
$this->classNameHashes[$class->name] = sha1($class->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
$key = $this->classNameHashes[$class->name];
|
||||||
|
|
||||||
|
if (isset($this->loadedAnnotations[$key])) {
|
||||||
|
return $this->loadedAnnotations[$key];
|
||||||
|
}
|
||||||
|
|
||||||
|
$path = $this->dir . '/' . strtr($key, '\\', '-') . '.cache.php';
|
||||||
|
if (! is_file($path)) {
|
||||||
|
$annot = $this->reader->getClassAnnotations($class);
|
||||||
|
$this->saveCacheFile($path, $annot);
|
||||||
|
|
||||||
|
return $this->loadedAnnotations[$key] = $annot;
|
||||||
|
}
|
||||||
|
|
||||||
|
$filename = $class->getFilename();
|
||||||
|
if (
|
||||||
|
$this->debug
|
||||||
|
&& $filename !== false
|
||||||
|
&& filemtime($path) < filemtime($filename)
|
||||||
|
) {
|
||||||
|
@unlink($path);
|
||||||
|
|
||||||
|
$annot = $this->reader->getClassAnnotations($class);
|
||||||
|
$this->saveCacheFile($path, $annot);
|
||||||
|
|
||||||
|
return $this->loadedAnnotations[$key] = $annot;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->loadedAnnotations[$key] = include $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getPropertyAnnotations(ReflectionProperty $property)
|
||||||
|
{
|
||||||
|
$class = $property->getDeclaringClass();
|
||||||
|
if (! isset($this->classNameHashes[$class->name])) {
|
||||||
|
$this->classNameHashes[$class->name] = sha1($class->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
$key = $this->classNameHashes[$class->name] . '$' . $property->getName();
|
||||||
|
|
||||||
|
if (isset($this->loadedAnnotations[$key])) {
|
||||||
|
return $this->loadedAnnotations[$key];
|
||||||
|
}
|
||||||
|
|
||||||
|
$path = $this->dir . '/' . strtr($key, '\\', '-') . '.cache.php';
|
||||||
|
if (! is_file($path)) {
|
||||||
|
$annot = $this->reader->getPropertyAnnotations($property);
|
||||||
|
$this->saveCacheFile($path, $annot);
|
||||||
|
|
||||||
|
return $this->loadedAnnotations[$key] = $annot;
|
||||||
|
}
|
||||||
|
|
||||||
|
$filename = $class->getFilename();
|
||||||
|
if (
|
||||||
|
$this->debug
|
||||||
|
&& $filename !== false
|
||||||
|
&& filemtime($path) < filemtime($filename)
|
||||||
|
) {
|
||||||
|
@unlink($path);
|
||||||
|
|
||||||
|
$annot = $this->reader->getPropertyAnnotations($property);
|
||||||
|
$this->saveCacheFile($path, $annot);
|
||||||
|
|
||||||
|
return $this->loadedAnnotations[$key] = $annot;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->loadedAnnotations[$key] = include $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getMethodAnnotations(ReflectionMethod $method)
|
||||||
|
{
|
||||||
|
$class = $method->getDeclaringClass();
|
||||||
|
if (! isset($this->classNameHashes[$class->name])) {
|
||||||
|
$this->classNameHashes[$class->name] = sha1($class->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
$key = $this->classNameHashes[$class->name] . '#' . $method->getName();
|
||||||
|
|
||||||
|
if (isset($this->loadedAnnotations[$key])) {
|
||||||
|
return $this->loadedAnnotations[$key];
|
||||||
|
}
|
||||||
|
|
||||||
|
$path = $this->dir . '/' . strtr($key, '\\', '-') . '.cache.php';
|
||||||
|
if (! is_file($path)) {
|
||||||
|
$annot = $this->reader->getMethodAnnotations($method);
|
||||||
|
$this->saveCacheFile($path, $annot);
|
||||||
|
|
||||||
|
return $this->loadedAnnotations[$key] = $annot;
|
||||||
|
}
|
||||||
|
|
||||||
|
$filename = $class->getFilename();
|
||||||
|
if (
|
||||||
|
$this->debug
|
||||||
|
&& $filename !== false
|
||||||
|
&& filemtime($path) < filemtime($filename)
|
||||||
|
) {
|
||||||
|
@unlink($path);
|
||||||
|
|
||||||
|
$annot = $this->reader->getMethodAnnotations($method);
|
||||||
|
$this->saveCacheFile($path, $annot);
|
||||||
|
|
||||||
|
return $this->loadedAnnotations[$key] = $annot;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->loadedAnnotations[$key] = include $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the cache file.
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @param mixed $data
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function saveCacheFile($path, $data)
|
||||||
|
{
|
||||||
|
if (! is_writable($this->dir)) {
|
||||||
|
throw new InvalidArgumentException(sprintf(
|
||||||
|
<<<'EXCEPTION'
|
||||||
|
The directory "%s" is not writable. Both the webserver and the console user need access.
|
||||||
|
You can manage access rights for multiple users with "chmod +a".
|
||||||
|
If your system does not support this, check out the acl package.,
|
||||||
|
EXCEPTION
|
||||||
|
,
|
||||||
|
$this->dir
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
$tempfile = tempnam($this->dir, uniqid('', true));
|
||||||
|
|
||||||
|
if ($tempfile === false) {
|
||||||
|
throw new RuntimeException(sprintf('Unable to create tempfile in directory: %s', $this->dir));
|
||||||
|
}
|
||||||
|
|
||||||
|
@chmod($tempfile, 0666 & (~$this->umask));
|
||||||
|
|
||||||
|
$written = file_put_contents(
|
||||||
|
$tempfile,
|
||||||
|
'<?php return unserialize(' . var_export(serialize($data), true) . ');'
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($written === false) {
|
||||||
|
throw new RuntimeException(sprintf('Unable to write cached file to: %s', $tempfile));
|
||||||
|
}
|
||||||
|
|
||||||
|
@chmod($tempfile, 0666 & (~$this->umask));
|
||||||
|
|
||||||
|
if (rename($tempfile, $path) === false) {
|
||||||
|
@unlink($tempfile);
|
||||||
|
|
||||||
|
throw new RuntimeException(sprintf('Unable to rename %s to %s', $tempfile, $path));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getClassAnnotation(ReflectionClass $class, $annotationName)
|
||||||
|
{
|
||||||
|
$annotations = $this->getClassAnnotations($class);
|
||||||
|
|
||||||
|
foreach ($annotations as $annotation) {
|
||||||
|
if ($annotation instanceof $annotationName) {
|
||||||
|
return $annotation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
|
||||||
|
{
|
||||||
|
$annotations = $this->getMethodAnnotations($method);
|
||||||
|
|
||||||
|
foreach ($annotations as $annotation) {
|
||||||
|
if ($annotation instanceof $annotationName) {
|
||||||
|
return $annotation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
|
||||||
|
{
|
||||||
|
$annotations = $this->getPropertyAnnotations($property);
|
||||||
|
|
||||||
|
foreach ($annotations as $annotation) {
|
||||||
|
if ($annotation instanceof $annotationName) {
|
||||||
|
return $annotation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears loaded annotations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function clearLoadedAnnotations()
|
||||||
|
{
|
||||||
|
$this->loadedAnnotations = [];
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,178 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A list of annotations that are implicitly ignored during the parsing process.
|
||||||
|
*
|
||||||
|
* All names are case sensitive.
|
||||||
|
*/
|
||||||
|
final class ImplicitlyIgnoredAnnotationNames
|
||||||
|
{
|
||||||
|
private const Reserved = [
|
||||||
|
'Annotation' => true,
|
||||||
|
'Attribute' => true,
|
||||||
|
'Attributes' => true,
|
||||||
|
/* Can we enable this? 'Enum' => true, */
|
||||||
|
'Required' => true,
|
||||||
|
'Target' => true,
|
||||||
|
'NamedArgumentConstructor' => true,
|
||||||
|
];
|
||||||
|
|
||||||
|
private const WidelyUsedNonStandard = [
|
||||||
|
'fix' => true,
|
||||||
|
'fixme' => true,
|
||||||
|
'override' => true,
|
||||||
|
];
|
||||||
|
|
||||||
|
private const PhpDocumentor1 = [
|
||||||
|
'abstract' => true,
|
||||||
|
'access' => true,
|
||||||
|
'code' => true,
|
||||||
|
'deprec' => true,
|
||||||
|
'endcode' => true,
|
||||||
|
'exception' => true,
|
||||||
|
'final' => true,
|
||||||
|
'ingroup' => true,
|
||||||
|
'inheritdoc' => true,
|
||||||
|
'inheritDoc' => true,
|
||||||
|
'magic' => true,
|
||||||
|
'name' => true,
|
||||||
|
'private' => true,
|
||||||
|
'static' => true,
|
||||||
|
'staticvar' => true,
|
||||||
|
'staticVar' => true,
|
||||||
|
'toc' => true,
|
||||||
|
'tutorial' => true,
|
||||||
|
'throw' => true,
|
||||||
|
];
|
||||||
|
|
||||||
|
private const PhpDocumentor2 = [
|
||||||
|
'api' => true,
|
||||||
|
'author' => true,
|
||||||
|
'category' => true,
|
||||||
|
'copyright' => true,
|
||||||
|
'deprecated' => true,
|
||||||
|
'example' => true,
|
||||||
|
'filesource' => true,
|
||||||
|
'global' => true,
|
||||||
|
'ignore' => true,
|
||||||
|
/* Can we enable this? 'index' => true, */
|
||||||
|
'internal' => true,
|
||||||
|
'license' => true,
|
||||||
|
'link' => true,
|
||||||
|
'method' => true,
|
||||||
|
'package' => true,
|
||||||
|
'param' => true,
|
||||||
|
'property' => true,
|
||||||
|
'property-read' => true,
|
||||||
|
'property-write' => true,
|
||||||
|
'return' => true,
|
||||||
|
'see' => true,
|
||||||
|
'since' => true,
|
||||||
|
'source' => true,
|
||||||
|
'subpackage' => true,
|
||||||
|
'throws' => true,
|
||||||
|
'todo' => true,
|
||||||
|
'TODO' => true,
|
||||||
|
'usedby' => true,
|
||||||
|
'uses' => true,
|
||||||
|
'var' => true,
|
||||||
|
'version' => true,
|
||||||
|
];
|
||||||
|
|
||||||
|
private const PHPUnit = [
|
||||||
|
'author' => true,
|
||||||
|
'after' => true,
|
||||||
|
'afterClass' => true,
|
||||||
|
'backupGlobals' => true,
|
||||||
|
'backupStaticAttributes' => true,
|
||||||
|
'before' => true,
|
||||||
|
'beforeClass' => true,
|
||||||
|
'codeCoverageIgnore' => true,
|
||||||
|
'codeCoverageIgnoreStart' => true,
|
||||||
|
'codeCoverageIgnoreEnd' => true,
|
||||||
|
'covers' => true,
|
||||||
|
'coversDefaultClass' => true,
|
||||||
|
'coversNothing' => true,
|
||||||
|
'dataProvider' => true,
|
||||||
|
'depends' => true,
|
||||||
|
'doesNotPerformAssertions' => true,
|
||||||
|
'expectedException' => true,
|
||||||
|
'expectedExceptionCode' => true,
|
||||||
|
'expectedExceptionMessage' => true,
|
||||||
|
'expectedExceptionMessageRegExp' => true,
|
||||||
|
'group' => true,
|
||||||
|
'large' => true,
|
||||||
|
'medium' => true,
|
||||||
|
'preserveGlobalState' => true,
|
||||||
|
'requires' => true,
|
||||||
|
'runTestsInSeparateProcesses' => true,
|
||||||
|
'runInSeparateProcess' => true,
|
||||||
|
'small' => true,
|
||||||
|
'test' => true,
|
||||||
|
'testdox' => true,
|
||||||
|
'testWith' => true,
|
||||||
|
'ticket' => true,
|
||||||
|
'uses' => true,
|
||||||
|
];
|
||||||
|
|
||||||
|
private const PhpCheckStyle = ['SuppressWarnings' => true];
|
||||||
|
|
||||||
|
private const PhpStorm = ['noinspection' => true];
|
||||||
|
|
||||||
|
private const PEAR = ['package_version' => true];
|
||||||
|
|
||||||
|
private const PlainUML = [
|
||||||
|
'startuml' => true,
|
||||||
|
'enduml' => true,
|
||||||
|
];
|
||||||
|
|
||||||
|
private const Symfony = ['experimental' => true];
|
||||||
|
|
||||||
|
private const PhpCodeSniffer = [
|
||||||
|
'codingStandardsIgnoreStart' => true,
|
||||||
|
'codingStandardsIgnoreEnd' => true,
|
||||||
|
];
|
||||||
|
|
||||||
|
private const SlevomatCodingStandard = ['phpcsSuppress' => true];
|
||||||
|
|
||||||
|
private const Phan = ['suppress' => true];
|
||||||
|
|
||||||
|
private const Rector = ['noRector' => true];
|
||||||
|
|
||||||
|
private const StaticAnalysis = [
|
||||||
|
// PHPStan, Psalm
|
||||||
|
'extends' => true,
|
||||||
|
'implements' => true,
|
||||||
|
'readonly' => true,
|
||||||
|
'template' => true,
|
||||||
|
'use' => true,
|
||||||
|
|
||||||
|
// Psalm
|
||||||
|
'pure' => true,
|
||||||
|
'immutable' => true,
|
||||||
|
];
|
||||||
|
|
||||||
|
public const LIST = self::Reserved
|
||||||
|
+ self::WidelyUsedNonStandard
|
||||||
|
+ self::PhpDocumentor1
|
||||||
|
+ self::PhpDocumentor2
|
||||||
|
+ self::PHPUnit
|
||||||
|
+ self::PhpCheckStyle
|
||||||
|
+ self::PhpStorm
|
||||||
|
+ self::PEAR
|
||||||
|
+ self::PlainUML
|
||||||
|
+ self::Symfony
|
||||||
|
+ self::SlevomatCodingStandard
|
||||||
|
+ self::PhpCodeSniffer
|
||||||
|
+ self::Phan
|
||||||
|
+ self::Rector
|
||||||
|
+ self::StaticAnalysis;
|
||||||
|
|
||||||
|
private function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,100 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations;
|
||||||
|
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionMethod;
|
||||||
|
use ReflectionProperty;
|
||||||
|
|
||||||
|
use function call_user_func_array;
|
||||||
|
use function get_class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allows the reader to be used in-place of Doctrine's reader.
|
||||||
|
*/
|
||||||
|
class IndexedReader implements Reader
|
||||||
|
{
|
||||||
|
/** @var Reader */
|
||||||
|
private $delegate;
|
||||||
|
|
||||||
|
public function __construct(Reader $reader)
|
||||||
|
{
|
||||||
|
$this->delegate = $reader;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getClassAnnotations(ReflectionClass $class)
|
||||||
|
{
|
||||||
|
$annotations = [];
|
||||||
|
foreach ($this->delegate->getClassAnnotations($class) as $annot) {
|
||||||
|
$annotations[get_class($annot)] = $annot;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $annotations;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getClassAnnotation(ReflectionClass $class, $annotationName)
|
||||||
|
{
|
||||||
|
return $this->delegate->getClassAnnotation($class, $annotationName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getMethodAnnotations(ReflectionMethod $method)
|
||||||
|
{
|
||||||
|
$annotations = [];
|
||||||
|
foreach ($this->delegate->getMethodAnnotations($method) as $annot) {
|
||||||
|
$annotations[get_class($annot)] = $annot;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $annotations;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
|
||||||
|
{
|
||||||
|
return $this->delegate->getMethodAnnotation($method, $annotationName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getPropertyAnnotations(ReflectionProperty $property)
|
||||||
|
{
|
||||||
|
$annotations = [];
|
||||||
|
foreach ($this->delegate->getPropertyAnnotations($property) as $annot) {
|
||||||
|
$annotations[get_class($annot)] = $annot;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $annotations;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
|
||||||
|
{
|
||||||
|
return $this->delegate->getPropertyAnnotation($property, $annotationName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Proxies all methods to the delegate.
|
||||||
|
*
|
||||||
|
* @param string $method
|
||||||
|
* @param mixed[] $args
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function __call($method, $args)
|
||||||
|
{
|
||||||
|
return call_user_func_array([$this->delegate, $method], $args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Marker interface for PHP7/PHP8 compatible support
|
||||||
|
* for named arguments (and constructor property promotion).
|
||||||
|
*
|
||||||
|
* @deprecated Implementing this interface is deprecated
|
||||||
|
* Use the Annotation @NamedArgumentConstructor instead
|
||||||
|
*/
|
||||||
|
interface NamedArgumentConstructorAnnotation
|
||||||
|
{
|
||||||
|
}
|
@ -0,0 +1,92 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations;
|
||||||
|
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionFunction;
|
||||||
|
use SplFileObject;
|
||||||
|
|
||||||
|
use function is_file;
|
||||||
|
use function method_exists;
|
||||||
|
use function preg_quote;
|
||||||
|
use function preg_replace;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a file for namespaces/use/class declarations.
|
||||||
|
*/
|
||||||
|
final class PhpParser
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Parses a class.
|
||||||
|
*
|
||||||
|
* @deprecated use parseUseStatements instead
|
||||||
|
*
|
||||||
|
* @param ReflectionClass $class A <code>ReflectionClass</code> object.
|
||||||
|
*
|
||||||
|
* @return array<string, class-string> A list with use statements in the form (Alias => FQN).
|
||||||
|
*/
|
||||||
|
public function parseClass(ReflectionClass $class)
|
||||||
|
{
|
||||||
|
return $this->parseUseStatements($class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse a class or function for use statements.
|
||||||
|
*
|
||||||
|
* @param ReflectionClass|ReflectionFunction $reflection
|
||||||
|
*
|
||||||
|
* @psalm-return array<string, string> a list with use statements in the form (Alias => FQN).
|
||||||
|
*/
|
||||||
|
public function parseUseStatements($reflection): array
|
||||||
|
{
|
||||||
|
if (method_exists($reflection, 'getUseStatements')) {
|
||||||
|
return $reflection->getUseStatements();
|
||||||
|
}
|
||||||
|
|
||||||
|
$filename = $reflection->getFileName();
|
||||||
|
|
||||||
|
if ($filename === false) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = $this->getFileContent($filename, $reflection->getStartLine());
|
||||||
|
|
||||||
|
if ($content === null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$namespace = preg_quote($reflection->getNamespaceName());
|
||||||
|
$content = preg_replace('/^.*?(\bnamespace\s+' . $namespace . '\s*[;{].*)$/s', '\\1', $content);
|
||||||
|
$tokenizer = new TokenParser('<?php ' . $content);
|
||||||
|
|
||||||
|
return $tokenizer->parseUseStatements($reflection->getNamespaceName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the content of the file right up to the given line number.
|
||||||
|
*
|
||||||
|
* @param string $filename The name of the file to load.
|
||||||
|
* @param int $lineNumber The number of lines to read from file.
|
||||||
|
*
|
||||||
|
* @return string|null The content of the file or null if the file does not exist.
|
||||||
|
*/
|
||||||
|
private function getFileContent($filename, $lineNumber)
|
||||||
|
{
|
||||||
|
if (! is_file($filename)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = '';
|
||||||
|
$lineCnt = 0;
|
||||||
|
$file = new SplFileObject($filename);
|
||||||
|
while (! $file->eof()) {
|
||||||
|
if ($lineCnt++ === $lineNumber) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$content .= $file->fgets();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,232 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations;
|
||||||
|
|
||||||
|
use Psr\Cache\CacheItemPoolInterface;
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionMethod;
|
||||||
|
use ReflectionProperty;
|
||||||
|
use Reflector;
|
||||||
|
|
||||||
|
use function array_map;
|
||||||
|
use function array_merge;
|
||||||
|
use function assert;
|
||||||
|
use function filemtime;
|
||||||
|
use function max;
|
||||||
|
use function rawurlencode;
|
||||||
|
use function time;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A cache aware annotation reader.
|
||||||
|
*/
|
||||||
|
final class PsrCachedReader implements Reader
|
||||||
|
{
|
||||||
|
/** @var Reader */
|
||||||
|
private $delegate;
|
||||||
|
|
||||||
|
/** @var CacheItemPoolInterface */
|
||||||
|
private $cache;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
private $debug;
|
||||||
|
|
||||||
|
/** @var array<string, array<object>> */
|
||||||
|
private $loadedAnnotations = [];
|
||||||
|
|
||||||
|
/** @var int[] */
|
||||||
|
private $loadedFilemtimes = [];
|
||||||
|
|
||||||
|
public function __construct(Reader $reader, CacheItemPoolInterface $cache, bool $debug = false)
|
||||||
|
{
|
||||||
|
$this->delegate = $reader;
|
||||||
|
$this->cache = $cache;
|
||||||
|
$this->debug = (bool) $debug;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getClassAnnotations(ReflectionClass $class)
|
||||||
|
{
|
||||||
|
$cacheKey = $class->getName();
|
||||||
|
|
||||||
|
if (isset($this->loadedAnnotations[$cacheKey])) {
|
||||||
|
return $this->loadedAnnotations[$cacheKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
$annots = $this->fetchFromCache($cacheKey, $class, 'getClassAnnotations', $class);
|
||||||
|
|
||||||
|
return $this->loadedAnnotations[$cacheKey] = $annots;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getClassAnnotation(ReflectionClass $class, $annotationName)
|
||||||
|
{
|
||||||
|
foreach ($this->getClassAnnotations($class) as $annot) {
|
||||||
|
if ($annot instanceof $annotationName) {
|
||||||
|
return $annot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getPropertyAnnotations(ReflectionProperty $property)
|
||||||
|
{
|
||||||
|
$class = $property->getDeclaringClass();
|
||||||
|
$cacheKey = $class->getName() . '$' . $property->getName();
|
||||||
|
|
||||||
|
if (isset($this->loadedAnnotations[$cacheKey])) {
|
||||||
|
return $this->loadedAnnotations[$cacheKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
$annots = $this->fetchFromCache($cacheKey, $class, 'getPropertyAnnotations', $property);
|
||||||
|
|
||||||
|
return $this->loadedAnnotations[$cacheKey] = $annots;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
|
||||||
|
{
|
||||||
|
foreach ($this->getPropertyAnnotations($property) as $annot) {
|
||||||
|
if ($annot instanceof $annotationName) {
|
||||||
|
return $annot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getMethodAnnotations(ReflectionMethod $method)
|
||||||
|
{
|
||||||
|
$class = $method->getDeclaringClass();
|
||||||
|
$cacheKey = $class->getName() . '#' . $method->getName();
|
||||||
|
|
||||||
|
if (isset($this->loadedAnnotations[$cacheKey])) {
|
||||||
|
return $this->loadedAnnotations[$cacheKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
$annots = $this->fetchFromCache($cacheKey, $class, 'getMethodAnnotations', $method);
|
||||||
|
|
||||||
|
return $this->loadedAnnotations[$cacheKey] = $annots;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
|
||||||
|
{
|
||||||
|
foreach ($this->getMethodAnnotations($method) as $annot) {
|
||||||
|
if ($annot instanceof $annotationName) {
|
||||||
|
return $annot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clearLoadedAnnotations(): void
|
||||||
|
{
|
||||||
|
$this->loadedAnnotations = [];
|
||||||
|
$this->loadedFilemtimes = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return mixed[] */
|
||||||
|
private function fetchFromCache(
|
||||||
|
string $cacheKey,
|
||||||
|
ReflectionClass $class,
|
||||||
|
string $method,
|
||||||
|
Reflector $reflector
|
||||||
|
): array {
|
||||||
|
$cacheKey = rawurlencode($cacheKey);
|
||||||
|
|
||||||
|
$item = $this->cache->getItem($cacheKey);
|
||||||
|
if (($this->debug && ! $this->refresh($cacheKey, $class)) || ! $item->isHit()) {
|
||||||
|
$this->cache->save($item->set($this->delegate->{$method}($reflector)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $item->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used in debug mode to check if the cache is fresh.
|
||||||
|
*
|
||||||
|
* @return bool Returns true if the cache was fresh, or false if the class
|
||||||
|
* being read was modified since writing to the cache.
|
||||||
|
*/
|
||||||
|
private function refresh(string $cacheKey, ReflectionClass $class): bool
|
||||||
|
{
|
||||||
|
$lastModification = $this->getLastModification($class);
|
||||||
|
if ($lastModification === 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$item = $this->cache->getItem('[C]' . $cacheKey);
|
||||||
|
if ($item->isHit() && $item->get() >= $lastModification) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->cache->save($item->set(time()));
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the time the class was last modified, testing traits and parents
|
||||||
|
*/
|
||||||
|
private function getLastModification(ReflectionClass $class): int
|
||||||
|
{
|
||||||
|
$filename = $class->getFileName();
|
||||||
|
|
||||||
|
if (isset($this->loadedFilemtimes[$filename])) {
|
||||||
|
return $this->loadedFilemtimes[$filename];
|
||||||
|
}
|
||||||
|
|
||||||
|
$parent = $class->getParentClass();
|
||||||
|
|
||||||
|
$lastModification = max(array_merge(
|
||||||
|
[$filename ? filemtime($filename) : 0],
|
||||||
|
array_map(function (ReflectionClass $reflectionTrait): int {
|
||||||
|
return $this->getTraitLastModificationTime($reflectionTrait);
|
||||||
|
}, $class->getTraits()),
|
||||||
|
array_map(function (ReflectionClass $class): int {
|
||||||
|
return $this->getLastModification($class);
|
||||||
|
}, $class->getInterfaces()),
|
||||||
|
$parent ? [$this->getLastModification($parent)] : []
|
||||||
|
));
|
||||||
|
|
||||||
|
assert($lastModification !== false);
|
||||||
|
|
||||||
|
return $this->loadedFilemtimes[$filename] = $lastModification;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getTraitLastModificationTime(ReflectionClass $reflectionTrait): int
|
||||||
|
{
|
||||||
|
$fileName = $reflectionTrait->getFileName();
|
||||||
|
|
||||||
|
if (isset($this->loadedFilemtimes[$fileName])) {
|
||||||
|
return $this->loadedFilemtimes[$fileName];
|
||||||
|
}
|
||||||
|
|
||||||
|
$lastModificationTime = max(array_merge(
|
||||||
|
[$fileName ? filemtime($fileName) : 0],
|
||||||
|
array_map(function (ReflectionClass $reflectionTrait): int {
|
||||||
|
return $this->getTraitLastModificationTime($reflectionTrait);
|
||||||
|
}, $reflectionTrait->getTraits())
|
||||||
|
));
|
||||||
|
|
||||||
|
assert($lastModificationTime !== false);
|
||||||
|
|
||||||
|
return $this->loadedFilemtimes[$fileName] = $lastModificationTime;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations;
|
||||||
|
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionMethod;
|
||||||
|
use ReflectionProperty;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for annotation readers.
|
||||||
|
*/
|
||||||
|
interface Reader
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Gets the annotations applied to a class.
|
||||||
|
*
|
||||||
|
* @param ReflectionClass $class The ReflectionClass of the class from which
|
||||||
|
* the class annotations should be read.
|
||||||
|
*
|
||||||
|
* @return array<object> An array of Annotations.
|
||||||
|
*/
|
||||||
|
public function getClassAnnotations(ReflectionClass $class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a class annotation.
|
||||||
|
*
|
||||||
|
* @param ReflectionClass $class The ReflectionClass of the class from which
|
||||||
|
* the class annotations should be read.
|
||||||
|
* @param class-string<T> $annotationName The name of the annotation.
|
||||||
|
*
|
||||||
|
* @return T|null The Annotation or NULL, if the requested annotation does not exist.
|
||||||
|
*
|
||||||
|
* @template T
|
||||||
|
*/
|
||||||
|
public function getClassAnnotation(ReflectionClass $class, $annotationName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the annotations applied to a method.
|
||||||
|
*
|
||||||
|
* @param ReflectionMethod $method The ReflectionMethod of the method from which
|
||||||
|
* the annotations should be read.
|
||||||
|
*
|
||||||
|
* @return array<object> An array of Annotations.
|
||||||
|
*/
|
||||||
|
public function getMethodAnnotations(ReflectionMethod $method);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a method annotation.
|
||||||
|
*
|
||||||
|
* @param ReflectionMethod $method The ReflectionMethod to read the annotations from.
|
||||||
|
* @param class-string<T> $annotationName The name of the annotation.
|
||||||
|
*
|
||||||
|
* @return T|null The Annotation or NULL, if the requested annotation does not exist.
|
||||||
|
*
|
||||||
|
* @template T
|
||||||
|
*/
|
||||||
|
public function getMethodAnnotation(ReflectionMethod $method, $annotationName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the annotations applied to a property.
|
||||||
|
*
|
||||||
|
* @param ReflectionProperty $property The ReflectionProperty of the property
|
||||||
|
* from which the annotations should be read.
|
||||||
|
*
|
||||||
|
* @return array<object> An array of Annotations.
|
||||||
|
*/
|
||||||
|
public function getPropertyAnnotations(ReflectionProperty $property);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a property annotation.
|
||||||
|
*
|
||||||
|
* @param ReflectionProperty $property The ReflectionProperty to read the annotations from.
|
||||||
|
* @param class-string<T> $annotationName The name of the annotation.
|
||||||
|
*
|
||||||
|
* @return T|null The Annotation or NULL, if the requested annotation does not exist.
|
||||||
|
*
|
||||||
|
* @template T
|
||||||
|
*/
|
||||||
|
public function getPropertyAnnotation(ReflectionProperty $property, $annotationName);
|
||||||
|
}
|
@ -0,0 +1,114 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations;
|
||||||
|
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionMethod;
|
||||||
|
use ReflectionProperty;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple Annotation Reader.
|
||||||
|
*
|
||||||
|
* This annotation reader is intended to be used in projects where you have
|
||||||
|
* full-control over all annotations that are available.
|
||||||
|
*
|
||||||
|
* @deprecated Deprecated in favour of using AnnotationReader
|
||||||
|
*/
|
||||||
|
class SimpleAnnotationReader implements Reader
|
||||||
|
{
|
||||||
|
/** @var DocParser */
|
||||||
|
private $parser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new SimpleAnnotationReader.
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->parser = new DocParser();
|
||||||
|
$this->parser->setIgnoreNotImportedAnnotations(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a namespace in which we will look for annotations.
|
||||||
|
*
|
||||||
|
* @param string $namespace
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function addNamespace($namespace)
|
||||||
|
{
|
||||||
|
$this->parser->addNamespace($namespace);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getClassAnnotations(ReflectionClass $class)
|
||||||
|
{
|
||||||
|
return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getMethodAnnotations(ReflectionMethod $method)
|
||||||
|
{
|
||||||
|
return $this->parser->parse(
|
||||||
|
$method->getDocComment(),
|
||||||
|
'method ' . $method->getDeclaringClass()->name . '::' . $method->getName() . '()'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getPropertyAnnotations(ReflectionProperty $property)
|
||||||
|
{
|
||||||
|
return $this->parser->parse(
|
||||||
|
$property->getDocComment(),
|
||||||
|
'property ' . $property->getDeclaringClass()->name . '::$' . $property->getName()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getClassAnnotation(ReflectionClass $class, $annotationName)
|
||||||
|
{
|
||||||
|
foreach ($this->getClassAnnotations($class) as $annot) {
|
||||||
|
if ($annot instanceof $annotationName) {
|
||||||
|
return $annot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
|
||||||
|
{
|
||||||
|
foreach ($this->getMethodAnnotations($method) as $annot) {
|
||||||
|
if ($annot instanceof $annotationName) {
|
||||||
|
return $annot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
|
||||||
|
{
|
||||||
|
foreach ($this->getPropertyAnnotations($property) as $annot) {
|
||||||
|
if ($annot instanceof $annotationName) {
|
||||||
|
return $annot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,206 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Annotations;
|
||||||
|
|
||||||
|
use function array_merge;
|
||||||
|
use function count;
|
||||||
|
use function explode;
|
||||||
|
use function strtolower;
|
||||||
|
use function token_get_all;
|
||||||
|
|
||||||
|
use const PHP_VERSION_ID;
|
||||||
|
use const T_AS;
|
||||||
|
use const T_COMMENT;
|
||||||
|
use const T_DOC_COMMENT;
|
||||||
|
use const T_NAME_FULLY_QUALIFIED;
|
||||||
|
use const T_NAME_QUALIFIED;
|
||||||
|
use const T_NAMESPACE;
|
||||||
|
use const T_NS_SEPARATOR;
|
||||||
|
use const T_STRING;
|
||||||
|
use const T_USE;
|
||||||
|
use const T_WHITESPACE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a file for namespaces/use/class declarations.
|
||||||
|
*/
|
||||||
|
class TokenParser
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The token list.
|
||||||
|
*
|
||||||
|
* @phpstan-var list<mixed[]>
|
||||||
|
*/
|
||||||
|
private $tokens;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The number of tokens.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $numTokens;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The current array pointer.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $pointer = 0;
|
||||||
|
|
||||||
|
/** @param string $contents */
|
||||||
|
public function __construct($contents)
|
||||||
|
{
|
||||||
|
$this->tokens = token_get_all($contents);
|
||||||
|
|
||||||
|
// The PHP parser sets internal compiler globals for certain things. Annoyingly, the last docblock comment it
|
||||||
|
// saw gets stored in doc_comment. When it comes to compile the next thing to be include()d this stored
|
||||||
|
// doc_comment becomes owned by the first thing the compiler sees in the file that it considers might have a
|
||||||
|
// docblock. If the first thing in the file is a class without a doc block this would cause calls to
|
||||||
|
// getDocBlock() on said class to return our long lost doc_comment. Argh.
|
||||||
|
// To workaround, cause the parser to parse an empty docblock. Sure getDocBlock() will return this, but at least
|
||||||
|
// it's harmless to us.
|
||||||
|
token_get_all("<?php\n/**\n *\n */");
|
||||||
|
|
||||||
|
$this->numTokens = count($this->tokens);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the next non whitespace and non comment token.
|
||||||
|
*
|
||||||
|
* @param bool $docCommentIsComment If TRUE then a doc comment is considered a comment and skipped.
|
||||||
|
* If FALSE then only whitespace and normal comments are skipped.
|
||||||
|
*
|
||||||
|
* @return mixed[]|string|null The token if exists, null otherwise.
|
||||||
|
*/
|
||||||
|
public function next($docCommentIsComment = true)
|
||||||
|
{
|
||||||
|
for ($i = $this->pointer; $i < $this->numTokens; $i++) {
|
||||||
|
$this->pointer++;
|
||||||
|
if (
|
||||||
|
$this->tokens[$i][0] === T_WHITESPACE ||
|
||||||
|
$this->tokens[$i][0] === T_COMMENT ||
|
||||||
|
($docCommentIsComment && $this->tokens[$i][0] === T_DOC_COMMENT)
|
||||||
|
) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->tokens[$i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a single use statement.
|
||||||
|
*
|
||||||
|
* @return array<string, string> A list with all found class names for a use statement.
|
||||||
|
*/
|
||||||
|
public function parseUseStatement()
|
||||||
|
{
|
||||||
|
$groupRoot = '';
|
||||||
|
$class = '';
|
||||||
|
$alias = '';
|
||||||
|
$statements = [];
|
||||||
|
$explicitAlias = false;
|
||||||
|
while (($token = $this->next())) {
|
||||||
|
if (! $explicitAlias && $token[0] === T_STRING) {
|
||||||
|
$class .= $token[1];
|
||||||
|
$alias = $token[1];
|
||||||
|
} elseif ($explicitAlias && $token[0] === T_STRING) {
|
||||||
|
$alias = $token[1];
|
||||||
|
} elseif (
|
||||||
|
PHP_VERSION_ID >= 80000 &&
|
||||||
|
($token[0] === T_NAME_QUALIFIED || $token[0] === T_NAME_FULLY_QUALIFIED)
|
||||||
|
) {
|
||||||
|
$class .= $token[1];
|
||||||
|
|
||||||
|
$classSplit = explode('\\', $token[1]);
|
||||||
|
$alias = $classSplit[count($classSplit) - 1];
|
||||||
|
} elseif ($token[0] === T_NS_SEPARATOR) {
|
||||||
|
$class .= '\\';
|
||||||
|
$alias = '';
|
||||||
|
} elseif ($token[0] === T_AS) {
|
||||||
|
$explicitAlias = true;
|
||||||
|
$alias = '';
|
||||||
|
} elseif ($token === ',') {
|
||||||
|
$statements[strtolower($alias)] = $groupRoot . $class;
|
||||||
|
$class = '';
|
||||||
|
$alias = '';
|
||||||
|
$explicitAlias = false;
|
||||||
|
} elseif ($token === ';') {
|
||||||
|
$statements[strtolower($alias)] = $groupRoot . $class;
|
||||||
|
break;
|
||||||
|
} elseif ($token === '{') {
|
||||||
|
$groupRoot = $class;
|
||||||
|
$class = '';
|
||||||
|
} elseif ($token === '}') {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $statements;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets all use statements.
|
||||||
|
*
|
||||||
|
* @param string $namespaceName The namespace name of the reflected class.
|
||||||
|
*
|
||||||
|
* @return array<string, string> A list with all found use statements.
|
||||||
|
*/
|
||||||
|
public function parseUseStatements($namespaceName)
|
||||||
|
{
|
||||||
|
$statements = [];
|
||||||
|
while (($token = $this->next())) {
|
||||||
|
if ($token[0] === T_USE) {
|
||||||
|
$statements = array_merge($statements, $this->parseUseStatement());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($token[0] !== T_NAMESPACE || $this->parseNamespace() !== $namespaceName) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get fresh array for new namespace. This is to prevent the parser to collect the use statements
|
||||||
|
// for a previous namespace with the same name. This is the case if a namespace is defined twice
|
||||||
|
// or if a namespace with the same name is commented out.
|
||||||
|
$statements = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $statements;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the namespace.
|
||||||
|
*
|
||||||
|
* @return string The found namespace.
|
||||||
|
*/
|
||||||
|
public function parseNamespace()
|
||||||
|
{
|
||||||
|
$name = '';
|
||||||
|
while (
|
||||||
|
($token = $this->next()) && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR || (
|
||||||
|
PHP_VERSION_ID >= 80000 &&
|
||||||
|
($token[0] === T_NAME_QUALIFIED || $token[0] === T_NAME_FULLY_QUALIFIED)
|
||||||
|
))
|
||||||
|
) {
|
||||||
|
$name .= $token[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the class name.
|
||||||
|
*
|
||||||
|
* @return string The found class name.
|
||||||
|
*/
|
||||||
|
public function parseClass()
|
||||||
|
{
|
||||||
|
// Namespaces and class names are tokenized the same: T_STRINGs
|
||||||
|
// separated by T_NS_SEPARATOR so we can use one function to provide
|
||||||
|
// both.
|
||||||
|
return $this->parseNamespace();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
<psalm
|
||||||
|
errorLevel="7"
|
||||||
|
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="lib/Doctrine/Common/Annotations" />
|
||||||
|
<ignoreFiles>
|
||||||
|
<directory name="vendor" />
|
||||||
|
</ignoreFiles>
|
||||||
|
</projectFiles>
|
||||||
|
</psalm>
|
@ -0,0 +1,19 @@
|
|||||||
|
Copyright (c) 2020-2021 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.
|
@ -0,0 +1,154 @@
|
|||||||
|
# Doctrine Deprecations
|
||||||
|
|
||||||
|
A small (side-effect free by default) layer on top of
|
||||||
|
`trigger_error(E_USER_DEPRECATED)` or PSR-3 logging.
|
||||||
|
|
||||||
|
- no side-effects by default, making it a perfect fit for libraries that don't know how the error handler works they operate under
|
||||||
|
- options to avoid having to rely on error handlers global state by using PSR-3 logging
|
||||||
|
- deduplicate deprecation messages to avoid excessive triggering and reduce overhead
|
||||||
|
|
||||||
|
We recommend to collect Deprecations using a PSR logger instead of relying on
|
||||||
|
the global error handler.
|
||||||
|
|
||||||
|
## Usage from consumer perspective:
|
||||||
|
|
||||||
|
Enable Doctrine deprecations to be sent to a PSR3 logger:
|
||||||
|
|
||||||
|
```php
|
||||||
|
\Doctrine\Deprecations\Deprecation::enableWithPsrLogger($logger);
|
||||||
|
```
|
||||||
|
|
||||||
|
Enable Doctrine deprecations to be sent as `@trigger_error($message, E_USER_DEPRECATED)`
|
||||||
|
messages.
|
||||||
|
|
||||||
|
```php
|
||||||
|
\Doctrine\Deprecations\Deprecation::enableWithTriggerError();
|
||||||
|
```
|
||||||
|
|
||||||
|
If you only want to enable deprecation tracking, without logging or calling `trigger_error` then call:
|
||||||
|
|
||||||
|
```php
|
||||||
|
\Doctrine\Deprecations\Deprecation::enableTrackingDeprecations();
|
||||||
|
```
|
||||||
|
|
||||||
|
Tracking is enabled with all three modes and provides access to all triggered
|
||||||
|
deprecations and their individual count:
|
||||||
|
|
||||||
|
```php
|
||||||
|
$deprecations = \Doctrine\Deprecations\Deprecation::getTriggeredDeprecations();
|
||||||
|
|
||||||
|
foreach ($deprecations as $identifier => $count) {
|
||||||
|
echo $identifier . " was triggered " . $count . " times\n";
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Suppressing Specific Deprecations
|
||||||
|
|
||||||
|
Disable triggering about specific deprecations:
|
||||||
|
|
||||||
|
```php
|
||||||
|
\Doctrine\Deprecations\Deprecation::ignoreDeprecations("https://link/to/deprecations-description-identifier");
|
||||||
|
```
|
||||||
|
|
||||||
|
Disable all deprecations from a package
|
||||||
|
|
||||||
|
```php
|
||||||
|
\Doctrine\Deprecations\Deprecation::ignorePackage("doctrine/orm");
|
||||||
|
```
|
||||||
|
|
||||||
|
### Other Operations
|
||||||
|
|
||||||
|
When used within PHPUnit or other tools that could collect multiple instances of the same deprecations
|
||||||
|
the deduplication can be disabled:
|
||||||
|
|
||||||
|
```php
|
||||||
|
\Doctrine\Deprecations\Deprecation::withoutDeduplication();
|
||||||
|
```
|
||||||
|
|
||||||
|
Disable deprecation tracking again:
|
||||||
|
|
||||||
|
```php
|
||||||
|
\Doctrine\Deprecations\Deprecation::disable();
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage from a library/producer perspective:
|
||||||
|
|
||||||
|
When you want to unconditionally trigger a deprecation even when called
|
||||||
|
from the library itself then the `trigger` method is the way to go:
|
||||||
|
|
||||||
|
```php
|
||||||
|
\Doctrine\Deprecations\Deprecation::trigger(
|
||||||
|
"doctrine/orm",
|
||||||
|
"https://link/to/deprecations-description",
|
||||||
|
"message"
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
If variable arguments are provided at the end, they are used with `sprintf` on
|
||||||
|
the message.
|
||||||
|
|
||||||
|
```php
|
||||||
|
\Doctrine\Deprecations\Deprecation::trigger(
|
||||||
|
"doctrine/orm",
|
||||||
|
"https://github.com/doctrine/orm/issue/1234",
|
||||||
|
"message %s %d",
|
||||||
|
"foo",
|
||||||
|
1234
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
When you want to trigger a deprecation only when it is called by a function
|
||||||
|
outside of the current package, but not trigger when the package itself is the cause,
|
||||||
|
then use:
|
||||||
|
|
||||||
|
```php
|
||||||
|
\Doctrine\Deprecations\Deprecation::triggerIfCalledFromOutside(
|
||||||
|
"doctrine/orm",
|
||||||
|
"https://link/to/deprecations-description",
|
||||||
|
"message"
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
Based on the issue link each deprecation message is only triggered once per
|
||||||
|
request.
|
||||||
|
|
||||||
|
A limited stacktrace is included in the deprecation message to find the
|
||||||
|
offending location.
|
||||||
|
|
||||||
|
Note: A producer/library should never call `Deprecation::enableWith` methods
|
||||||
|
and leave the decision how to handle deprecations to application and
|
||||||
|
frameworks.
|
||||||
|
|
||||||
|
## Usage in PHPUnit tests
|
||||||
|
|
||||||
|
There is a `VerifyDeprecations` trait that you can use to make assertions on
|
||||||
|
the occurrence of deprecations within a test.
|
||||||
|
|
||||||
|
```php
|
||||||
|
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
|
||||||
|
|
||||||
|
class MyTest extends TestCase
|
||||||
|
{
|
||||||
|
use VerifyDeprecations;
|
||||||
|
|
||||||
|
public function testSomethingDeprecation()
|
||||||
|
{
|
||||||
|
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issue/1234');
|
||||||
|
|
||||||
|
triggerTheCodeWithDeprecation();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSomethingDeprecationFixed()
|
||||||
|
{
|
||||||
|
$this->expectNoDeprecationWithIdentifier('https://github.com/doctrine/orm/issue/1234');
|
||||||
|
|
||||||
|
triggerTheCodeWithoutDeprecation();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## What is a deprecation identifier?
|
||||||
|
|
||||||
|
An identifier for deprecations is just a link to any resource, most often a
|
||||||
|
Github Issue or Pull Request explaining the deprecation and potentially its
|
||||||
|
alternative.
|
@ -0,0 +1,32 @@
|
|||||||
|
{
|
||||||
|
"name": "doctrine/deprecations",
|
||||||
|
"type": "library",
|
||||||
|
"description": "A small layer on top of trigger_error(E_USER_DEPRECATED) or PSR-3 logging with options to disable all deprecations or selectively for packages.",
|
||||||
|
"homepage": "https://www.doctrine-project.org/",
|
||||||
|
"license": "MIT",
|
||||||
|
"require": {
|
||||||
|
"php": "^7.1|^8.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^7.5|^8.5|^9.5",
|
||||||
|
"psr/log": "^1|^2|^3",
|
||||||
|
"doctrine/coding-standard": "^9"
|
||||||
|
},
|
||||||
|
"suggest": {
|
||||||
|
"psr/log": "Allows logging deprecations via PSR-3 logger implementation"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {"Doctrine\\Deprecations\\": "lib/Doctrine/Deprecations"}
|
||||||
|
},
|
||||||
|
"autoload-dev": {
|
||||||
|
"psr-4": {
|
||||||
|
"DeprecationTests\\": "test_fixtures/src",
|
||||||
|
"Doctrine\\Foo\\": "test_fixtures/vendor/doctrine/foo"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"allow-plugins": {
|
||||||
|
"dealerdirect/phpcodesniffer-composer-installer": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,266 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Doctrine\Deprecations;
|
||||||
|
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
|
use function array_key_exists;
|
||||||
|
use function array_reduce;
|
||||||
|
use function debug_backtrace;
|
||||||
|
use function sprintf;
|
||||||
|
use function strpos;
|
||||||
|
use function strrpos;
|
||||||
|
use function substr;
|
||||||
|
use function trigger_error;
|
||||||
|
|
||||||
|
use const DEBUG_BACKTRACE_IGNORE_ARGS;
|
||||||
|
use const DIRECTORY_SEPARATOR;
|
||||||
|
use const E_USER_DEPRECATED;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manages Deprecation logging in different ways.
|
||||||
|
*
|
||||||
|
* By default triggered exceptions are not logged.
|
||||||
|
*
|
||||||
|
* To enable different deprecation logging mechanisms you can call the
|
||||||
|
* following methods:
|
||||||
|
*
|
||||||
|
* - Minimal collection of deprecations via getTriggeredDeprecations()
|
||||||
|
* \Doctrine\Deprecations\Deprecation::enableTrackingDeprecations();
|
||||||
|
*
|
||||||
|
* - Uses @trigger_error with E_USER_DEPRECATED
|
||||||
|
* \Doctrine\Deprecations\Deprecation::enableWithTriggerError();
|
||||||
|
*
|
||||||
|
* - Sends deprecation messages via a PSR-3 logger
|
||||||
|
* \Doctrine\Deprecations\Deprecation::enableWithPsrLogger($logger);
|
||||||
|
*
|
||||||
|
* Packages that trigger deprecations should use the `trigger()` or
|
||||||
|
* `triggerIfCalledFromOutside()` methods.
|
||||||
|
*/
|
||||||
|
class Deprecation
|
||||||
|
{
|
||||||
|
private const TYPE_NONE = 0;
|
||||||
|
private const TYPE_TRACK_DEPRECATIONS = 1;
|
||||||
|
private const TYPE_TRIGGER_ERROR = 2;
|
||||||
|
private const TYPE_PSR_LOGGER = 4;
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
private static $type = self::TYPE_NONE;
|
||||||
|
|
||||||
|
/** @var LoggerInterface|null */
|
||||||
|
private static $logger;
|
||||||
|
|
||||||
|
/** @var array<string,bool> */
|
||||||
|
private static $ignoredPackages = [];
|
||||||
|
|
||||||
|
/** @var array<string,int> */
|
||||||
|
private static $ignoredLinks = [];
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
private static $deduplication = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trigger a deprecation for the given package and identfier.
|
||||||
|
*
|
||||||
|
* The link should point to a Github issue or Wiki entry detailing the
|
||||||
|
* deprecation. It is additionally used to de-duplicate the trigger of the
|
||||||
|
* same deprecation during a request.
|
||||||
|
*
|
||||||
|
* @param mixed $args
|
||||||
|
*/
|
||||||
|
public static function trigger(string $package, string $link, string $message, ...$args): void
|
||||||
|
{
|
||||||
|
if (self::$type === self::TYPE_NONE) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (array_key_exists($link, self::$ignoredLinks)) {
|
||||||
|
self::$ignoredLinks[$link]++;
|
||||||
|
} else {
|
||||||
|
self::$ignoredLinks[$link] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self::$deduplication === true && self::$ignoredLinks[$link] > 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset(self::$ignoredPackages[$package])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
|
||||||
|
|
||||||
|
$message = sprintf($message, ...$args);
|
||||||
|
|
||||||
|
self::delegateTriggerToBackend($message, $backtrace, $link, $package);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trigger a deprecation for the given package and identifier when called from outside.
|
||||||
|
*
|
||||||
|
* "Outside" means we assume that $package is currently installed as a
|
||||||
|
* dependency and the caller is not a file in that package. When $package
|
||||||
|
* is installed as a root package then deprecations triggered from the
|
||||||
|
* tests folder are also considered "outside".
|
||||||
|
*
|
||||||
|
* This deprecation method assumes that you are using Composer to install
|
||||||
|
* the dependency and are using the default /vendor/ folder and not a
|
||||||
|
* Composer plugin to change the install location. The assumption is also
|
||||||
|
* that $package is the exact composer packge name.
|
||||||
|
*
|
||||||
|
* Compared to {@link trigger()} this method causes some overhead when
|
||||||
|
* deprecation tracking is enabled even during deduplication, because it
|
||||||
|
* needs to call {@link debug_backtrace()}
|
||||||
|
*
|
||||||
|
* @param mixed $args
|
||||||
|
*/
|
||||||
|
public static function triggerIfCalledFromOutside(string $package, string $link, string $message, ...$args): void
|
||||||
|
{
|
||||||
|
if (self::$type === self::TYPE_NONE) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
|
||||||
|
|
||||||
|
// first check that the caller is not from a tests folder, in which case we always let deprecations pass
|
||||||
|
if (strpos($backtrace[1]['file'], DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR) === false) {
|
||||||
|
$path = DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . $package . DIRECTORY_SEPARATOR;
|
||||||
|
|
||||||
|
if (strpos($backtrace[0]['file'], $path) === false) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strpos($backtrace[1]['file'], $path) !== false) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (array_key_exists($link, self::$ignoredLinks)) {
|
||||||
|
self::$ignoredLinks[$link]++;
|
||||||
|
} else {
|
||||||
|
self::$ignoredLinks[$link] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self::$deduplication === true && self::$ignoredLinks[$link] > 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset(self::$ignoredPackages[$package])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$message = sprintf($message, ...$args);
|
||||||
|
|
||||||
|
self::delegateTriggerToBackend($message, $backtrace, $link, $package);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<mixed> $backtrace
|
||||||
|
*/
|
||||||
|
private static function delegateTriggerToBackend(string $message, array $backtrace, string $link, string $package): void
|
||||||
|
{
|
||||||
|
if ((self::$type & self::TYPE_PSR_LOGGER) > 0) {
|
||||||
|
$context = [
|
||||||
|
'file' => $backtrace[0]['file'],
|
||||||
|
'line' => $backtrace[0]['line'],
|
||||||
|
'package' => $package,
|
||||||
|
'link' => $link,
|
||||||
|
];
|
||||||
|
|
||||||
|
self::$logger->notice($message, $context);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! ((self::$type & self::TYPE_TRIGGER_ERROR) > 0)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$message .= sprintf(
|
||||||
|
' (%s:%d called by %s:%d, %s, package %s)',
|
||||||
|
self::basename($backtrace[0]['file']),
|
||||||
|
$backtrace[0]['line'],
|
||||||
|
self::basename($backtrace[1]['file']),
|
||||||
|
$backtrace[1]['line'],
|
||||||
|
$link,
|
||||||
|
$package
|
||||||
|
);
|
||||||
|
|
||||||
|
@trigger_error($message, E_USER_DEPRECATED);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A non-local-aware version of PHPs basename function.
|
||||||
|
*/
|
||||||
|
private static function basename(string $filename): string
|
||||||
|
{
|
||||||
|
$pos = strrpos($filename, DIRECTORY_SEPARATOR);
|
||||||
|
|
||||||
|
if ($pos === false) {
|
||||||
|
return $filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
return substr($filename, $pos + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function enableTrackingDeprecations(): void
|
||||||
|
{
|
||||||
|
self::$type |= self::TYPE_TRACK_DEPRECATIONS;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function enableWithTriggerError(): void
|
||||||
|
{
|
||||||
|
self::$type |= self::TYPE_TRIGGER_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function enableWithPsrLogger(LoggerInterface $logger): void
|
||||||
|
{
|
||||||
|
self::$type |= self::TYPE_PSR_LOGGER;
|
||||||
|
self::$logger = $logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function withoutDeduplication(): void
|
||||||
|
{
|
||||||
|
self::$deduplication = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function disable(): void
|
||||||
|
{
|
||||||
|
self::$type = self::TYPE_NONE;
|
||||||
|
self::$logger = null;
|
||||||
|
self::$deduplication = true;
|
||||||
|
|
||||||
|
foreach (self::$ignoredLinks as $link => $count) {
|
||||||
|
self::$ignoredLinks[$link] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function ignorePackage(string $packageName): void
|
||||||
|
{
|
||||||
|
self::$ignoredPackages[$packageName] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function ignoreDeprecations(string ...$links): void
|
||||||
|
{
|
||||||
|
foreach ($links as $link) {
|
||||||
|
self::$ignoredLinks[$link] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getUniqueTriggeredDeprecationsCount(): int
|
||||||
|
{
|
||||||
|
return array_reduce(self::$ignoredLinks, static function (int $carry, int $count) {
|
||||||
|
return $carry + $count;
|
||||||
|
}, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns each triggered deprecation link identifier and the amount of occurrences.
|
||||||
|
*
|
||||||
|
* @return array<string,int>
|
||||||
|
*/
|
||||||
|
public static function getTriggeredDeprecations(): array
|
||||||
|
{
|
||||||
|
return self::$ignoredLinks;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Doctrine\Deprecations\PHPUnit;
|
||||||
|
|
||||||
|
use Doctrine\Deprecations\Deprecation;
|
||||||
|
|
||||||
|
use function sprintf;
|
||||||
|
|
||||||
|
trait VerifyDeprecations
|
||||||
|
{
|
||||||
|
/** @var array<string,int> */
|
||||||
|
private $doctrineDeprecationsExpectations = [];
|
||||||
|
|
||||||
|
/** @var array<string,int> */
|
||||||
|
private $doctrineNoDeprecationsExpectations = [];
|
||||||
|
|
||||||
|
public function expectDeprecationWithIdentifier(string $identifier): void
|
||||||
|
{
|
||||||
|
$this->doctrineDeprecationsExpectations[$identifier] = Deprecation::getTriggeredDeprecations()[$identifier] ?? 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function expectNoDeprecationWithIdentifier(string $identifier): void
|
||||||
|
{
|
||||||
|
$this->doctrineNoDeprecationsExpectations[$identifier] = Deprecation::getTriggeredDeprecations()[$identifier] ?? 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @before
|
||||||
|
*/
|
||||||
|
public function enableDeprecationTracking(): void
|
||||||
|
{
|
||||||
|
Deprecation::enableTrackingDeprecations();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @after
|
||||||
|
*/
|
||||||
|
public function verifyDeprecationsAreTriggered(): void
|
||||||
|
{
|
||||||
|
foreach ($this->doctrineDeprecationsExpectations as $identifier => $expectation) {
|
||||||
|
$actualCount = Deprecation::getTriggeredDeprecations()[$identifier] ?? 0;
|
||||||
|
|
||||||
|
$this->assertTrue(
|
||||||
|
$actualCount > $expectation,
|
||||||
|
sprintf(
|
||||||
|
"Expected deprecation with identifier '%s' was not triggered by code executed in test.",
|
||||||
|
$identifier
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->doctrineNoDeprecationsExpectations as $identifier => $expectation) {
|
||||||
|
$actualCount = Deprecation::getTriggeredDeprecations()[$identifier] ?? 0;
|
||||||
|
|
||||||
|
$this->assertTrue(
|
||||||
|
$actualCount === $expectation,
|
||||||
|
sprintf(
|
||||||
|
"Expected deprecation with identifier '%s' was triggered by code executed in test, but expected not to.",
|
||||||
|
$identifier
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
<ruleset>
|
||||||
|
<arg name="basepath" value="."/>
|
||||||
|
<arg name="extensions" value="php"/>
|
||||||
|
<arg name="parallel" value="80"/>
|
||||||
|
<arg name="cache" value=".phpcs-cache"/>
|
||||||
|
<arg name="colors"/>
|
||||||
|
|
||||||
|
<!-- Ignore warnings, show progress of the run and show sniff names -->
|
||||||
|
<arg value="nps"/>
|
||||||
|
|
||||||
|
<config name="php_version" value="70100"/>
|
||||||
|
|
||||||
|
<!-- Directories to be checked -->
|
||||||
|
<file>lib</file>
|
||||||
|
<file>tests</file>
|
||||||
|
|
||||||
|
<!-- Include full Doctrine Coding Standard -->
|
||||||
|
<rule ref="Doctrine">
|
||||||
|
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint" />
|
||||||
|
</rule>
|
||||||
|
</ruleset>
|
@ -0,0 +1,19 @@
|
|||||||
|
Copyright (c) 2006-2018 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.
|
@ -0,0 +1,9 @@
|
|||||||
|
# Doctrine Lexer
|
||||||
|
|
||||||
|
[![Build Status](https://github.com/doctrine/lexer/workflows/Continuous%20Integration/badge.svg)](https://github.com/doctrine/lexer/actions)
|
||||||
|
|
||||||
|
Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
|
||||||
|
|
||||||
|
This lexer is used in Doctrine Annotations and in Doctrine ORM (DQL).
|
||||||
|
|
||||||
|
https://www.doctrine-project.org/projects/lexer.html
|
@ -0,0 +1,14 @@
|
|||||||
|
Note about upgrading: Doctrine uses static and runtime mechanisms to raise
|
||||||
|
awareness about deprecated code.
|
||||||
|
|
||||||
|
- Use of `@deprecated` docblock that is detected by IDEs (like PHPStorm) or
|
||||||
|
Static Analysis tools (like Psalm, phpstan)
|
||||||
|
- Use of our low-overhead runtime deprecation API, details:
|
||||||
|
https://github.com/doctrine/deprecations/
|
||||||
|
|
||||||
|
# Upgrade to 2.0.0
|
||||||
|
|
||||||
|
`AbstractLexer::glimpse()` and `AbstractLexer::peek()` now return
|
||||||
|
instances of `Doctrine\Common\Lexer\Token`, which is an array-like class
|
||||||
|
Using it as an array is deprecated in favor of using properties of that class.
|
||||||
|
Using `count()` on it is deprecated with no replacement.
|
@ -0,0 +1,56 @@
|
|||||||
|
{
|
||||||
|
"name": "doctrine/lexer",
|
||||||
|
"description": "PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.",
|
||||||
|
"license": "MIT",
|
||||||
|
"type": "library",
|
||||||
|
"keywords": [
|
||||||
|
"php",
|
||||||
|
"parser",
|
||||||
|
"lexer",
|
||||||
|
"annotations",
|
||||||
|
"docblock"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Guilherme Blanco",
|
||||||
|
"email": "guilhermeblanco@gmail.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Roman Borschel",
|
||||||
|
"email": "roman@code-factory.org"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Johannes Schmitt",
|
||||||
|
"email": "schmittjoh@gmail.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"homepage": "https://www.doctrine-project.org/projects/lexer.html",
|
||||||
|
"require": {
|
||||||
|
"php": "^7.1 || ^8.0",
|
||||||
|
"doctrine/deprecations": "^1.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"doctrine/coding-standard": "^9 || ^10",
|
||||||
|
"phpstan/phpstan": "^1.3",
|
||||||
|
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.5",
|
||||||
|
"psalm/plugin-phpunit": "^0.18.3",
|
||||||
|
"vimeo/psalm": "^4.11 || ^5.0"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Doctrine\\Common\\Lexer\\": "src"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload-dev": {
|
||||||
|
"psr-4": {
|
||||||
|
"Doctrine\\Tests\\Common\\Lexer\\": "tests"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"allow-plugins": {
|
||||||
|
"composer/package-versions-deprecated": true,
|
||||||
|
"dealerdirect/phpcodesniffer-composer-installer": true
|
||||||
|
},
|
||||||
|
"sort-packages": true
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,336 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Lexer;
|
||||||
|
|
||||||
|
use ReflectionClass;
|
||||||
|
use UnitEnum;
|
||||||
|
|
||||||
|
use function get_class;
|
||||||
|
use function implode;
|
||||||
|
use function preg_split;
|
||||||
|
use function sprintf;
|
||||||
|
use function substr;
|
||||||
|
|
||||||
|
use const PREG_SPLIT_DELIM_CAPTURE;
|
||||||
|
use const PREG_SPLIT_NO_EMPTY;
|
||||||
|
use const PREG_SPLIT_OFFSET_CAPTURE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base class for writing simple lexers, i.e. for creating small DSLs.
|
||||||
|
*
|
||||||
|
* @template T of UnitEnum|string|int
|
||||||
|
*/
|
||||||
|
abstract class AbstractLexer
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Lexer original input string.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $input;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Array of scanned tokens.
|
||||||
|
*
|
||||||
|
* @var list<Token<T>>
|
||||||
|
*/
|
||||||
|
private $tokens = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Current lexer position in input string.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $position = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Current peek of current lexer position.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $peek = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The next token in the input.
|
||||||
|
*
|
||||||
|
* @var mixed[]|null
|
||||||
|
* @psalm-var Token<T>|null
|
||||||
|
*/
|
||||||
|
public $lookahead;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The last matched/seen token.
|
||||||
|
*
|
||||||
|
* @var mixed[]|null
|
||||||
|
* @psalm-var Token<T>|null
|
||||||
|
*/
|
||||||
|
public $token;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Composed regex for input parsing.
|
||||||
|
*
|
||||||
|
* @var string|null
|
||||||
|
*/
|
||||||
|
private $regex;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the input data to be tokenized.
|
||||||
|
*
|
||||||
|
* The Lexer is immediately reset and the new input tokenized.
|
||||||
|
* Any unprocessed tokens from any previous input are lost.
|
||||||
|
*
|
||||||
|
* @param string $input The input to be tokenized.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setInput($input)
|
||||||
|
{
|
||||||
|
$this->input = $input;
|
||||||
|
$this->tokens = [];
|
||||||
|
|
||||||
|
$this->reset();
|
||||||
|
$this->scan($input);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets the lexer.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function reset()
|
||||||
|
{
|
||||||
|
$this->lookahead = null;
|
||||||
|
$this->token = null;
|
||||||
|
$this->peek = 0;
|
||||||
|
$this->position = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets the peek pointer to 0.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function resetPeek()
|
||||||
|
{
|
||||||
|
$this->peek = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets the lexer position on the input to the given position.
|
||||||
|
*
|
||||||
|
* @param int $position Position to place the lexical scanner.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function resetPosition($position = 0)
|
||||||
|
{
|
||||||
|
$this->position = $position;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the original lexer's input until a given position.
|
||||||
|
*
|
||||||
|
* @param int $position
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getInputUntilPosition($position)
|
||||||
|
{
|
||||||
|
return substr($this->input, 0, $position);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether a given token matches the current lookahead.
|
||||||
|
*
|
||||||
|
* @param T $type
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isNextToken($type)
|
||||||
|
{
|
||||||
|
return $this->lookahead !== null && $this->lookahead->isA($type);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether any of the given tokens matches the current lookahead.
|
||||||
|
*
|
||||||
|
* @param list<T> $types
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isNextTokenAny(array $types)
|
||||||
|
{
|
||||||
|
return $this->lookahead !== null && $this->lookahead->isA(...$types);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Moves to the next token in the input string.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function moveNext()
|
||||||
|
{
|
||||||
|
$this->peek = 0;
|
||||||
|
$this->token = $this->lookahead;
|
||||||
|
$this->lookahead = isset($this->tokens[$this->position])
|
||||||
|
? $this->tokens[$this->position++] : null;
|
||||||
|
|
||||||
|
return $this->lookahead !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tells the lexer to skip input tokens until it sees a token with the given value.
|
||||||
|
*
|
||||||
|
* @param T $type The token type to skip until.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function skipUntil($type)
|
||||||
|
{
|
||||||
|
while ($this->lookahead !== null && ! $this->lookahead->isA($type)) {
|
||||||
|
$this->moveNext();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if given value is identical to the given token.
|
||||||
|
*
|
||||||
|
* @param string $value
|
||||||
|
* @param int|string $token
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isA($value, $token)
|
||||||
|
{
|
||||||
|
return $this->getType($value) === $token;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Moves the lookahead token forward.
|
||||||
|
*
|
||||||
|
* @return mixed[]|null The next token or NULL if there are no more tokens ahead.
|
||||||
|
* @psalm-return Token<T>|null
|
||||||
|
*/
|
||||||
|
public function peek()
|
||||||
|
{
|
||||||
|
if (isset($this->tokens[$this->position + $this->peek])) {
|
||||||
|
return $this->tokens[$this->position + $this->peek++];
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Peeks at the next token, returns it and immediately resets the peek.
|
||||||
|
*
|
||||||
|
* @return mixed[]|null The next token or NULL if there are no more tokens ahead.
|
||||||
|
* @psalm-return Token<T>|null
|
||||||
|
*/
|
||||||
|
public function glimpse()
|
||||||
|
{
|
||||||
|
$peek = $this->peek();
|
||||||
|
$this->peek = 0;
|
||||||
|
|
||||||
|
return $peek;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scans the input string for tokens.
|
||||||
|
*
|
||||||
|
* @param string $input A query string.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function scan($input)
|
||||||
|
{
|
||||||
|
if (! isset($this->regex)) {
|
||||||
|
$this->regex = sprintf(
|
||||||
|
'/(%s)|%s/%s',
|
||||||
|
implode(')|(', $this->getCatchablePatterns()),
|
||||||
|
implode('|', $this->getNonCatchablePatterns()),
|
||||||
|
$this->getModifiers()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
|
||||||
|
$matches = preg_split($this->regex, $input, -1, $flags);
|
||||||
|
|
||||||
|
if ($matches === false) {
|
||||||
|
// Work around https://bugs.php.net/78122
|
||||||
|
$matches = [[$input, 0]];
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($matches as $match) {
|
||||||
|
// Must remain before 'value' assignment since it can change content
|
||||||
|
$type = $this->getType($match[0]);
|
||||||
|
|
||||||
|
$this->tokens[] = new Token(
|
||||||
|
$match[0],
|
||||||
|
$type,
|
||||||
|
$match[1]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the literal for a given token.
|
||||||
|
*
|
||||||
|
* @param T $token
|
||||||
|
*
|
||||||
|
* @return int|string
|
||||||
|
*/
|
||||||
|
public function getLiteral($token)
|
||||||
|
{
|
||||||
|
if ($token instanceof UnitEnum) {
|
||||||
|
return get_class($token) . '::' . $token->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
$className = static::class;
|
||||||
|
|
||||||
|
$reflClass = new ReflectionClass($className);
|
||||||
|
$constants = $reflClass->getConstants();
|
||||||
|
|
||||||
|
foreach ($constants as $name => $value) {
|
||||||
|
if ($value === $token) {
|
||||||
|
return $className . '::' . $name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $token;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Regex modifiers
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getModifiers()
|
||||||
|
{
|
||||||
|
return 'iu';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lexical catchable patterns.
|
||||||
|
*
|
||||||
|
* @return string[]
|
||||||
|
*/
|
||||||
|
abstract protected function getCatchablePatterns();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lexical non-catchable patterns.
|
||||||
|
*
|
||||||
|
* @return string[]
|
||||||
|
*/
|
||||||
|
abstract protected function getNonCatchablePatterns();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve token type. Also processes the token value if necessary.
|
||||||
|
*
|
||||||
|
* @param string $value
|
||||||
|
*
|
||||||
|
* @return T|null
|
||||||
|
*/
|
||||||
|
abstract protected function getType(&$value);
|
||||||
|
}
|
@ -0,0 +1,129 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Doctrine\Common\Lexer;
|
||||||
|
|
||||||
|
use ArrayAccess;
|
||||||
|
use Doctrine\Deprecations\Deprecation;
|
||||||
|
use ReturnTypeWillChange;
|
||||||
|
use UnitEnum;
|
||||||
|
|
||||||
|
use function in_array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @template T of UnitEnum|string|int
|
||||||
|
* @implements ArrayAccess<string,mixed>
|
||||||
|
*/
|
||||||
|
final class Token implements ArrayAccess
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The string value of the token in the input string
|
||||||
|
*
|
||||||
|
* @readonly
|
||||||
|
* @var string|int
|
||||||
|
*/
|
||||||
|
public $value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type of the token (identifier, numeric, string, input parameter, none)
|
||||||
|
*
|
||||||
|
* @readonly
|
||||||
|
* @var T|null
|
||||||
|
*/
|
||||||
|
public $type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The position of the token in the input string
|
||||||
|
*
|
||||||
|
* @readonly
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
public $position;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string|int $value
|
||||||
|
* @param T|null $type
|
||||||
|
*/
|
||||||
|
public function __construct($value, $type, int $position)
|
||||||
|
{
|
||||||
|
$this->value = $value;
|
||||||
|
$this->type = $type;
|
||||||
|
$this->position = $position;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param T ...$types */
|
||||||
|
public function isA(...$types): bool
|
||||||
|
{
|
||||||
|
return in_array($this->type, $types, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use the value, type or position property instead
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function offsetExists($offset): bool
|
||||||
|
{
|
||||||
|
Deprecation::trigger(
|
||||||
|
'doctrine/lexer',
|
||||||
|
'https://github.com/doctrine/lexer/pull/79',
|
||||||
|
'Accessing %s properties via ArrayAccess is deprecated, use the value, type or position property instead',
|
||||||
|
self::class
|
||||||
|
);
|
||||||
|
|
||||||
|
return in_array($offset, ['value', 'type', 'position'], true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use the value, type or position property instead
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* @param array-key $offset
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
#[ReturnTypeWillChange]
|
||||||
|
public function offsetGet($offset)
|
||||||
|
{
|
||||||
|
Deprecation::trigger(
|
||||||
|
'doctrine/lexer',
|
||||||
|
'https://github.com/doctrine/lexer/pull/79',
|
||||||
|
'Accessing %s properties via ArrayAccess is deprecated, use the value, type or position property instead',
|
||||||
|
self::class
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this->$offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated no replacement planned
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function offsetSet($offset, $value): void
|
||||||
|
{
|
||||||
|
Deprecation::trigger(
|
||||||
|
'doctrine/lexer',
|
||||||
|
'https://github.com/doctrine/lexer/pull/79',
|
||||||
|
'Setting %s properties via ArrayAccess is deprecated',
|
||||||
|
self::class
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->$offset = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated no replacement planned
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function offsetUnset($offset): void
|
||||||
|
{
|
||||||
|
Deprecation::trigger(
|
||||||
|
'doctrine/lexer',
|
||||||
|
'https://github.com/doctrine/lexer/pull/79',
|
||||||
|
'Setting %s properties via ArrayAccess is deprecated',
|
||||||
|
self::class
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->$offset = null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
All notable changes to this project will be documented in this file, in reverse chronological order by release.
|
||||||
|
|
||||||
|
## 1.0.1 - 2016-08-06
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- Make spacing consistent in phpdoc annotations php-fig/cache#9 - chalasr
|
||||||
|
- Fix grammar in phpdoc annotations php-fig/cache#10 - chalasr
|
||||||
|
- Be more specific in docblocks that `getItems()` and `deleteItems()` take an array of strings (`string[]`) compared to just `array` php-fig/cache#8 - GrahamCampbell
|
||||||
|
- For `expiresAt()` and `expiresAfter()` in CacheItemInterface fix docblock to specify null as a valid parameters as well as an implementation of DateTimeInterface php-fig/cache#7 - GrahamCampbell
|
||||||
|
|
||||||
|
## 1.0.0 - 2015-12-11
|
||||||
|
|
||||||
|
Initial stable release; reflects accepted PSR-6 specification
|
@ -0,0 +1,19 @@
|
|||||||
|
Copyright (c) 2015 PHP Framework Interoperability Group
|
||||||
|
|
||||||
|
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.
|
@ -0,0 +1,12 @@
|
|||||||
|
Caching Interface
|
||||||
|
==============
|
||||||
|
|
||||||
|
This repository holds all interfaces related to [PSR-6 (Caching Interface)][psr-url].
|
||||||
|
|
||||||
|
Note that this is not a Caching implementation of its own. It is merely interfaces that describe the components of a Caching mechanism.
|
||||||
|
|
||||||
|
The installable [package][package-url] and [implementations][implementation-url] are listed on Packagist.
|
||||||
|
|
||||||
|
[psr-url]: https://www.php-fig.org/psr/psr-6/
|
||||||
|
[package-url]: https://packagist.org/packages/psr/cache
|
||||||
|
[implementation-url]: https://packagist.org/providers/psr/cache-implementation
|
@ -0,0 +1,25 @@
|
|||||||
|
{
|
||||||
|
"name": "psr/cache",
|
||||||
|
"description": "Common interface for caching libraries",
|
||||||
|
"keywords": ["psr", "psr-6", "cache"],
|
||||||
|
"license": "MIT",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "PHP-FIG",
|
||||||
|
"homepage": "https://www.php-fig.org/"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"require": {
|
||||||
|
"php": ">=8.0.0"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Psr\\Cache\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"branch-alias": {
|
||||||
|
"dev-master": "1.0.x-dev"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Psr\Cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception interface for all exceptions thrown by an Implementing Library.
|
||||||
|
*/
|
||||||
|
interface CacheException extends \Throwable
|
||||||
|
{
|
||||||
|
}
|
@ -0,0 +1,105 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Psr\Cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CacheItemInterface defines an interface for interacting with objects inside a cache.
|
||||||
|
*
|
||||||
|
* Each Item object MUST be associated with a specific key, which can be set
|
||||||
|
* according to the implementing system and is typically passed by the
|
||||||
|
* Cache\CacheItemPoolInterface object.
|
||||||
|
*
|
||||||
|
* The Cache\CacheItemInterface object encapsulates the storage and retrieval of
|
||||||
|
* cache items. Each Cache\CacheItemInterface is generated by a
|
||||||
|
* Cache\CacheItemPoolInterface object, which is responsible for any required
|
||||||
|
* setup as well as associating the object with a unique Key.
|
||||||
|
* Cache\CacheItemInterface objects MUST be able to store and retrieve any type
|
||||||
|
* of PHP value defined in the Data section of the specification.
|
||||||
|
*
|
||||||
|
* Calling Libraries MUST NOT instantiate Item objects themselves. They may only
|
||||||
|
* be requested from a Pool object via the getItem() method. Calling Libraries
|
||||||
|
* SHOULD NOT assume that an Item created by one Implementing Library is
|
||||||
|
* compatible with a Pool from another Implementing Library.
|
||||||
|
*/
|
||||||
|
interface CacheItemInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns the key for the current cache item.
|
||||||
|
*
|
||||||
|
* The key is loaded by the Implementing Library, but should be available to
|
||||||
|
* the higher level callers when needed.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
* The key string for this cache item.
|
||||||
|
*/
|
||||||
|
public function getKey(): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the value of the item from the cache associated with this object's key.
|
||||||
|
*
|
||||||
|
* The value returned must be identical to the value originally stored by set().
|
||||||
|
*
|
||||||
|
* If isHit() returns false, this method MUST return null. Note that null
|
||||||
|
* is a legitimate cached value, so the isHit() method SHOULD be used to
|
||||||
|
* differentiate between "null value was found" and "no value was found."
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
* The value corresponding to this cache item's key, or null if not found.
|
||||||
|
*/
|
||||||
|
public function get(): mixed;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Confirms if the cache item lookup resulted in a cache hit.
|
||||||
|
*
|
||||||
|
* Note: This method MUST NOT have a race condition between calling isHit()
|
||||||
|
* and calling get().
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
* True if the request resulted in a cache hit. False otherwise.
|
||||||
|
*/
|
||||||
|
public function isHit(): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value represented by this cache item.
|
||||||
|
*
|
||||||
|
* The $value argument may be any item that can be serialized by PHP,
|
||||||
|
* although the method of serialization is left up to the Implementing
|
||||||
|
* Library.
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
* The serializable value to be stored.
|
||||||
|
*
|
||||||
|
* @return static
|
||||||
|
* The invoked object.
|
||||||
|
*/
|
||||||
|
public function set(mixed $value): static;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the expiration time for this cache item.
|
||||||
|
*
|
||||||
|
* @param ?\DateTimeInterface $expiration
|
||||||
|
* The point in time after which the item MUST be considered expired.
|
||||||
|
* If null is passed explicitly, a default value MAY be used. If none is set,
|
||||||
|
* the value should be stored permanently or for as long as the
|
||||||
|
* implementation allows.
|
||||||
|
*
|
||||||
|
* @return static
|
||||||
|
* The called object.
|
||||||
|
*/
|
||||||
|
public function expiresAt(?\DateTimeInterface $expiration): static;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the expiration time for this cache item.
|
||||||
|
*
|
||||||
|
* @param int|\DateInterval|null $time
|
||||||
|
* The period of time from the present after which the item MUST be considered
|
||||||
|
* expired. An integer parameter is understood to be the time in seconds until
|
||||||
|
* expiration. If null is passed explicitly, a default value MAY be used.
|
||||||
|
* If none is set, the value should be stored permanently or for as long as the
|
||||||
|
* implementation allows.
|
||||||
|
*
|
||||||
|
* @return static
|
||||||
|
* The called object.
|
||||||
|
*/
|
||||||
|
public function expiresAfter(int|\DateInterval|null $time): static;
|
||||||
|
}
|
@ -0,0 +1,138 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Psr\Cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CacheItemPoolInterface generates CacheItemInterface objects.
|
||||||
|
*
|
||||||
|
* The primary purpose of Cache\CacheItemPoolInterface is to accept a key from
|
||||||
|
* the Calling Library and return the associated Cache\CacheItemInterface object.
|
||||||
|
* It is also the primary point of interaction with the entire cache collection.
|
||||||
|
* All configuration and initialization of the Pool is left up to an
|
||||||
|
* Implementing Library.
|
||||||
|
*/
|
||||||
|
interface CacheItemPoolInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns a Cache Item representing the specified key.
|
||||||
|
*
|
||||||
|
* This method must always return a CacheItemInterface object, even in case of
|
||||||
|
* a cache miss. It MUST NOT return null.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* The key for which to return the corresponding Cache Item.
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
* If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
|
||||||
|
* MUST be thrown.
|
||||||
|
*
|
||||||
|
* @return CacheItemInterface
|
||||||
|
* The corresponding Cache Item.
|
||||||
|
*/
|
||||||
|
public function getItem(string $key): CacheItemInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a traversable set of cache items.
|
||||||
|
*
|
||||||
|
* @param string[] $keys
|
||||||
|
* An indexed array of keys of items to retrieve.
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
* If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
|
||||||
|
* MUST be thrown.
|
||||||
|
*
|
||||||
|
* @return iterable
|
||||||
|
* An iterable collection of Cache Items keyed by the cache keys of
|
||||||
|
* each item. A Cache item will be returned for each key, even if that
|
||||||
|
* key is not found. However, if no keys are specified then an empty
|
||||||
|
* traversable MUST be returned instead.
|
||||||
|
*/
|
||||||
|
public function getItems(array $keys = []): iterable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Confirms if the cache contains specified cache item.
|
||||||
|
*
|
||||||
|
* Note: This method MAY avoid retrieving the cached value for performance reasons.
|
||||||
|
* This could result in a race condition with CacheItemInterface::get(). To avoid
|
||||||
|
* such situation use CacheItemInterface::isHit() instead.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* The key for which to check existence.
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
* If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
|
||||||
|
* MUST be thrown.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
* True if item exists in the cache, false otherwise.
|
||||||
|
*/
|
||||||
|
public function hasItem(string $key): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes all items in the pool.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
* True if the pool was successfully cleared. False if there was an error.
|
||||||
|
*/
|
||||||
|
public function clear(): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the item from the pool.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* The key to delete.
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
* If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
|
||||||
|
* MUST be thrown.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
* True if the item was successfully removed. False if there was an error.
|
||||||
|
*/
|
||||||
|
public function deleteItem(string $key): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes multiple items from the pool.
|
||||||
|
*
|
||||||
|
* @param string[] $keys
|
||||||
|
* An array of keys that should be removed from the pool.
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
* If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
|
||||||
|
* MUST be thrown.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
* True if the items were successfully removed. False if there was an error.
|
||||||
|
*/
|
||||||
|
public function deleteItems(array $keys): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persists a cache item immediately.
|
||||||
|
*
|
||||||
|
* @param CacheItemInterface $item
|
||||||
|
* The cache item to save.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
* True if the item was successfully persisted. False if there was an error.
|
||||||
|
*/
|
||||||
|
public function save(CacheItemInterface $item): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a cache item to be persisted later.
|
||||||
|
*
|
||||||
|
* @param CacheItemInterface $item
|
||||||
|
* The cache item to save.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
* False if the item could not be queued or if a commit was attempted and failed. True otherwise.
|
||||||
|
*/
|
||||||
|
public function saveDeferred(CacheItemInterface $item): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persists any deferred cache items.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
* True if all not-yet-saved items were successfully saved or there were none. False otherwise.
|
||||||
|
*/
|
||||||
|
public function commit(): bool;
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Psr\Cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception interface for invalid cache arguments.
|
||||||
|
*
|
||||||
|
* Any time an invalid argument is passed into a method it must throw an
|
||||||
|
* exception class which implements Psr\Cache\InvalidArgumentException.
|
||||||
|
*/
|
||||||
|
interface InvalidArgumentException extends CacheException
|
||||||
|
{
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
CHANGELOG
|
||||||
|
=========
|
||||||
|
|
||||||
|
The changelog is maintained for all Symfony contracts at the following URL:
|
||||||
|
https://github.com/symfony/contracts/blob/main/CHANGELOG.md
|
@ -0,0 +1,19 @@
|
|||||||
|
Copyright (c) 2020-2022 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.
|
@ -0,0 +1,26 @@
|
|||||||
|
Symfony Deprecation Contracts
|
||||||
|
=============================
|
||||||
|
|
||||||
|
A generic function and convention to trigger deprecation notices.
|
||||||
|
|
||||||
|
This package provides a single global function named `trigger_deprecation()` that triggers silenced deprecation notices.
|
||||||
|
|
||||||
|
By using a custom PHP error handler such as the one provided by the Symfony ErrorHandler component,
|
||||||
|
the triggered deprecations can be caught and logged for later discovery, both on dev and prod environments.
|
||||||
|
|
||||||
|
The function requires at least 3 arguments:
|
||||||
|
- the name of the Composer package that is triggering the deprecation
|
||||||
|
- the version of the package that introduced the deprecation
|
||||||
|
- the message of the deprecation
|
||||||
|
- more arguments can be provided: they will be inserted in the message using `printf()` formatting
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```php
|
||||||
|
trigger_deprecation('symfony/blockchain', '8.9', 'Using "%s" is deprecated, use "%s" instead.', 'bitcoin', 'fabcoin');
|
||||||
|
```
|
||||||
|
|
||||||
|
This will generate the following message:
|
||||||
|
`Since symfony/blockchain 8.9: Using "bitcoin" is deprecated, use "fabcoin" instead.`
|
||||||
|
|
||||||
|
While not necessarily recommended, the deprecation notices can be completely ignored by declaring an empty
|
||||||
|
`function trigger_deprecation() {}` in your application.
|
@ -0,0 +1,35 @@
|
|||||||
|
{
|
||||||
|
"name": "symfony/deprecation-contracts",
|
||||||
|
"type": "library",
|
||||||
|
"description": "A generic function and convention to trigger deprecation notices",
|
||||||
|
"homepage": "https://symfony.com",
|
||||||
|
"license": "MIT",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Nicolas Grekas",
|
||||||
|
"email": "p@tchwork.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Symfony Community",
|
||||||
|
"homepage": "https://symfony.com/contributors"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"require": {
|
||||||
|
"php": ">=8.1"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"files": [
|
||||||
|
"function.php"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"minimum-stability": "dev",
|
||||||
|
"extra": {
|
||||||
|
"branch-alias": {
|
||||||
|
"dev-main": "3.3-dev"
|
||||||
|
},
|
||||||
|
"thanks": {
|
||||||
|
"name": "symfony/contracts",
|
||||||
|
"url": "https://github.com/symfony/contracts"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!function_exists('trigger_deprecation')) {
|
||||||
|
/**
|
||||||
|
* Triggers a silenced deprecation notice.
|
||||||
|
*
|
||||||
|
* @param string $package The name of the Composer package that is triggering the deprecation
|
||||||
|
* @param string $version The version of the package that introduced the deprecation
|
||||||
|
* @param string $message The message of the deprecation
|
||||||
|
* @param mixed ...$args Values to insert in the message using printf() formatting
|
||||||
|
*
|
||||||
|
* @author Nicolas Grekas <p@tchwork.com>
|
||||||
|
*/
|
||||||
|
function trigger_deprecation(string $package, string $version, string $message, mixed ...$args): void
|
||||||
|
{
|
||||||
|
@trigger_error(($package || $version ? "Since $package $version: " : '').($args ? vsprintf($message, $args) : $message), \E_USER_DEPRECATED);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,98 @@
|
|||||||
|
CHANGELOG
|
||||||
|
=========
|
||||||
|
|
||||||
|
6.2
|
||||||
|
---
|
||||||
|
|
||||||
|
* Add `Finder::sortByExtension()` and `Finder::sortBySize()`
|
||||||
|
* Add `Finder::sortByCaseInsensitiveName()` to sort by name with case insensitive sorting methods
|
||||||
|
|
||||||
|
6.0
|
||||||
|
---
|
||||||
|
|
||||||
|
* Remove `Comparator::setTarget()` and `Comparator::setOperator()`
|
||||||
|
|
||||||
|
5.4.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Deprecate `Comparator::setTarget()` and `Comparator::setOperator()`
|
||||||
|
* Add a constructor to `Comparator` that allows setting target and operator
|
||||||
|
* Finder's iterator has now `Symfony\Component\Finder\SplFileInfo` inner type specified
|
||||||
|
* Add recursive .gitignore files support
|
||||||
|
|
||||||
|
5.0.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* added `$useNaturalSort` argument to `Finder::sortByName()`
|
||||||
|
|
||||||
|
4.3.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* added Finder::ignoreVCSIgnored() to ignore files based on rules listed in .gitignore
|
||||||
|
|
||||||
|
4.2.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* added $useNaturalSort option to Finder::sortByName() method
|
||||||
|
* the `Finder::sortByName()` method will have a new `$useNaturalSort`
|
||||||
|
argument in version 5.0, not defining it is deprecated
|
||||||
|
* added `Finder::reverseSorting()` to reverse the sorting
|
||||||
|
|
||||||
|
4.0.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* removed `ExceptionInterface`
|
||||||
|
* removed `Symfony\Component\Finder\Iterator\FilterIterator`
|
||||||
|
|
||||||
|
3.4.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* deprecated `Symfony\Component\Finder\Iterator\FilterIterator`
|
||||||
|
* added Finder::hasResults() method to check if any results were found
|
||||||
|
|
||||||
|
3.3.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* added double-star matching to Glob::toRegex()
|
||||||
|
|
||||||
|
3.0.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* removed deprecated classes
|
||||||
|
|
||||||
|
2.8.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* deprecated adapters and related classes
|
||||||
|
|
||||||
|
2.5.0
|
||||||
|
-----
|
||||||
|
* added support for GLOB_BRACE in the paths passed to Finder::in()
|
||||||
|
|
||||||
|
2.3.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* added a way to ignore unreadable directories (via Finder::ignoreUnreadableDirs())
|
||||||
|
* unified the way subfolders that are not executable are handled by always throwing an AccessDeniedException exception
|
||||||
|
|
||||||
|
2.2.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* added Finder::path() and Finder::notPath() methods
|
||||||
|
* added finder adapters to improve performance on specific platforms
|
||||||
|
* added support for wildcard characters (glob patterns) in the paths passed
|
||||||
|
to Finder::in()
|
||||||
|
|
||||||
|
2.1.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* added Finder::sortByAccessedTime(), Finder::sortByChangedTime(), and
|
||||||
|
Finder::sortByModifiedTime()
|
||||||
|
* added Countable to Finder
|
||||||
|
* added support for an array of directories as an argument to
|
||||||
|
Finder::exclude()
|
||||||
|
* added searching based on the file content via Finder::contains() and
|
||||||
|
Finder::notContains()
|
||||||
|
* added support for the != operator in the Comparator
|
||||||
|
* [BC BREAK] filter expressions (used for file name and content) are no more
|
||||||
|
considered as regexps but glob patterns when they are enclosed in '*' or '?'
|
@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Comparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*/
|
||||||
|
class Comparator
|
||||||
|
{
|
||||||
|
private string $target;
|
||||||
|
private string $operator;
|
||||||
|
|
||||||
|
public function __construct(string $target, string $operator = '==')
|
||||||
|
{
|
||||||
|
if (!\in_array($operator, ['>', '<', '>=', '<=', '==', '!='])) {
|
||||||
|
throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator));
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->target = $target;
|
||||||
|
$this->operator = $operator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the target value.
|
||||||
|
*/
|
||||||
|
public function getTarget(): string
|
||||||
|
{
|
||||||
|
return $this->target;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the comparison operator.
|
||||||
|
*/
|
||||||
|
public function getOperator(): string
|
||||||
|
{
|
||||||
|
return $this->operator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests against the target.
|
||||||
|
*/
|
||||||
|
public function test(mixed $test): bool
|
||||||
|
{
|
||||||
|
return match ($this->operator) {
|
||||||
|
'>' => $test > $this->target,
|
||||||
|
'>=' => $test >= $this->target,
|
||||||
|
'<' => $test < $this->target,
|
||||||
|
'<=' => $test <= $this->target,
|
||||||
|
'!=' => $test != $this->target,
|
||||||
|
default => $test == $this->target,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Comparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DateCompare compiles date comparisons.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*/
|
||||||
|
class DateComparator extends Comparator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param string $test A comparison string
|
||||||
|
*
|
||||||
|
* @throws \InvalidArgumentException If the test is not understood
|
||||||
|
*/
|
||||||
|
public function __construct(string $test)
|
||||||
|
{
|
||||||
|
if (!preg_match('#^\s*(==|!=|[<>]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) {
|
||||||
|
throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a date test.', $test));
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$date = new \DateTimeImmutable($matches[2]);
|
||||||
|
$target = $date->format('U');
|
||||||
|
} catch (\Exception) {
|
||||||
|
throw new \InvalidArgumentException(sprintf('"%s" is not a valid date.', $matches[2]));
|
||||||
|
}
|
||||||
|
|
||||||
|
$operator = $matches[1] ?? '==';
|
||||||
|
if ('since' === $operator || 'after' === $operator) {
|
||||||
|
$operator = '>';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ('until' === $operator || 'before' === $operator) {
|
||||||
|
$operator = '<';
|
||||||
|
}
|
||||||
|
|
||||||
|
parent::__construct($target, $operator);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Comparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NumberComparator compiles a simple comparison to an anonymous
|
||||||
|
* subroutine, which you can call with a value to be tested again.
|
||||||
|
*
|
||||||
|
* Now this would be very pointless, if NumberCompare didn't understand
|
||||||
|
* magnitudes.
|
||||||
|
*
|
||||||
|
* The target value may use magnitudes of kilobytes (k, ki),
|
||||||
|
* megabytes (m, mi), or gigabytes (g, gi). Those suffixed
|
||||||
|
* with an i use the appropriate 2**n version in accordance with the
|
||||||
|
* IEC standard: http://physics.nist.gov/cuu/Units/binary.html
|
||||||
|
*
|
||||||
|
* Based on the Perl Number::Compare module.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com> PHP port
|
||||||
|
* @author Richard Clamp <richardc@unixbeard.net> Perl version
|
||||||
|
* @copyright 2004-2005 Fabien Potencier <fabien@symfony.com>
|
||||||
|
* @copyright 2002 Richard Clamp <richardc@unixbeard.net>
|
||||||
|
*
|
||||||
|
* @see http://physics.nist.gov/cuu/Units/binary.html
|
||||||
|
*/
|
||||||
|
class NumberComparator extends Comparator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param string|int $test A comparison string or an integer
|
||||||
|
*
|
||||||
|
* @throws \InvalidArgumentException If the test is not understood
|
||||||
|
*/
|
||||||
|
public function __construct(?string $test)
|
||||||
|
{
|
||||||
|
if (null === $test || !preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) {
|
||||||
|
throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $test ?? 'null'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$target = $matches[2];
|
||||||
|
if (!is_numeric($target)) {
|
||||||
|
throw new \InvalidArgumentException(sprintf('Invalid number "%s".', $target));
|
||||||
|
}
|
||||||
|
if (isset($matches[3])) {
|
||||||
|
// magnitude
|
||||||
|
switch (strtolower($matches[3])) {
|
||||||
|
case 'k':
|
||||||
|
$target *= 1000;
|
||||||
|
break;
|
||||||
|
case 'ki':
|
||||||
|
$target *= 1024;
|
||||||
|
break;
|
||||||
|
case 'm':
|
||||||
|
$target *= 1000000;
|
||||||
|
break;
|
||||||
|
case 'mi':
|
||||||
|
$target *= 1024 * 1024;
|
||||||
|
break;
|
||||||
|
case 'g':
|
||||||
|
$target *= 1000000000;
|
||||||
|
break;
|
||||||
|
case 'gi':
|
||||||
|
$target *= 1024 * 1024 * 1024;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parent::__construct($target, $matches[1] ?: '==');
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
|
||||||
|
*/
|
||||||
|
class AccessDeniedException extends \UnexpectedValueException
|
||||||
|
{
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Andreas Erhard <andreas.erhard@i-med.ac.at>
|
||||||
|
*/
|
||||||
|
class DirectoryNotFoundException extends \InvalidArgumentException
|
||||||
|
{
|
||||||
|
}
|
@ -0,0 +1,846 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder;
|
||||||
|
|
||||||
|
use Symfony\Component\Finder\Comparator\DateComparator;
|
||||||
|
use Symfony\Component\Finder\Comparator\NumberComparator;
|
||||||
|
use Symfony\Component\Finder\Exception\DirectoryNotFoundException;
|
||||||
|
use Symfony\Component\Finder\Iterator\CustomFilterIterator;
|
||||||
|
use Symfony\Component\Finder\Iterator\DateRangeFilterIterator;
|
||||||
|
use Symfony\Component\Finder\Iterator\DepthRangeFilterIterator;
|
||||||
|
use Symfony\Component\Finder\Iterator\ExcludeDirectoryFilterIterator;
|
||||||
|
use Symfony\Component\Finder\Iterator\FilecontentFilterIterator;
|
||||||
|
use Symfony\Component\Finder\Iterator\FilenameFilterIterator;
|
||||||
|
use Symfony\Component\Finder\Iterator\LazyIterator;
|
||||||
|
use Symfony\Component\Finder\Iterator\SizeRangeFilterIterator;
|
||||||
|
use Symfony\Component\Finder\Iterator\SortableIterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finder allows to build rules to find files and directories.
|
||||||
|
*
|
||||||
|
* It is a thin wrapper around several specialized iterator classes.
|
||||||
|
*
|
||||||
|
* All rules may be invoked several times.
|
||||||
|
*
|
||||||
|
* All methods return the current Finder object to allow chaining:
|
||||||
|
*
|
||||||
|
* $finder = Finder::create()->files()->name('*.php')->in(__DIR__);
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* @implements \IteratorAggregate<string, SplFileInfo>
|
||||||
|
*/
|
||||||
|
class Finder implements \IteratorAggregate, \Countable
|
||||||
|
{
|
||||||
|
public const IGNORE_VCS_FILES = 1;
|
||||||
|
public const IGNORE_DOT_FILES = 2;
|
||||||
|
public const IGNORE_VCS_IGNORED_FILES = 4;
|
||||||
|
|
||||||
|
private int $mode = 0;
|
||||||
|
private array $names = [];
|
||||||
|
private array $notNames = [];
|
||||||
|
private array $exclude = [];
|
||||||
|
private array $filters = [];
|
||||||
|
private array $depths = [];
|
||||||
|
private array $sizes = [];
|
||||||
|
private bool $followLinks = false;
|
||||||
|
private bool $reverseSorting = false;
|
||||||
|
private \Closure|int|false $sort = false;
|
||||||
|
private int $ignore = 0;
|
||||||
|
private array $dirs = [];
|
||||||
|
private array $dates = [];
|
||||||
|
private array $iterators = [];
|
||||||
|
private array $contains = [];
|
||||||
|
private array $notContains = [];
|
||||||
|
private array $paths = [];
|
||||||
|
private array $notPaths = [];
|
||||||
|
private bool $ignoreUnreadableDirs = false;
|
||||||
|
|
||||||
|
private static array $vcsPatterns = ['.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg'];
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->ignore = static::IGNORE_VCS_FILES | static::IGNORE_DOT_FILES;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new Finder.
|
||||||
|
*/
|
||||||
|
public static function create(): static
|
||||||
|
{
|
||||||
|
return new static();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restricts the matching to directories only.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function directories(): static
|
||||||
|
{
|
||||||
|
$this->mode = Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restricts the matching to files only.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function files(): static
|
||||||
|
{
|
||||||
|
$this->mode = Iterator\FileTypeFilterIterator::ONLY_FILES;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds tests for the directory depth.
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
*
|
||||||
|
* $finder->depth('> 1') // the Finder will start matching at level 1.
|
||||||
|
* $finder->depth('< 3') // the Finder will descend at most 3 levels of directories below the starting point.
|
||||||
|
* $finder->depth(['>= 1', '< 3'])
|
||||||
|
*
|
||||||
|
* @param string|int|string[]|int[] $levels The depth level expression or an array of depth levels
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see DepthRangeFilterIterator
|
||||||
|
* @see NumberComparator
|
||||||
|
*/
|
||||||
|
public function depth(string|int|array $levels): static
|
||||||
|
{
|
||||||
|
foreach ((array) $levels as $level) {
|
||||||
|
$this->depths[] = new Comparator\NumberComparator($level);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds tests for file dates (last modified).
|
||||||
|
*
|
||||||
|
* The date must be something that strtotime() is able to parse:
|
||||||
|
*
|
||||||
|
* $finder->date('since yesterday');
|
||||||
|
* $finder->date('until 2 days ago');
|
||||||
|
* $finder->date('> now - 2 hours');
|
||||||
|
* $finder->date('>= 2005-10-15');
|
||||||
|
* $finder->date(['>= 2005-10-15', '<= 2006-05-27']);
|
||||||
|
*
|
||||||
|
* @param string|string[] $dates A date range string or an array of date ranges
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see strtotime
|
||||||
|
* @see DateRangeFilterIterator
|
||||||
|
* @see DateComparator
|
||||||
|
*/
|
||||||
|
public function date(string|array $dates): static
|
||||||
|
{
|
||||||
|
foreach ((array) $dates as $date) {
|
||||||
|
$this->dates[] = new Comparator\DateComparator($date);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds rules that files must match.
|
||||||
|
*
|
||||||
|
* You can use patterns (delimited with / sign), globs or simple strings.
|
||||||
|
*
|
||||||
|
* $finder->name('*.php')
|
||||||
|
* $finder->name('/\.php$/') // same as above
|
||||||
|
* $finder->name('test.php')
|
||||||
|
* $finder->name(['test.py', 'test.php'])
|
||||||
|
*
|
||||||
|
* @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see FilenameFilterIterator
|
||||||
|
*/
|
||||||
|
public function name(string|array $patterns): static
|
||||||
|
{
|
||||||
|
$this->names = array_merge($this->names, (array) $patterns);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds rules that files must not match.
|
||||||
|
*
|
||||||
|
* @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see FilenameFilterIterator
|
||||||
|
*/
|
||||||
|
public function notName(string|array $patterns): static
|
||||||
|
{
|
||||||
|
$this->notNames = array_merge($this->notNames, (array) $patterns);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds tests that file contents must match.
|
||||||
|
*
|
||||||
|
* Strings or PCRE patterns can be used:
|
||||||
|
*
|
||||||
|
* $finder->contains('Lorem ipsum')
|
||||||
|
* $finder->contains('/Lorem ipsum/i')
|
||||||
|
* $finder->contains(['dolor', '/ipsum/i'])
|
||||||
|
*
|
||||||
|
* @param string|string[] $patterns A pattern (string or regexp) or an array of patterns
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see FilecontentFilterIterator
|
||||||
|
*/
|
||||||
|
public function contains(string|array $patterns): static
|
||||||
|
{
|
||||||
|
$this->contains = array_merge($this->contains, (array) $patterns);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds tests that file contents must not match.
|
||||||
|
*
|
||||||
|
* Strings or PCRE patterns can be used:
|
||||||
|
*
|
||||||
|
* $finder->notContains('Lorem ipsum')
|
||||||
|
* $finder->notContains('/Lorem ipsum/i')
|
||||||
|
* $finder->notContains(['lorem', '/dolor/i'])
|
||||||
|
*
|
||||||
|
* @param string|string[] $patterns A pattern (string or regexp) or an array of patterns
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see FilecontentFilterIterator
|
||||||
|
*/
|
||||||
|
public function notContains(string|array $patterns): static
|
||||||
|
{
|
||||||
|
$this->notContains = array_merge($this->notContains, (array) $patterns);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds rules that filenames must match.
|
||||||
|
*
|
||||||
|
* You can use patterns (delimited with / sign) or simple strings.
|
||||||
|
*
|
||||||
|
* $finder->path('some/special/dir')
|
||||||
|
* $finder->path('/some\/special\/dir/') // same as above
|
||||||
|
* $finder->path(['some dir', 'another/dir'])
|
||||||
|
*
|
||||||
|
* Use only / as dirname separator.
|
||||||
|
*
|
||||||
|
* @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see FilenameFilterIterator
|
||||||
|
*/
|
||||||
|
public function path(string|array $patterns): static
|
||||||
|
{
|
||||||
|
$this->paths = array_merge($this->paths, (array) $patterns);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds rules that filenames must not match.
|
||||||
|
*
|
||||||
|
* You can use patterns (delimited with / sign) or simple strings.
|
||||||
|
*
|
||||||
|
* $finder->notPath('some/special/dir')
|
||||||
|
* $finder->notPath('/some\/special\/dir/') // same as above
|
||||||
|
* $finder->notPath(['some/file.txt', 'another/file.log'])
|
||||||
|
*
|
||||||
|
* Use only / as dirname separator.
|
||||||
|
*
|
||||||
|
* @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see FilenameFilterIterator
|
||||||
|
*/
|
||||||
|
public function notPath(string|array $patterns): static
|
||||||
|
{
|
||||||
|
$this->notPaths = array_merge($this->notPaths, (array) $patterns);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds tests for file sizes.
|
||||||
|
*
|
||||||
|
* $finder->size('> 10K');
|
||||||
|
* $finder->size('<= 1Ki');
|
||||||
|
* $finder->size(4);
|
||||||
|
* $finder->size(['> 10K', '< 20K'])
|
||||||
|
*
|
||||||
|
* @param string|int|string[]|int[] $sizes A size range string or an integer or an array of size ranges
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see SizeRangeFilterIterator
|
||||||
|
* @see NumberComparator
|
||||||
|
*/
|
||||||
|
public function size(string|int|array $sizes): static
|
||||||
|
{
|
||||||
|
foreach ((array) $sizes as $size) {
|
||||||
|
$this->sizes[] = new Comparator\NumberComparator($size);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Excludes directories.
|
||||||
|
*
|
||||||
|
* Directories passed as argument must be relative to the ones defined with the `in()` method. For example:
|
||||||
|
*
|
||||||
|
* $finder->in(__DIR__)->exclude('ruby');
|
||||||
|
*
|
||||||
|
* @param string|array $dirs A directory path or an array of directories
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see ExcludeDirectoryFilterIterator
|
||||||
|
*/
|
||||||
|
public function exclude(string|array $dirs): static
|
||||||
|
{
|
||||||
|
$this->exclude = array_merge($this->exclude, (array) $dirs);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Excludes "hidden" directories and files (starting with a dot).
|
||||||
|
*
|
||||||
|
* This option is enabled by default.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see ExcludeDirectoryFilterIterator
|
||||||
|
*/
|
||||||
|
public function ignoreDotFiles(bool $ignoreDotFiles): static
|
||||||
|
{
|
||||||
|
if ($ignoreDotFiles) {
|
||||||
|
$this->ignore |= static::IGNORE_DOT_FILES;
|
||||||
|
} else {
|
||||||
|
$this->ignore &= ~static::IGNORE_DOT_FILES;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Forces the finder to ignore version control directories.
|
||||||
|
*
|
||||||
|
* This option is enabled by default.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see ExcludeDirectoryFilterIterator
|
||||||
|
*/
|
||||||
|
public function ignoreVCS(bool $ignoreVCS): static
|
||||||
|
{
|
||||||
|
if ($ignoreVCS) {
|
||||||
|
$this->ignore |= static::IGNORE_VCS_FILES;
|
||||||
|
} else {
|
||||||
|
$this->ignore &= ~static::IGNORE_VCS_FILES;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Forces Finder to obey .gitignore and ignore files based on rules listed there.
|
||||||
|
*
|
||||||
|
* This option is disabled by default.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function ignoreVCSIgnored(bool $ignoreVCSIgnored): static
|
||||||
|
{
|
||||||
|
if ($ignoreVCSIgnored) {
|
||||||
|
$this->ignore |= static::IGNORE_VCS_IGNORED_FILES;
|
||||||
|
} else {
|
||||||
|
$this->ignore &= ~static::IGNORE_VCS_IGNORED_FILES;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds VCS patterns.
|
||||||
|
*
|
||||||
|
* @see ignoreVCS()
|
||||||
|
*
|
||||||
|
* @param string|string[] $pattern VCS patterns to ignore
|
||||||
|
*/
|
||||||
|
public static function addVCSPattern(string|array $pattern)
|
||||||
|
{
|
||||||
|
foreach ((array) $pattern as $p) {
|
||||||
|
self::$vcsPatterns[] = $p;
|
||||||
|
}
|
||||||
|
|
||||||
|
self::$vcsPatterns = array_unique(self::$vcsPatterns);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts files and directories by an anonymous function.
|
||||||
|
*
|
||||||
|
* The anonymous function receives two \SplFileInfo instances to compare.
|
||||||
|
*
|
||||||
|
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see SortableIterator
|
||||||
|
*/
|
||||||
|
public function sort(\Closure $closure): static
|
||||||
|
{
|
||||||
|
$this->sort = $closure;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts files and directories by extension.
|
||||||
|
*
|
||||||
|
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see SortableIterator
|
||||||
|
*/
|
||||||
|
public function sortByExtension(): static
|
||||||
|
{
|
||||||
|
$this->sort = Iterator\SortableIterator::SORT_BY_EXTENSION;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts files and directories by name.
|
||||||
|
*
|
||||||
|
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see SortableIterator
|
||||||
|
*/
|
||||||
|
public function sortByName(bool $useNaturalSort = false): static
|
||||||
|
{
|
||||||
|
$this->sort = $useNaturalSort ? Iterator\SortableIterator::SORT_BY_NAME_NATURAL : Iterator\SortableIterator::SORT_BY_NAME;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts files and directories by name case insensitive.
|
||||||
|
*
|
||||||
|
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see SortableIterator
|
||||||
|
*/
|
||||||
|
public function sortByCaseInsensitiveName(bool $useNaturalSort = false): static
|
||||||
|
{
|
||||||
|
$this->sort = $useNaturalSort ? Iterator\SortableIterator::SORT_BY_NAME_NATURAL_CASE_INSENSITIVE : Iterator\SortableIterator::SORT_BY_NAME_CASE_INSENSITIVE;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts files and directories by size.
|
||||||
|
*
|
||||||
|
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see SortableIterator
|
||||||
|
*/
|
||||||
|
public function sortBySize(): static
|
||||||
|
{
|
||||||
|
$this->sort = Iterator\SortableIterator::SORT_BY_SIZE;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts files and directories by type (directories before files), then by name.
|
||||||
|
*
|
||||||
|
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see SortableIterator
|
||||||
|
*/
|
||||||
|
public function sortByType(): static
|
||||||
|
{
|
||||||
|
$this->sort = Iterator\SortableIterator::SORT_BY_TYPE;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts files and directories by the last accessed time.
|
||||||
|
*
|
||||||
|
* This is the time that the file was last accessed, read or written to.
|
||||||
|
*
|
||||||
|
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see SortableIterator
|
||||||
|
*/
|
||||||
|
public function sortByAccessedTime(): static
|
||||||
|
{
|
||||||
|
$this->sort = Iterator\SortableIterator::SORT_BY_ACCESSED_TIME;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverses the sorting.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function reverseSorting(): static
|
||||||
|
{
|
||||||
|
$this->reverseSorting = true;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts files and directories by the last inode changed time.
|
||||||
|
*
|
||||||
|
* This is the time that the inode information was last modified (permissions, owner, group or other metadata).
|
||||||
|
*
|
||||||
|
* On Windows, since inode is not available, changed time is actually the file creation time.
|
||||||
|
*
|
||||||
|
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see SortableIterator
|
||||||
|
*/
|
||||||
|
public function sortByChangedTime(): static
|
||||||
|
{
|
||||||
|
$this->sort = Iterator\SortableIterator::SORT_BY_CHANGED_TIME;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts files and directories by the last modified time.
|
||||||
|
*
|
||||||
|
* This is the last time the actual contents of the file were last modified.
|
||||||
|
*
|
||||||
|
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see SortableIterator
|
||||||
|
*/
|
||||||
|
public function sortByModifiedTime(): static
|
||||||
|
{
|
||||||
|
$this->sort = Iterator\SortableIterator::SORT_BY_MODIFIED_TIME;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters the iterator with an anonymous function.
|
||||||
|
*
|
||||||
|
* The anonymous function receives a \SplFileInfo and must return false
|
||||||
|
* to remove files.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @see CustomFilterIterator
|
||||||
|
*/
|
||||||
|
public function filter(\Closure $closure): static
|
||||||
|
{
|
||||||
|
$this->filters[] = $closure;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Forces the following of symlinks.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function followLinks(): static
|
||||||
|
{
|
||||||
|
$this->followLinks = true;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tells finder to ignore unreadable directories.
|
||||||
|
*
|
||||||
|
* By default, scanning unreadable directories content throws an AccessDeniedException.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function ignoreUnreadableDirs(bool $ignore = true): static
|
||||||
|
{
|
||||||
|
$this->ignoreUnreadableDirs = $ignore;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches files and directories which match defined rules.
|
||||||
|
*
|
||||||
|
* @param string|string[] $dirs A directory path or an array of directories
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @throws DirectoryNotFoundException if one of the directories does not exist
|
||||||
|
*/
|
||||||
|
public function in(string|array $dirs): static
|
||||||
|
{
|
||||||
|
$resolvedDirs = [];
|
||||||
|
|
||||||
|
foreach ((array) $dirs as $dir) {
|
||||||
|
if (is_dir($dir)) {
|
||||||
|
$resolvedDirs[] = [$this->normalizeDir($dir)];
|
||||||
|
} elseif ($glob = glob($dir, (\defined('GLOB_BRACE') ? \GLOB_BRACE : 0) | \GLOB_ONLYDIR | \GLOB_NOSORT)) {
|
||||||
|
sort($glob);
|
||||||
|
$resolvedDirs[] = array_map($this->normalizeDir(...), $glob);
|
||||||
|
} else {
|
||||||
|
throw new DirectoryNotFoundException(sprintf('The "%s" directory does not exist.', $dir));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->dirs = array_merge($this->dirs, ...$resolvedDirs);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an Iterator for the current Finder configuration.
|
||||||
|
*
|
||||||
|
* This method implements the IteratorAggregate interface.
|
||||||
|
*
|
||||||
|
* @return \Iterator<string, SplFileInfo>
|
||||||
|
*
|
||||||
|
* @throws \LogicException if the in() method has not been called
|
||||||
|
*/
|
||||||
|
public function getIterator(): \Iterator
|
||||||
|
{
|
||||||
|
if (0 === \count($this->dirs) && 0 === \count($this->iterators)) {
|
||||||
|
throw new \LogicException('You must call one of in() or append() methods before iterating over a Finder.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (1 === \count($this->dirs) && 0 === \count($this->iterators)) {
|
||||||
|
$iterator = $this->searchInDirectory($this->dirs[0]);
|
||||||
|
|
||||||
|
if ($this->sort || $this->reverseSorting) {
|
||||||
|
$iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $iterator;
|
||||||
|
}
|
||||||
|
|
||||||
|
$iterator = new \AppendIterator();
|
||||||
|
foreach ($this->dirs as $dir) {
|
||||||
|
$iterator->append(new \IteratorIterator(new LazyIterator(function () use ($dir) {
|
||||||
|
return $this->searchInDirectory($dir);
|
||||||
|
})));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->iterators as $it) {
|
||||||
|
$iterator->append($it);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->sort || $this->reverseSorting) {
|
||||||
|
$iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $iterator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends an existing set of files/directories to the finder.
|
||||||
|
*
|
||||||
|
* The set can be another Finder, an Iterator, an IteratorAggregate, or even a plain array.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @throws \InvalidArgumentException when the given argument is not iterable
|
||||||
|
*/
|
||||||
|
public function append(iterable $iterator): static
|
||||||
|
{
|
||||||
|
if ($iterator instanceof \IteratorAggregate) {
|
||||||
|
$this->iterators[] = $iterator->getIterator();
|
||||||
|
} elseif ($iterator instanceof \Iterator) {
|
||||||
|
$this->iterators[] = $iterator;
|
||||||
|
} elseif (is_iterable($iterator)) {
|
||||||
|
$it = new \ArrayIterator();
|
||||||
|
foreach ($iterator as $file) {
|
||||||
|
$file = $file instanceof \SplFileInfo ? $file : new \SplFileInfo($file);
|
||||||
|
$it[$file->getPathname()] = $file;
|
||||||
|
}
|
||||||
|
$this->iterators[] = $it;
|
||||||
|
} else {
|
||||||
|
throw new \InvalidArgumentException('Finder::append() method wrong argument type.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if any results were found.
|
||||||
|
*/
|
||||||
|
public function hasResults(): bool
|
||||||
|
{
|
||||||
|
foreach ($this->getIterator() as $_) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Counts all the results collected by the iterators.
|
||||||
|
*/
|
||||||
|
public function count(): int
|
||||||
|
{
|
||||||
|
return iterator_count($this->getIterator());
|
||||||
|
}
|
||||||
|
|
||||||
|
private function searchInDirectory(string $dir): \Iterator
|
||||||
|
{
|
||||||
|
$exclude = $this->exclude;
|
||||||
|
$notPaths = $this->notPaths;
|
||||||
|
|
||||||
|
if (static::IGNORE_VCS_FILES === (static::IGNORE_VCS_FILES & $this->ignore)) {
|
||||||
|
$exclude = array_merge($exclude, self::$vcsPatterns);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (static::IGNORE_DOT_FILES === (static::IGNORE_DOT_FILES & $this->ignore)) {
|
||||||
|
$notPaths[] = '#(^|/)\..+(/|$)#';
|
||||||
|
}
|
||||||
|
|
||||||
|
$minDepth = 0;
|
||||||
|
$maxDepth = \PHP_INT_MAX;
|
||||||
|
|
||||||
|
foreach ($this->depths as $comparator) {
|
||||||
|
switch ($comparator->getOperator()) {
|
||||||
|
case '>':
|
||||||
|
$minDepth = $comparator->getTarget() + 1;
|
||||||
|
break;
|
||||||
|
case '>=':
|
||||||
|
$minDepth = $comparator->getTarget();
|
||||||
|
break;
|
||||||
|
case '<':
|
||||||
|
$maxDepth = $comparator->getTarget() - 1;
|
||||||
|
break;
|
||||||
|
case '<=':
|
||||||
|
$maxDepth = $comparator->getTarget();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$minDepth = $maxDepth = $comparator->getTarget();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$flags = \RecursiveDirectoryIterator::SKIP_DOTS;
|
||||||
|
|
||||||
|
if ($this->followLinks) {
|
||||||
|
$flags |= \RecursiveDirectoryIterator::FOLLOW_SYMLINKS;
|
||||||
|
}
|
||||||
|
|
||||||
|
$iterator = new Iterator\RecursiveDirectoryIterator($dir, $flags, $this->ignoreUnreadableDirs);
|
||||||
|
|
||||||
|
if ($exclude) {
|
||||||
|
$iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $exclude);
|
||||||
|
}
|
||||||
|
|
||||||
|
$iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
|
||||||
|
|
||||||
|
if ($minDepth > 0 || $maxDepth < \PHP_INT_MAX) {
|
||||||
|
$iterator = new Iterator\DepthRangeFilterIterator($iterator, $minDepth, $maxDepth);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->mode) {
|
||||||
|
$iterator = new Iterator\FileTypeFilterIterator($iterator, $this->mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->names || $this->notNames) {
|
||||||
|
$iterator = new Iterator\FilenameFilterIterator($iterator, $this->names, $this->notNames);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->contains || $this->notContains) {
|
||||||
|
$iterator = new Iterator\FilecontentFilterIterator($iterator, $this->contains, $this->notContains);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->sizes) {
|
||||||
|
$iterator = new Iterator\SizeRangeFilterIterator($iterator, $this->sizes);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->dates) {
|
||||||
|
$iterator = new Iterator\DateRangeFilterIterator($iterator, $this->dates);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->filters) {
|
||||||
|
$iterator = new Iterator\CustomFilterIterator($iterator, $this->filters);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->paths || $notPaths) {
|
||||||
|
$iterator = new Iterator\PathFilterIterator($iterator, $this->paths, $notPaths);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (static::IGNORE_VCS_IGNORED_FILES === (static::IGNORE_VCS_IGNORED_FILES & $this->ignore)) {
|
||||||
|
$iterator = new Iterator\VcsIgnoredFilterIterator($iterator, $dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $iterator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalizes given directory names by removing trailing slashes.
|
||||||
|
*
|
||||||
|
* Excluding: (s)ftp:// or ssh2.(s)ftp:// wrapper
|
||||||
|
*/
|
||||||
|
private function normalizeDir(string $dir): string
|
||||||
|
{
|
||||||
|
if ('/' === $dir) {
|
||||||
|
return $dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
$dir = rtrim($dir, '/'.\DIRECTORY_SEPARATOR);
|
||||||
|
|
||||||
|
if (preg_match('#^(ssh2\.)?s?ftp://#', $dir)) {
|
||||||
|
$dir .= '/';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $dir;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,93 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gitignore matches against text.
|
||||||
|
*
|
||||||
|
* @author Michael Voříšek <vorismi3@fel.cvut.cz>
|
||||||
|
* @author Ahmed Abdou <mail@ahmd.io>
|
||||||
|
*/
|
||||||
|
class Gitignore
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns a regexp which is the equivalent of the gitignore pattern.
|
||||||
|
*
|
||||||
|
* Format specification: https://git-scm.com/docs/gitignore#_pattern_format
|
||||||
|
*/
|
||||||
|
public static function toRegex(string $gitignoreFileContent): string
|
||||||
|
{
|
||||||
|
return self::buildRegex($gitignoreFileContent, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function toRegexMatchingNegatedPatterns(string $gitignoreFileContent): string
|
||||||
|
{
|
||||||
|
return self::buildRegex($gitignoreFileContent, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function buildRegex(string $gitignoreFileContent, bool $inverted): string
|
||||||
|
{
|
||||||
|
$gitignoreFileContent = preg_replace('~(?<!\\\\)#[^\n\r]*~', '', $gitignoreFileContent);
|
||||||
|
$gitignoreLines = preg_split('~\r\n?|\n~', $gitignoreFileContent);
|
||||||
|
|
||||||
|
$res = self::lineToRegex('');
|
||||||
|
foreach ($gitignoreLines as $line) {
|
||||||
|
$line = preg_replace('~(?<!\\\\)[ \t]+$~', '', $line);
|
||||||
|
|
||||||
|
if (str_starts_with($line, '!')) {
|
||||||
|
$line = substr($line, 1);
|
||||||
|
$isNegative = true;
|
||||||
|
} else {
|
||||||
|
$isNegative = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ('' !== $line) {
|
||||||
|
if ($isNegative xor $inverted) {
|
||||||
|
$res = '(?!'.self::lineToRegex($line).'$)'.$res;
|
||||||
|
} else {
|
||||||
|
$res = '(?:'.$res.'|'.self::lineToRegex($line).')';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return '~^(?:'.$res.')~s';
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function lineToRegex(string $gitignoreLine): string
|
||||||
|
{
|
||||||
|
if ('' === $gitignoreLine) {
|
||||||
|
return '$f'; // always false
|
||||||
|
}
|
||||||
|
|
||||||
|
$slashPos = strpos($gitignoreLine, '/');
|
||||||
|
if (false !== $slashPos && \strlen($gitignoreLine) - 1 !== $slashPos) {
|
||||||
|
if (0 === $slashPos) {
|
||||||
|
$gitignoreLine = substr($gitignoreLine, 1);
|
||||||
|
}
|
||||||
|
$isAbsolute = true;
|
||||||
|
} else {
|
||||||
|
$isAbsolute = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$regex = preg_quote(str_replace('\\', '', $gitignoreLine), '~');
|
||||||
|
$regex = preg_replace_callback('~\\\\\[((?:\\\\!)?)([^\[\]]*)\\\\\]~', function (array $matches): string {
|
||||||
|
return '['.('' !== $matches[1] ? '^' : '').str_replace('\\-', '-', $matches[2]).']';
|
||||||
|
}, $regex);
|
||||||
|
$regex = preg_replace('~(?:(?:\\\\\*){2,}(/?))+~', '(?:(?:(?!//).(?<!//))+$1)?', $regex);
|
||||||
|
$regex = preg_replace('~\\\\\*~', '[^/]*', $regex);
|
||||||
|
$regex = preg_replace('~\\\\\?~', '[^/]', $regex);
|
||||||
|
|
||||||
|
return ($isAbsolute ? '' : '(?:[^/]+/)*')
|
||||||
|
.$regex
|
||||||
|
.(!str_ends_with($gitignoreLine, '/') ? '(?:$|/)' : '');
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,109 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Glob matches globbing patterns against text.
|
||||||
|
*
|
||||||
|
* if match_glob("foo.*", "foo.bar") echo "matched\n";
|
||||||
|
*
|
||||||
|
* // prints foo.bar and foo.baz
|
||||||
|
* $regex = glob_to_regex("foo.*");
|
||||||
|
* for (['foo.bar', 'foo.baz', 'foo', 'bar'] as $t)
|
||||||
|
* {
|
||||||
|
* if (/$regex/) echo "matched: $car\n";
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* Glob implements glob(3) style matching that can be used to match
|
||||||
|
* against text, rather than fetching names from a filesystem.
|
||||||
|
*
|
||||||
|
* Based on the Perl Text::Glob module.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com> PHP port
|
||||||
|
* @author Richard Clamp <richardc@unixbeard.net> Perl version
|
||||||
|
* @copyright 2004-2005 Fabien Potencier <fabien@symfony.com>
|
||||||
|
* @copyright 2002 Richard Clamp <richardc@unixbeard.net>
|
||||||
|
*/
|
||||||
|
class Glob
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns a regexp which is the equivalent of the glob pattern.
|
||||||
|
*/
|
||||||
|
public static function toRegex(string $glob, bool $strictLeadingDot = true, bool $strictWildcardSlash = true, string $delimiter = '#'): string
|
||||||
|
{
|
||||||
|
$firstByte = true;
|
||||||
|
$escaping = false;
|
||||||
|
$inCurlies = 0;
|
||||||
|
$regex = '';
|
||||||
|
$sizeGlob = \strlen($glob);
|
||||||
|
for ($i = 0; $i < $sizeGlob; ++$i) {
|
||||||
|
$car = $glob[$i];
|
||||||
|
if ($firstByte && $strictLeadingDot && '.' !== $car) {
|
||||||
|
$regex .= '(?=[^\.])';
|
||||||
|
}
|
||||||
|
|
||||||
|
$firstByte = '/' === $car;
|
||||||
|
|
||||||
|
if ($firstByte && $strictWildcardSlash && isset($glob[$i + 2]) && '**' === $glob[$i + 1].$glob[$i + 2] && (!isset($glob[$i + 3]) || '/' === $glob[$i + 3])) {
|
||||||
|
$car = '[^/]++/';
|
||||||
|
if (!isset($glob[$i + 3])) {
|
||||||
|
$car .= '?';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($strictLeadingDot) {
|
||||||
|
$car = '(?=[^\.])'.$car;
|
||||||
|
}
|
||||||
|
|
||||||
|
$car = '/(?:'.$car.')*';
|
||||||
|
$i += 2 + isset($glob[$i + 3]);
|
||||||
|
|
||||||
|
if ('/' === $delimiter) {
|
||||||
|
$car = str_replace('/', '\\/', $car);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($delimiter === $car || '.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
|
||||||
|
$regex .= "\\$car";
|
||||||
|
} elseif ('*' === $car) {
|
||||||
|
$regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
|
||||||
|
} elseif ('?' === $car) {
|
||||||
|
$regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
|
||||||
|
} elseif ('{' === $car) {
|
||||||
|
$regex .= $escaping ? '\\{' : '(';
|
||||||
|
if (!$escaping) {
|
||||||
|
++$inCurlies;
|
||||||
|
}
|
||||||
|
} elseif ('}' === $car && $inCurlies) {
|
||||||
|
$regex .= $escaping ? '}' : ')';
|
||||||
|
if (!$escaping) {
|
||||||
|
--$inCurlies;
|
||||||
|
}
|
||||||
|
} elseif (',' === $car && $inCurlies) {
|
||||||
|
$regex .= $escaping ? ',' : '|';
|
||||||
|
} elseif ('\\' === $car) {
|
||||||
|
if ($escaping) {
|
||||||
|
$regex .= '\\\\';
|
||||||
|
$escaping = false;
|
||||||
|
} else {
|
||||||
|
$escaping = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
$regex .= $car;
|
||||||
|
}
|
||||||
|
$escaping = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $delimiter.'^'.$regex.'$'.$delimiter;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CustomFilterIterator filters files by applying anonymous functions.
|
||||||
|
*
|
||||||
|
* The anonymous function receives a \SplFileInfo and must return false
|
||||||
|
* to remove files.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* @extends \FilterIterator<string, \SplFileInfo>
|
||||||
|
*/
|
||||||
|
class CustomFilterIterator extends \FilterIterator
|
||||||
|
{
|
||||||
|
private array $filters = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Iterator<string, \SplFileInfo> $iterator The Iterator to filter
|
||||||
|
* @param callable[] $filters An array of PHP callbacks
|
||||||
|
*
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function __construct(\Iterator $iterator, array $filters)
|
||||||
|
{
|
||||||
|
foreach ($filters as $filter) {
|
||||||
|
if (!\is_callable($filter)) {
|
||||||
|
throw new \InvalidArgumentException('Invalid PHP callback.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->filters = $filters;
|
||||||
|
|
||||||
|
parent::__construct($iterator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters the iterator values.
|
||||||
|
*/
|
||||||
|
public function accept(): bool
|
||||||
|
{
|
||||||
|
$fileinfo = $this->current();
|
||||||
|
|
||||||
|
foreach ($this->filters as $filter) {
|
||||||
|
if (false === $filter($fileinfo)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Iterator;
|
||||||
|
|
||||||
|
use Symfony\Component\Finder\Comparator\DateComparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DateRangeFilterIterator filters out files that are not in the given date range (last modified dates).
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* @extends \FilterIterator<string, \SplFileInfo>
|
||||||
|
*/
|
||||||
|
class DateRangeFilterIterator extends \FilterIterator
|
||||||
|
{
|
||||||
|
private array $comparators = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Iterator<string, \SplFileInfo> $iterator
|
||||||
|
* @param DateComparator[] $comparators
|
||||||
|
*/
|
||||||
|
public function __construct(\Iterator $iterator, array $comparators)
|
||||||
|
{
|
||||||
|
$this->comparators = $comparators;
|
||||||
|
|
||||||
|
parent::__construct($iterator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters the iterator values.
|
||||||
|
*/
|
||||||
|
public function accept(): bool
|
||||||
|
{
|
||||||
|
$fileinfo = $this->current();
|
||||||
|
|
||||||
|
if (!file_exists($fileinfo->getPathname())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$filedate = $fileinfo->getMTime();
|
||||||
|
foreach ($this->comparators as $compare) {
|
||||||
|
if (!$compare->test($filedate)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DepthRangeFilterIterator limits the directory depth.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* @template-covariant TKey
|
||||||
|
* @template-covariant TValue
|
||||||
|
*
|
||||||
|
* @extends \FilterIterator<TKey, TValue>
|
||||||
|
*/
|
||||||
|
class DepthRangeFilterIterator extends \FilterIterator
|
||||||
|
{
|
||||||
|
private int $minDepth = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \RecursiveIteratorIterator<\RecursiveIterator<TKey, TValue>> $iterator The Iterator to filter
|
||||||
|
* @param int $minDepth The min depth
|
||||||
|
* @param int $maxDepth The max depth
|
||||||
|
*/
|
||||||
|
public function __construct(\RecursiveIteratorIterator $iterator, int $minDepth = 0, int $maxDepth = \PHP_INT_MAX)
|
||||||
|
{
|
||||||
|
$this->minDepth = $minDepth;
|
||||||
|
$iterator->setMaxDepth(\PHP_INT_MAX === $maxDepth ? -1 : $maxDepth);
|
||||||
|
|
||||||
|
parent::__construct($iterator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters the iterator values.
|
||||||
|
*/
|
||||||
|
public function accept(): bool
|
||||||
|
{
|
||||||
|
return $this->getInnerIterator()->getDepth() >= $this->minDepth;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Iterator;
|
||||||
|
|
||||||
|
use Symfony\Component\Finder\SplFileInfo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ExcludeDirectoryFilterIterator filters out directories.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* @extends \FilterIterator<string, SplFileInfo>
|
||||||
|
* @implements \RecursiveIterator<string, SplFileInfo>
|
||||||
|
*/
|
||||||
|
class ExcludeDirectoryFilterIterator extends \FilterIterator implements \RecursiveIterator
|
||||||
|
{
|
||||||
|
/** @var \Iterator<string, SplFileInfo> */
|
||||||
|
private \Iterator $iterator;
|
||||||
|
private bool $isRecursive;
|
||||||
|
private array $excludedDirs = [];
|
||||||
|
private ?string $excludedPattern = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Iterator<string, SplFileInfo> $iterator The Iterator to filter
|
||||||
|
* @param string[] $directories An array of directories to exclude
|
||||||
|
*/
|
||||||
|
public function __construct(\Iterator $iterator, array $directories)
|
||||||
|
{
|
||||||
|
$this->iterator = $iterator;
|
||||||
|
$this->isRecursive = $iterator instanceof \RecursiveIterator;
|
||||||
|
$patterns = [];
|
||||||
|
foreach ($directories as $directory) {
|
||||||
|
$directory = rtrim($directory, '/');
|
||||||
|
if (!$this->isRecursive || str_contains($directory, '/')) {
|
||||||
|
$patterns[] = preg_quote($directory, '#');
|
||||||
|
} else {
|
||||||
|
$this->excludedDirs[$directory] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($patterns) {
|
||||||
|
$this->excludedPattern = '#(?:^|/)(?:'.implode('|', $patterns).')(?:/|$)#';
|
||||||
|
}
|
||||||
|
|
||||||
|
parent::__construct($iterator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters the iterator values.
|
||||||
|
*/
|
||||||
|
public function accept(): bool
|
||||||
|
{
|
||||||
|
if ($this->isRecursive && isset($this->excludedDirs[$this->getFilename()]) && $this->isDir()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->excludedPattern) {
|
||||||
|
$path = $this->isDir() ? $this->current()->getRelativePathname() : $this->current()->getRelativePath();
|
||||||
|
$path = str_replace('\\', '/', $path);
|
||||||
|
|
||||||
|
return !preg_match($this->excludedPattern, $path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasChildren(): bool
|
||||||
|
{
|
||||||
|
return $this->isRecursive && $this->iterator->hasChildren();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getChildren(): self
|
||||||
|
{
|
||||||
|
$children = new self($this->iterator->getChildren(), []);
|
||||||
|
$children->excludedDirs = $this->excludedDirs;
|
||||||
|
$children->excludedPattern = $this->excludedPattern;
|
||||||
|
|
||||||
|
return $children;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FileTypeFilterIterator only keeps files, directories, or both.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* @extends \FilterIterator<string, \SplFileInfo>
|
||||||
|
*/
|
||||||
|
class FileTypeFilterIterator extends \FilterIterator
|
||||||
|
{
|
||||||
|
public const ONLY_FILES = 1;
|
||||||
|
public const ONLY_DIRECTORIES = 2;
|
||||||
|
|
||||||
|
private int $mode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Iterator<string, \SplFileInfo> $iterator The Iterator to filter
|
||||||
|
* @param int $mode The mode (self::ONLY_FILES or self::ONLY_DIRECTORIES)
|
||||||
|
*/
|
||||||
|
public function __construct(\Iterator $iterator, int $mode)
|
||||||
|
{
|
||||||
|
$this->mode = $mode;
|
||||||
|
|
||||||
|
parent::__construct($iterator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters the iterator values.
|
||||||
|
*/
|
||||||
|
public function accept(): bool
|
||||||
|
{
|
||||||
|
$fileinfo = $this->current();
|
||||||
|
if (self::ONLY_DIRECTORIES === (self::ONLY_DIRECTORIES & $this->mode) && $fileinfo->isFile()) {
|
||||||
|
return false;
|
||||||
|
} elseif (self::ONLY_FILES === (self::ONLY_FILES & $this->mode) && $fileinfo->isDir()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Iterator;
|
||||||
|
|
||||||
|
use Symfony\Component\Finder\SplFileInfo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FilecontentFilterIterator filters files by their contents using patterns (regexps or strings).
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
* @author Włodzimierz Gajda <gajdaw@gajdaw.pl>
|
||||||
|
*
|
||||||
|
* @extends MultiplePcreFilterIterator<string, SplFileInfo>
|
||||||
|
*/
|
||||||
|
class FilecontentFilterIterator extends MultiplePcreFilterIterator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Filters the iterator values.
|
||||||
|
*/
|
||||||
|
public function accept(): bool
|
||||||
|
{
|
||||||
|
if (!$this->matchRegexps && !$this->noMatchRegexps) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$fileinfo = $this->current();
|
||||||
|
|
||||||
|
if ($fileinfo->isDir() || !$fileinfo->isReadable()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = $fileinfo->getContents();
|
||||||
|
if (!$content) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->isAccepted($content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts string to regexp if necessary.
|
||||||
|
*
|
||||||
|
* @param string $str Pattern: string or regexp
|
||||||
|
*/
|
||||||
|
protected function toRegex(string $str): string
|
||||||
|
{
|
||||||
|
return $this->isRegex($str) ? $str : '/'.preg_quote($str, '/').'/';
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Iterator;
|
||||||
|
|
||||||
|
use Symfony\Component\Finder\Glob;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FilenameFilterIterator filters files by patterns (a regexp, a glob, or a string).
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* @extends MultiplePcreFilterIterator<string, \SplFileInfo>
|
||||||
|
*/
|
||||||
|
class FilenameFilterIterator extends MultiplePcreFilterIterator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Filters the iterator values.
|
||||||
|
*/
|
||||||
|
public function accept(): bool
|
||||||
|
{
|
||||||
|
return $this->isAccepted($this->current()->getFilename());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts glob to regexp.
|
||||||
|
*
|
||||||
|
* PCRE patterns are left unchanged.
|
||||||
|
* Glob strings are transformed with Glob::toRegex().
|
||||||
|
*
|
||||||
|
* @param string $str Pattern: glob or regexp
|
||||||
|
*/
|
||||||
|
protected function toRegex(string $str): string
|
||||||
|
{
|
||||||
|
return $this->isRegex($str) ? $str : Glob::toRegex($str);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Jérémy Derussé <jeremy@derusse.com>
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
class LazyIterator implements \IteratorAggregate
|
||||||
|
{
|
||||||
|
private \Closure $iteratorFactory;
|
||||||
|
|
||||||
|
public function __construct(callable $iteratorFactory)
|
||||||
|
{
|
||||||
|
$this->iteratorFactory = $iteratorFactory(...);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getIterator(): \Traversable
|
||||||
|
{
|
||||||
|
yield from ($this->iteratorFactory)();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,111 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MultiplePcreFilterIterator filters files using patterns (regexps, globs or strings).
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* @template-covariant TKey
|
||||||
|
* @template-covariant TValue
|
||||||
|
*
|
||||||
|
* @extends \FilterIterator<TKey, TValue>
|
||||||
|
*/
|
||||||
|
abstract class MultiplePcreFilterIterator extends \FilterIterator
|
||||||
|
{
|
||||||
|
protected $matchRegexps = [];
|
||||||
|
protected $noMatchRegexps = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Iterator<TKey, TValue> $iterator The Iterator to filter
|
||||||
|
* @param string[] $matchPatterns An array of patterns that need to match
|
||||||
|
* @param string[] $noMatchPatterns An array of patterns that need to not match
|
||||||
|
*/
|
||||||
|
public function __construct(\Iterator $iterator, array $matchPatterns, array $noMatchPatterns)
|
||||||
|
{
|
||||||
|
foreach ($matchPatterns as $pattern) {
|
||||||
|
$this->matchRegexps[] = $this->toRegex($pattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($noMatchPatterns as $pattern) {
|
||||||
|
$this->noMatchRegexps[] = $this->toRegex($pattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
parent::__construct($iterator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the string is accepted by the regex filters.
|
||||||
|
*
|
||||||
|
* If there is no regexps defined in the class, this method will accept the string.
|
||||||
|
* Such case can be handled by child classes before calling the method if they want to
|
||||||
|
* apply a different behavior.
|
||||||
|
*/
|
||||||
|
protected function isAccepted(string $string): bool
|
||||||
|
{
|
||||||
|
// should at least not match one rule to exclude
|
||||||
|
foreach ($this->noMatchRegexps as $regex) {
|
||||||
|
if (preg_match($regex, $string)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// should at least match one rule
|
||||||
|
if ($this->matchRegexps) {
|
||||||
|
foreach ($this->matchRegexps as $regex) {
|
||||||
|
if (preg_match($regex, $string)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there is no match rules, the file is accepted
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the string is a regex.
|
||||||
|
*/
|
||||||
|
protected function isRegex(string $str): bool
|
||||||
|
{
|
||||||
|
$availableModifiers = 'imsxuADU';
|
||||||
|
|
||||||
|
if (\PHP_VERSION_ID >= 80200) {
|
||||||
|
$availableModifiers .= 'n';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preg_match('/^(.{3,}?)['.$availableModifiers.']*$/', $str, $m)) {
|
||||||
|
$start = substr($m[1], 0, 1);
|
||||||
|
$end = substr($m[1], -1);
|
||||||
|
|
||||||
|
if ($start === $end) {
|
||||||
|
return !preg_match('/[*?[:alnum:] \\\\]/', $start);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ([['{', '}'], ['(', ')'], ['[', ']'], ['<', '>']] as $delimiters) {
|
||||||
|
if ($start === $delimiters[0] && $end === $delimiters[1]) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts string into regexp.
|
||||||
|
*/
|
||||||
|
abstract protected function toRegex(string $str): string;
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Iterator;
|
||||||
|
|
||||||
|
use Symfony\Component\Finder\SplFileInfo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PathFilterIterator filters files by path patterns (e.g. some/special/dir).
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
* @author Włodzimierz Gajda <gajdaw@gajdaw.pl>
|
||||||
|
*
|
||||||
|
* @extends MultiplePcreFilterIterator<string, SplFileInfo>
|
||||||
|
*/
|
||||||
|
class PathFilterIterator extends MultiplePcreFilterIterator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Filters the iterator values.
|
||||||
|
*/
|
||||||
|
public function accept(): bool
|
||||||
|
{
|
||||||
|
$filename = $this->current()->getRelativePathname();
|
||||||
|
|
||||||
|
if ('\\' === \DIRECTORY_SEPARATOR) {
|
||||||
|
$filename = str_replace('\\', '/', $filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->isAccepted($filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts strings to regexp.
|
||||||
|
*
|
||||||
|
* PCRE patterns are left unchanged.
|
||||||
|
*
|
||||||
|
* Default conversion:
|
||||||
|
* 'lorem/ipsum/dolor' ==> 'lorem\/ipsum\/dolor/'
|
||||||
|
*
|
||||||
|
* Use only / as directory separator (on Windows also).
|
||||||
|
*
|
||||||
|
* @param string $str Pattern: regexp or dirname
|
||||||
|
*/
|
||||||
|
protected function toRegex(string $str): string
|
||||||
|
{
|
||||||
|
return $this->isRegex($str) ? $str : '/'.preg_quote($str, '/').'/';
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,146 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Iterator;
|
||||||
|
|
||||||
|
use Symfony\Component\Finder\Exception\AccessDeniedException;
|
||||||
|
use Symfony\Component\Finder\SplFileInfo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extends the \RecursiveDirectoryIterator to support relative paths.
|
||||||
|
*
|
||||||
|
* @author Victor Berchet <victor@suumit.com>
|
||||||
|
* @extends \RecursiveDirectoryIterator<string, SplFileInfo>
|
||||||
|
*/
|
||||||
|
class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator
|
||||||
|
{
|
||||||
|
private bool $ignoreUnreadableDirs;
|
||||||
|
private ?bool $rewindable = null;
|
||||||
|
|
||||||
|
// these 3 properties take part of the performance optimization to avoid redoing the same work in all iterations
|
||||||
|
private string $rootPath;
|
||||||
|
private string $subPath;
|
||||||
|
private string $directorySeparator = '/';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws \RuntimeException
|
||||||
|
*/
|
||||||
|
public function __construct(string $path, int $flags, bool $ignoreUnreadableDirs = false)
|
||||||
|
{
|
||||||
|
if ($flags & (self::CURRENT_AS_PATHNAME | self::CURRENT_AS_SELF)) {
|
||||||
|
throw new \RuntimeException('This iterator only support returning current as fileinfo.');
|
||||||
|
}
|
||||||
|
|
||||||
|
parent::__construct($path, $flags);
|
||||||
|
$this->ignoreUnreadableDirs = $ignoreUnreadableDirs;
|
||||||
|
$this->rootPath = $path;
|
||||||
|
if ('/' !== \DIRECTORY_SEPARATOR && !($flags & self::UNIX_PATHS)) {
|
||||||
|
$this->directorySeparator = \DIRECTORY_SEPARATOR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an instance of SplFileInfo with support for relative paths.
|
||||||
|
*/
|
||||||
|
public function current(): SplFileInfo
|
||||||
|
{
|
||||||
|
// the logic here avoids redoing the same work in all iterations
|
||||||
|
|
||||||
|
if (!isset($this->subPath)) {
|
||||||
|
$this->subPath = $this->getSubPath();
|
||||||
|
}
|
||||||
|
$subPathname = $this->subPath;
|
||||||
|
if ('' !== $subPathname) {
|
||||||
|
$subPathname .= $this->directorySeparator;
|
||||||
|
}
|
||||||
|
$subPathname .= $this->getFilename();
|
||||||
|
|
||||||
|
if ('/' !== $basePath = $this->rootPath) {
|
||||||
|
$basePath .= $this->directorySeparator;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new SplFileInfo($basePath.$subPathname, $this->subPath, $subPathname);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasChildren(bool $allowLinks = false): bool
|
||||||
|
{
|
||||||
|
$hasChildren = parent::hasChildren($allowLinks);
|
||||||
|
|
||||||
|
if (!$hasChildren || !$this->ignoreUnreadableDirs) {
|
||||||
|
return $hasChildren;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
parent::getChildren();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (\UnexpectedValueException) {
|
||||||
|
// If directory is unreadable and finder is set to ignore it, skip children
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws AccessDeniedException
|
||||||
|
*/
|
||||||
|
public function getChildren(): \RecursiveDirectoryIterator
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$children = parent::getChildren();
|
||||||
|
|
||||||
|
if ($children instanceof self) {
|
||||||
|
// parent method will call the constructor with default arguments, so unreadable dirs won't be ignored anymore
|
||||||
|
$children->ignoreUnreadableDirs = $this->ignoreUnreadableDirs;
|
||||||
|
|
||||||
|
// performance optimization to avoid redoing the same work in all children
|
||||||
|
$children->rewindable = &$this->rewindable;
|
||||||
|
$children->rootPath = $this->rootPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $children;
|
||||||
|
} catch (\UnexpectedValueException $e) {
|
||||||
|
throw new AccessDeniedException($e->getMessage(), $e->getCode(), $e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Do nothing for non rewindable stream.
|
||||||
|
*/
|
||||||
|
public function rewind(): void
|
||||||
|
{
|
||||||
|
if (false === $this->isRewindable()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
parent::rewind();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the stream is rewindable.
|
||||||
|
*/
|
||||||
|
public function isRewindable(): bool
|
||||||
|
{
|
||||||
|
if (null !== $this->rewindable) {
|
||||||
|
return $this->rewindable;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (false !== $stream = @opendir($this->getPath())) {
|
||||||
|
$infos = stream_get_meta_data($stream);
|
||||||
|
closedir($stream);
|
||||||
|
|
||||||
|
if ($infos['seekable']) {
|
||||||
|
return $this->rewindable = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->rewindable = false;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Iterator;
|
||||||
|
|
||||||
|
use Symfony\Component\Finder\Comparator\NumberComparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SizeRangeFilterIterator filters out files that are not in the given size range.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* @extends \FilterIterator<string, \SplFileInfo>
|
||||||
|
*/
|
||||||
|
class SizeRangeFilterIterator extends \FilterIterator
|
||||||
|
{
|
||||||
|
private array $comparators = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Iterator<string, \SplFileInfo> $iterator
|
||||||
|
* @param NumberComparator[] $comparators
|
||||||
|
*/
|
||||||
|
public function __construct(\Iterator $iterator, array $comparators)
|
||||||
|
{
|
||||||
|
$this->comparators = $comparators;
|
||||||
|
|
||||||
|
parent::__construct($iterator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters the iterator values.
|
||||||
|
*/
|
||||||
|
public function accept(): bool
|
||||||
|
{
|
||||||
|
$fileinfo = $this->current();
|
||||||
|
if (!$fileinfo->isFile()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$filesize = $fileinfo->getSize();
|
||||||
|
foreach ($this->comparators as $compare) {
|
||||||
|
if (!$compare->test($filesize)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,121 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SortableIterator applies a sort on a given Iterator.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* @implements \IteratorAggregate<string, \SplFileInfo>
|
||||||
|
*/
|
||||||
|
class SortableIterator implements \IteratorAggregate
|
||||||
|
{
|
||||||
|
public const SORT_BY_NONE = 0;
|
||||||
|
public const SORT_BY_NAME = 1;
|
||||||
|
public const SORT_BY_TYPE = 2;
|
||||||
|
public const SORT_BY_ACCESSED_TIME = 3;
|
||||||
|
public const SORT_BY_CHANGED_TIME = 4;
|
||||||
|
public const SORT_BY_MODIFIED_TIME = 5;
|
||||||
|
public const SORT_BY_NAME_NATURAL = 6;
|
||||||
|
public const SORT_BY_NAME_CASE_INSENSITIVE = 7;
|
||||||
|
public const SORT_BY_NAME_NATURAL_CASE_INSENSITIVE = 8;
|
||||||
|
public const SORT_BY_EXTENSION = 9;
|
||||||
|
public const SORT_BY_SIZE = 10;
|
||||||
|
|
||||||
|
/** @var \Traversable<string, \SplFileInfo> */
|
||||||
|
private \Traversable $iterator;
|
||||||
|
private \Closure|int $sort;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Traversable<string, \SplFileInfo> $iterator
|
||||||
|
* @param int|callable $sort The sort type (SORT_BY_NAME, SORT_BY_TYPE, or a PHP callback)
|
||||||
|
*
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function __construct(\Traversable $iterator, int|callable $sort, bool $reverseOrder = false)
|
||||||
|
{
|
||||||
|
$this->iterator = $iterator;
|
||||||
|
$order = $reverseOrder ? -1 : 1;
|
||||||
|
|
||||||
|
if (self::SORT_BY_NAME === $sort) {
|
||||||
|
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
||||||
|
return $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
|
||||||
|
};
|
||||||
|
} elseif (self::SORT_BY_NAME_NATURAL === $sort) {
|
||||||
|
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
||||||
|
return $order * strnatcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
|
||||||
|
};
|
||||||
|
} elseif (self::SORT_BY_NAME_CASE_INSENSITIVE === $sort) {
|
||||||
|
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
||||||
|
return $order * strcasecmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
|
||||||
|
};
|
||||||
|
} elseif (self::SORT_BY_NAME_NATURAL_CASE_INSENSITIVE === $sort) {
|
||||||
|
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
||||||
|
return $order * strnatcasecmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
|
||||||
|
};
|
||||||
|
} elseif (self::SORT_BY_TYPE === $sort) {
|
||||||
|
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
||||||
|
if ($a->isDir() && $b->isFile()) {
|
||||||
|
return -$order;
|
||||||
|
} elseif ($a->isFile() && $b->isDir()) {
|
||||||
|
return $order;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
|
||||||
|
};
|
||||||
|
} elseif (self::SORT_BY_ACCESSED_TIME === $sort) {
|
||||||
|
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
||||||
|
return $order * ($a->getATime() - $b->getATime());
|
||||||
|
};
|
||||||
|
} elseif (self::SORT_BY_CHANGED_TIME === $sort) {
|
||||||
|
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
||||||
|
return $order * ($a->getCTime() - $b->getCTime());
|
||||||
|
};
|
||||||
|
} elseif (self::SORT_BY_MODIFIED_TIME === $sort) {
|
||||||
|
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
||||||
|
return $order * ($a->getMTime() - $b->getMTime());
|
||||||
|
};
|
||||||
|
} elseif (self::SORT_BY_EXTENSION === $sort) {
|
||||||
|
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
||||||
|
return $order * strnatcmp($a->getExtension(), $b->getExtension());
|
||||||
|
};
|
||||||
|
} elseif (self::SORT_BY_SIZE === $sort) {
|
||||||
|
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
|
||||||
|
return $order * ($a->getSize() - $b->getSize());
|
||||||
|
};
|
||||||
|
} elseif (self::SORT_BY_NONE === $sort) {
|
||||||
|
$this->sort = $order;
|
||||||
|
} elseif (\is_callable($sort)) {
|
||||||
|
$this->sort = $reverseOrder ? static function (\SplFileInfo $a, \SplFileInfo $b) use ($sort) { return -$sort($a, $b); } : $sort(...);
|
||||||
|
} else {
|
||||||
|
throw new \InvalidArgumentException('The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getIterator(): \Traversable
|
||||||
|
{
|
||||||
|
if (1 === $this->sort) {
|
||||||
|
return $this->iterator;
|
||||||
|
}
|
||||||
|
|
||||||
|
$array = iterator_to_array($this->iterator, true);
|
||||||
|
|
||||||
|
if (-1 === $this->sort) {
|
||||||
|
$array = array_reverse($array);
|
||||||
|
} else {
|
||||||
|
uasort($array, $this->sort);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new \ArrayIterator($array);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,178 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder\Iterator;
|
||||||
|
|
||||||
|
use Symfony\Component\Finder\Gitignore;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends \FilterIterator<string, \SplFileInfo>
|
||||||
|
*/
|
||||||
|
final class VcsIgnoredFilterIterator extends \FilterIterator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $baseDir;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array<string, array{0: string, 1: string}|null>
|
||||||
|
*/
|
||||||
|
private $gitignoreFilesCache = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array<string, bool>
|
||||||
|
*/
|
||||||
|
private $ignoredPathsCache = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Iterator<string, \SplFileInfo> $iterator
|
||||||
|
*/
|
||||||
|
public function __construct(\Iterator $iterator, string $baseDir)
|
||||||
|
{
|
||||||
|
$this->baseDir = $this->normalizePath($baseDir);
|
||||||
|
|
||||||
|
foreach ($this->parentDirectoriesUpwards($this->baseDir) as $parentDirectory) {
|
||||||
|
if (@is_dir("{$parentDirectory}/.git")) {
|
||||||
|
$this->baseDir = $parentDirectory;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parent::__construct($iterator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function accept(): bool
|
||||||
|
{
|
||||||
|
$file = $this->current();
|
||||||
|
|
||||||
|
$fileRealPath = $this->normalizePath($file->getRealPath());
|
||||||
|
|
||||||
|
return !$this->isIgnored($fileRealPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isIgnored(string $fileRealPath): bool
|
||||||
|
{
|
||||||
|
if (is_dir($fileRealPath) && !str_ends_with($fileRealPath, '/')) {
|
||||||
|
$fileRealPath .= '/';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($this->ignoredPathsCache[$fileRealPath])) {
|
||||||
|
return $this->ignoredPathsCache[$fileRealPath];
|
||||||
|
}
|
||||||
|
|
||||||
|
$ignored = false;
|
||||||
|
|
||||||
|
foreach ($this->parentDirectoriesDownwards($fileRealPath) as $parentDirectory) {
|
||||||
|
if ($this->isIgnored($parentDirectory)) {
|
||||||
|
// rules in ignored directories are ignored, no need to check further.
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$fileRelativePath = substr($fileRealPath, \strlen($parentDirectory) + 1);
|
||||||
|
|
||||||
|
if (null === $regexps = $this->readGitignoreFile("{$parentDirectory}/.gitignore")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
[$exclusionRegex, $inclusionRegex] = $regexps;
|
||||||
|
|
||||||
|
if (preg_match($exclusionRegex, $fileRelativePath)) {
|
||||||
|
$ignored = true;
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preg_match($inclusionRegex, $fileRelativePath)) {
|
||||||
|
$ignored = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->ignoredPathsCache[$fileRealPath] = $ignored;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<string>
|
||||||
|
*/
|
||||||
|
private function parentDirectoriesUpwards(string $from): array
|
||||||
|
{
|
||||||
|
$parentDirectories = [];
|
||||||
|
|
||||||
|
$parentDirectory = $from;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
$newParentDirectory = \dirname($parentDirectory);
|
||||||
|
|
||||||
|
// dirname('/') = '/'
|
||||||
|
if ($newParentDirectory === $parentDirectory) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$parentDirectories[] = $parentDirectory = $newParentDirectory;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $parentDirectories;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function parentDirectoriesUpTo(string $from, string $upTo): array
|
||||||
|
{
|
||||||
|
return array_filter(
|
||||||
|
$this->parentDirectoriesUpwards($from),
|
||||||
|
static function (string $directory) use ($upTo): bool {
|
||||||
|
return str_starts_with($directory, $upTo);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<string>
|
||||||
|
*/
|
||||||
|
private function parentDirectoriesDownwards(string $fileRealPath): array
|
||||||
|
{
|
||||||
|
return array_reverse(
|
||||||
|
$this->parentDirectoriesUpTo($fileRealPath, $this->baseDir)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{0: string, 1: string}|null
|
||||||
|
*/
|
||||||
|
private function readGitignoreFile(string $path): ?array
|
||||||
|
{
|
||||||
|
if (\array_key_exists($path, $this->gitignoreFilesCache)) {
|
||||||
|
return $this->gitignoreFilesCache[$path];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!file_exists($path)) {
|
||||||
|
return $this->gitignoreFilesCache[$path] = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_file($path) || !is_readable($path)) {
|
||||||
|
throw new \RuntimeException("The \"ignoreVCSIgnored\" option cannot be used by the Finder as the \"{$path}\" file is not readable.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$gitignoreFileContent = file_get_contents($path);
|
||||||
|
|
||||||
|
return $this->gitignoreFilesCache[$path] = [
|
||||||
|
Gitignore::toRegex($gitignoreFileContent),
|
||||||
|
Gitignore::toRegexMatchingNegatedPatterns($gitignoreFileContent),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function normalizePath(string $path): string
|
||||||
|
{
|
||||||
|
if ('\\' === \DIRECTORY_SEPARATOR) {
|
||||||
|
return str_replace('\\', '/', $path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
Copyright (c) 2004-2022 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.
|
@ -0,0 +1,14 @@
|
|||||||
|
Finder Component
|
||||||
|
================
|
||||||
|
|
||||||
|
The Finder component finds files and directories via an intuitive fluent
|
||||||
|
interface.
|
||||||
|
|
||||||
|
Resources
|
||||||
|
---------
|
||||||
|
|
||||||
|
* [Documentation](https://symfony.com/doc/current/components/finder.html)
|
||||||
|
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
|
||||||
|
* [Report issues](https://github.com/symfony/symfony/issues) and
|
||||||
|
[send Pull Requests](https://github.com/symfony/symfony/pulls)
|
||||||
|
in the [main Symfony repository](https://github.com/symfony/symfony)
|
@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Finder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extends \SplFileInfo to support relative paths.
|
||||||
|
*
|
||||||
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
*/
|
||||||
|
class SplFileInfo extends \SplFileInfo
|
||||||
|
{
|
||||||
|
private string $relativePath;
|
||||||
|
private string $relativePathname;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $file The file name
|
||||||
|
* @param string $relativePath The relative path
|
||||||
|
* @param string $relativePathname The relative path name
|
||||||
|
*/
|
||||||
|
public function __construct(string $file, string $relativePath, string $relativePathname)
|
||||||
|
{
|
||||||
|
parent::__construct($file);
|
||||||
|
$this->relativePath = $relativePath;
|
||||||
|
$this->relativePathname = $relativePathname;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the relative path.
|
||||||
|
*
|
||||||
|
* This path does not contain the file name.
|
||||||
|
*/
|
||||||
|
public function getRelativePath(): string
|
||||||
|
{
|
||||||
|
return $this->relativePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the relative path name.
|
||||||
|
*
|
||||||
|
* This path contains the file name.
|
||||||
|
*/
|
||||||
|
public function getRelativePathname(): string
|
||||||
|
{
|
||||||
|
return $this->relativePathname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFilenameWithoutExtension(): string
|
||||||
|
{
|
||||||
|
$filename = $this->getFilename();
|
||||||
|
|
||||||
|
return pathinfo($filename, \PATHINFO_FILENAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the contents of the file.
|
||||||
|
*
|
||||||
|
* @throws \RuntimeException
|
||||||
|
*/
|
||||||
|
public function getContents(): string
|
||||||
|
{
|
||||||
|
set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
|
||||||
|
try {
|
||||||
|
$content = file_get_contents($this->getPathname());
|
||||||
|
} finally {
|
||||||
|
restore_error_handler();
|
||||||
|
}
|
||||||
|
if (false === $content) {
|
||||||
|
throw new \RuntimeException($error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"name": "symfony/finder",
|
||||||
|
"type": "library",
|
||||||
|
"description": "Finds files and directories via an intuitive fluent interface",
|
||||||
|
"keywords": [],
|
||||||
|
"homepage": "https://symfony.com",
|
||||||
|
"license": "MIT",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Fabien Potencier",
|
||||||
|
"email": "fabien@symfony.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Symfony Community",
|
||||||
|
"homepage": "https://symfony.com/contributors"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"require": {
|
||||||
|
"php": ">=8.1"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"symfony/filesystem": "^6.0"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": { "Symfony\\Component\\Finder\\": "" },
|
||||||
|
"exclude-from-classmap": [
|
||||||
|
"/Tests/"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"minimum-stability": "dev"
|
||||||
|
}
|
@ -0,0 +1,232 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Polyfill\Ctype;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ctype implementation through regex.
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*
|
||||||
|
* @author Gert de Pagter <BackEndTea@gmail.com>
|
||||||
|
*/
|
||||||
|
final class Ctype
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns TRUE if every character in text is either a letter or a digit, FALSE otherwise.
|
||||||
|
*
|
||||||
|
* @see https://php.net/ctype-alnum
|
||||||
|
*
|
||||||
|
* @param mixed $text
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function ctype_alnum($text)
|
||||||
|
{
|
||||||
|
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
|
||||||
|
|
||||||
|
return \is_string($text) && '' !== $text && !preg_match('/[^A-Za-z0-9]/', $text);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns TRUE if every character in text is a letter, FALSE otherwise.
|
||||||
|
*
|
||||||
|
* @see https://php.net/ctype-alpha
|
||||||
|
*
|
||||||
|
* @param mixed $text
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function ctype_alpha($text)
|
||||||
|
{
|
||||||
|
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
|
||||||
|
|
||||||
|
return \is_string($text) && '' !== $text && !preg_match('/[^A-Za-z]/', $text);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns TRUE if every character in text is a control character from the current locale, FALSE otherwise.
|
||||||
|
*
|
||||||
|
* @see https://php.net/ctype-cntrl
|
||||||
|
*
|
||||||
|
* @param mixed $text
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function ctype_cntrl($text)
|
||||||
|
{
|
||||||
|
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
|
||||||
|
|
||||||
|
return \is_string($text) && '' !== $text && !preg_match('/[^\x00-\x1f\x7f]/', $text);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns TRUE if every character in the string text is a decimal digit, FALSE otherwise.
|
||||||
|
*
|
||||||
|
* @see https://php.net/ctype-digit
|
||||||
|
*
|
||||||
|
* @param mixed $text
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function ctype_digit($text)
|
||||||
|
{
|
||||||
|
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
|
||||||
|
|
||||||
|
return \is_string($text) && '' !== $text && !preg_match('/[^0-9]/', $text);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns TRUE if every character in text is printable and actually creates visible output (no white space), FALSE otherwise.
|
||||||
|
*
|
||||||
|
* @see https://php.net/ctype-graph
|
||||||
|
*
|
||||||
|
* @param mixed $text
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function ctype_graph($text)
|
||||||
|
{
|
||||||
|
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
|
||||||
|
|
||||||
|
return \is_string($text) && '' !== $text && !preg_match('/[^!-~]/', $text);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns TRUE if every character in text is a lowercase letter.
|
||||||
|
*
|
||||||
|
* @see https://php.net/ctype-lower
|
||||||
|
*
|
||||||
|
* @param mixed $text
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function ctype_lower($text)
|
||||||
|
{
|
||||||
|
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
|
||||||
|
|
||||||
|
return \is_string($text) && '' !== $text && !preg_match('/[^a-z]/', $text);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns TRUE if every character in text will actually create output (including blanks). Returns FALSE if text contains control characters or characters that do not have any output or control function at all.
|
||||||
|
*
|
||||||
|
* @see https://php.net/ctype-print
|
||||||
|
*
|
||||||
|
* @param mixed $text
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function ctype_print($text)
|
||||||
|
{
|
||||||
|
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
|
||||||
|
|
||||||
|
return \is_string($text) && '' !== $text && !preg_match('/[^ -~]/', $text);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns TRUE if every character in text is printable, but neither letter, digit or blank, FALSE otherwise.
|
||||||
|
*
|
||||||
|
* @see https://php.net/ctype-punct
|
||||||
|
*
|
||||||
|
* @param mixed $text
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function ctype_punct($text)
|
||||||
|
{
|
||||||
|
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
|
||||||
|
|
||||||
|
return \is_string($text) && '' !== $text && !preg_match('/[^!-\/\:-@\[-`\{-~]/', $text);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns TRUE if every character in text creates some sort of white space, FALSE otherwise. Besides the blank character this also includes tab, vertical tab, line feed, carriage return and form feed characters.
|
||||||
|
*
|
||||||
|
* @see https://php.net/ctype-space
|
||||||
|
*
|
||||||
|
* @param mixed $text
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function ctype_space($text)
|
||||||
|
{
|
||||||
|
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
|
||||||
|
|
||||||
|
return \is_string($text) && '' !== $text && !preg_match('/[^\s]/', $text);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns TRUE if every character in text is an uppercase letter.
|
||||||
|
*
|
||||||
|
* @see https://php.net/ctype-upper
|
||||||
|
*
|
||||||
|
* @param mixed $text
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function ctype_upper($text)
|
||||||
|
{
|
||||||
|
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
|
||||||
|
|
||||||
|
return \is_string($text) && '' !== $text && !preg_match('/[^A-Z]/', $text);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns TRUE if every character in text is a hexadecimal 'digit', that is a decimal digit or a character from [A-Fa-f] , FALSE otherwise.
|
||||||
|
*
|
||||||
|
* @see https://php.net/ctype-xdigit
|
||||||
|
*
|
||||||
|
* @param mixed $text
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function ctype_xdigit($text)
|
||||||
|
{
|
||||||
|
$text = self::convert_int_to_char_for_ctype($text, __FUNCTION__);
|
||||||
|
|
||||||
|
return \is_string($text) && '' !== $text && !preg_match('/[^A-Fa-f0-9]/', $text);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts integers to their char versions according to normal ctype behaviour, if needed.
|
||||||
|
*
|
||||||
|
* If an integer between -128 and 255 inclusive is provided,
|
||||||
|
* it is interpreted as the ASCII value of a single character
|
||||||
|
* (negative values have 256 added in order to allow characters in the Extended ASCII range).
|
||||||
|
* Any other integer is interpreted as a string containing the decimal digits of the integer.
|
||||||
|
*
|
||||||
|
* @param mixed $int
|
||||||
|
* @param string $function
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private static function convert_int_to_char_for_ctype($int, $function)
|
||||||
|
{
|
||||||
|
if (!\is_int($int)) {
|
||||||
|
return $int;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($int < -128 || $int > 255) {
|
||||||
|
return (string) $int;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (\PHP_VERSION_ID >= 80100) {
|
||||||
|
@trigger_error($function.'(): Argument of type int will be interpreted as string in the future', \E_USER_DEPRECATED);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($int < 0) {
|
||||||
|
$int += 256;
|
||||||
|
}
|
||||||
|
|
||||||
|
return \chr($int);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
Copyright (c) 2018-2019 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.
|
@ -0,0 +1,12 @@
|
|||||||
|
Symfony Polyfill / Ctype
|
||||||
|
========================
|
||||||
|
|
||||||
|
This component provides `ctype_*` functions to users who run php versions without the ctype extension.
|
||||||
|
|
||||||
|
More information can be found in the
|
||||||
|
[main Polyfill README](https://github.com/symfony/polyfill/blob/main/README.md).
|
||||||
|
|
||||||
|
License
|
||||||
|
=======
|
||||||
|
|
||||||
|
This library is released under the [MIT license](LICENSE).
|
@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use Symfony\Polyfill\Ctype as p;
|
||||||
|
|
||||||
|
if (\PHP_VERSION_ID >= 80000) {
|
||||||
|
return require __DIR__.'/bootstrap80.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!function_exists('ctype_alnum')) {
|
||||||
|
function ctype_alnum($text) { return p\Ctype::ctype_alnum($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_alpha')) {
|
||||||
|
function ctype_alpha($text) { return p\Ctype::ctype_alpha($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_cntrl')) {
|
||||||
|
function ctype_cntrl($text) { return p\Ctype::ctype_cntrl($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_digit')) {
|
||||||
|
function ctype_digit($text) { return p\Ctype::ctype_digit($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_graph')) {
|
||||||
|
function ctype_graph($text) { return p\Ctype::ctype_graph($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_lower')) {
|
||||||
|
function ctype_lower($text) { return p\Ctype::ctype_lower($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_print')) {
|
||||||
|
function ctype_print($text) { return p\Ctype::ctype_print($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_punct')) {
|
||||||
|
function ctype_punct($text) { return p\Ctype::ctype_punct($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_space')) {
|
||||||
|
function ctype_space($text) { return p\Ctype::ctype_space($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_upper')) {
|
||||||
|
function ctype_upper($text) { return p\Ctype::ctype_upper($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_xdigit')) {
|
||||||
|
function ctype_xdigit($text) { return p\Ctype::ctype_xdigit($text); }
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use Symfony\Polyfill\Ctype as p;
|
||||||
|
|
||||||
|
if (!function_exists('ctype_alnum')) {
|
||||||
|
function ctype_alnum(mixed $text): bool { return p\Ctype::ctype_alnum($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_alpha')) {
|
||||||
|
function ctype_alpha(mixed $text): bool { return p\Ctype::ctype_alpha($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_cntrl')) {
|
||||||
|
function ctype_cntrl(mixed $text): bool { return p\Ctype::ctype_cntrl($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_digit')) {
|
||||||
|
function ctype_digit(mixed $text): bool { return p\Ctype::ctype_digit($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_graph')) {
|
||||||
|
function ctype_graph(mixed $text): bool { return p\Ctype::ctype_graph($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_lower')) {
|
||||||
|
function ctype_lower(mixed $text): bool { return p\Ctype::ctype_lower($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_print')) {
|
||||||
|
function ctype_print(mixed $text): bool { return p\Ctype::ctype_print($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_punct')) {
|
||||||
|
function ctype_punct(mixed $text): bool { return p\Ctype::ctype_punct($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_space')) {
|
||||||
|
function ctype_space(mixed $text): bool { return p\Ctype::ctype_space($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_upper')) {
|
||||||
|
function ctype_upper(mixed $text): bool { return p\Ctype::ctype_upper($text); }
|
||||||
|
}
|
||||||
|
if (!function_exists('ctype_xdigit')) {
|
||||||
|
function ctype_xdigit(mixed $text): bool { return p\Ctype::ctype_xdigit($text); }
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"name": "symfony/polyfill-ctype",
|
||||||
|
"type": "library",
|
||||||
|
"description": "Symfony polyfill for ctype functions",
|
||||||
|
"keywords": ["polyfill", "compatibility", "portable", "ctype"],
|
||||||
|
"homepage": "https://symfony.com",
|
||||||
|
"license": "MIT",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Gert de Pagter",
|
||||||
|
"email": "BackEndTea@gmail.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Symfony Community",
|
||||||
|
"homepage": "https://symfony.com/contributors"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"require": {
|
||||||
|
"php": ">=7.1"
|
||||||
|
},
|
||||||
|
"provide": {
|
||||||
|
"ext-ctype": "*"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": { "Symfony\\Polyfill\\Ctype\\": "" },
|
||||||
|
"files": [ "bootstrap.php" ]
|
||||||
|
},
|
||||||
|
"suggest": {
|
||||||
|
"ext-ctype": "For best performance"
|
||||||
|
},
|
||||||
|
"minimum-stability": "dev",
|
||||||
|
"extra": {
|
||||||
|
"branch-alias": {
|
||||||
|
"dev-main": "1.27-dev"
|
||||||
|
},
|
||||||
|
"thanks": {
|
||||||
|
"name": "symfony/polyfill",
|
||||||
|
"url": "https://github.com/symfony/polyfill"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,248 @@
|
|||||||
|
CHANGELOG
|
||||||
|
=========
|
||||||
|
|
||||||
|
6.2
|
||||||
|
---
|
||||||
|
|
||||||
|
* Add support for `!php/enum` and `!php/enum *->value`
|
||||||
|
* Deprecate the `!php/const:` tag in key which will be replaced by the `!php/const` tag (without the colon) since 3.4
|
||||||
|
|
||||||
|
6.1
|
||||||
|
---
|
||||||
|
|
||||||
|
* In cases where it will likely improve readability, strings containing single quotes will be double-quoted
|
||||||
|
|
||||||
|
5.4
|
||||||
|
---
|
||||||
|
|
||||||
|
* Add new `lint:yaml dirname --exclude=/dirname/foo.yaml --exclude=/dirname/bar.yaml`
|
||||||
|
option to exclude one or more specific files from multiple file list
|
||||||
|
* Allow negatable for the parse tags option with `--no-parse-tags`
|
||||||
|
|
||||||
|
5.3
|
||||||
|
---
|
||||||
|
|
||||||
|
* Added `github` format support & autodetection to render errors as annotations
|
||||||
|
when running the YAML linter command in a Github Action environment.
|
||||||
|
|
||||||
|
5.1.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Added support for parsing numbers prefixed with `0o` as octal numbers.
|
||||||
|
* Deprecated support for parsing numbers starting with `0` as octal numbers. They will be parsed as strings as of Symfony 6.0. Prefix numbers with `0o`
|
||||||
|
so that they are parsed as octal numbers.
|
||||||
|
|
||||||
|
Before:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
Yaml::parse('072');
|
||||||
|
```
|
||||||
|
|
||||||
|
After:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
Yaml::parse('0o72');
|
||||||
|
```
|
||||||
|
|
||||||
|
* Added `yaml-lint` binary.
|
||||||
|
* Deprecated using the `!php/object` and `!php/const` tags without a value.
|
||||||
|
|
||||||
|
5.0.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Removed support for mappings inside multi-line strings.
|
||||||
|
* removed support for implicit STDIN usage in the `lint:yaml` command, use `lint:yaml -` (append a dash) instead to make it explicit.
|
||||||
|
|
||||||
|
4.4.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Added support for parsing the inline notation spanning multiple lines.
|
||||||
|
* Added support to dump `null` as `~` by using the `Yaml::DUMP_NULL_AS_TILDE` flag.
|
||||||
|
* deprecated accepting STDIN implicitly when using the `lint:yaml` command, use `lint:yaml -` (append a dash) instead to make it explicit.
|
||||||
|
|
||||||
|
4.3.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Using a mapping inside a multi-line string is deprecated and will throw a `ParseException` in 5.0.
|
||||||
|
|
||||||
|
4.2.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* added support for multiple files or directories in `LintCommand`
|
||||||
|
|
||||||
|
4.0.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* The behavior of the non-specific tag `!` is changed and now forces
|
||||||
|
non-evaluating your values.
|
||||||
|
* complex mappings will throw a `ParseException`
|
||||||
|
* support for the comma as a group separator for floats has been dropped, use
|
||||||
|
the underscore instead
|
||||||
|
* support for the `!!php/object` tag has been dropped, use the `!php/object`
|
||||||
|
tag instead
|
||||||
|
* duplicate mapping keys throw a `ParseException`
|
||||||
|
* non-string mapping keys throw a `ParseException`, use the `Yaml::PARSE_KEYS_AS_STRINGS`
|
||||||
|
flag to cast them to strings
|
||||||
|
* `%` at the beginning of an unquoted string throw a `ParseException`
|
||||||
|
* mappings with a colon (`:`) that is not followed by a whitespace throw a
|
||||||
|
`ParseException`
|
||||||
|
* the `Dumper::setIndentation()` method has been removed
|
||||||
|
* being able to pass boolean options to the `Yaml::parse()`, `Yaml::dump()`,
|
||||||
|
`Parser::parse()`, and `Dumper::dump()` methods to configure the behavior of
|
||||||
|
the parser and dumper is no longer supported, pass bitmask flags instead
|
||||||
|
* the constructor arguments of the `Parser` class have been removed
|
||||||
|
* the `Inline` class is internal and no longer part of the BC promise
|
||||||
|
* removed support for the `!str` tag, use the `!!str` tag instead
|
||||||
|
* added support for tagged scalars.
|
||||||
|
|
||||||
|
```yml
|
||||||
|
Yaml::parse('!foo bar', Yaml::PARSE_CUSTOM_TAGS);
|
||||||
|
// returns TaggedValue('foo', 'bar');
|
||||||
|
```
|
||||||
|
|
||||||
|
3.4.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* added support for parsing YAML files using the `Yaml::parseFile()` or `Parser::parseFile()` method
|
||||||
|
|
||||||
|
* the `Dumper`, `Parser`, and `Yaml` classes are marked as final
|
||||||
|
|
||||||
|
* Deprecated the `!php/object:` tag which will be replaced by the
|
||||||
|
`!php/object` tag (without the colon) in 4.0.
|
||||||
|
|
||||||
|
* Deprecated the `!php/const:` tag which will be replaced by the
|
||||||
|
`!php/const` tag (without the colon) in 4.0.
|
||||||
|
|
||||||
|
* Support for the `!str` tag is deprecated, use the `!!str` tag instead.
|
||||||
|
|
||||||
|
* Deprecated using the non-specific tag `!` as its behavior will change in 4.0.
|
||||||
|
It will force non-evaluating your values in 4.0. Use plain integers or `!!float` instead.
|
||||||
|
|
||||||
|
3.3.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Starting an unquoted string with a question mark followed by a space is
|
||||||
|
deprecated and will throw a `ParseException` in Symfony 4.0.
|
||||||
|
|
||||||
|
* Deprecated support for implicitly parsing non-string mapping keys as strings.
|
||||||
|
Mapping keys that are no strings will lead to a `ParseException` in Symfony
|
||||||
|
4.0. Use quotes to opt-in for keys to be parsed as strings.
|
||||||
|
|
||||||
|
Before:
|
||||||
|
|
||||||
|
```php
|
||||||
|
$yaml = <<<YAML
|
||||||
|
null: null key
|
||||||
|
true: boolean true
|
||||||
|
2.0: float key
|
||||||
|
YAML;
|
||||||
|
|
||||||
|
Yaml::parse($yaml);
|
||||||
|
```
|
||||||
|
|
||||||
|
After:
|
||||||
|
|
||||||
|
```php
|
||||||
|
|
||||||
|
$yaml = <<<YAML
|
||||||
|
"null": null key
|
||||||
|
"true": boolean true
|
||||||
|
"2.0": float key
|
||||||
|
YAML;
|
||||||
|
|
||||||
|
Yaml::parse($yaml);
|
||||||
|
```
|
||||||
|
|
||||||
|
* Omitted mapping values will be parsed as `null`.
|
||||||
|
|
||||||
|
* Omitting the key of a mapping is deprecated and will throw a `ParseException` in Symfony 4.0.
|
||||||
|
|
||||||
|
* Added support for dumping empty PHP arrays as YAML sequences:
|
||||||
|
|
||||||
|
```php
|
||||||
|
Yaml::dump([], 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
|
||||||
|
```
|
||||||
|
|
||||||
|
3.2.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Mappings with a colon (`:`) that is not followed by a whitespace are deprecated
|
||||||
|
when the mapping key is not quoted and will lead to a `ParseException` in
|
||||||
|
Symfony 4.0 (e.g. `foo:bar` must be `foo: bar`).
|
||||||
|
|
||||||
|
* Added support for parsing PHP constants:
|
||||||
|
|
||||||
|
```php
|
||||||
|
Yaml::parse('!php/const:PHP_INT_MAX', Yaml::PARSE_CONSTANT);
|
||||||
|
```
|
||||||
|
|
||||||
|
* Support for silently ignoring duplicate mapping keys in YAML has been
|
||||||
|
deprecated and will lead to a `ParseException` in Symfony 4.0.
|
||||||
|
|
||||||
|
3.1.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Added support to dump `stdClass` and `ArrayAccess` objects as YAML mappings
|
||||||
|
through the `Yaml::DUMP_OBJECT_AS_MAP` flag.
|
||||||
|
|
||||||
|
* Strings that are not UTF-8 encoded will be dumped as base64 encoded binary
|
||||||
|
data.
|
||||||
|
|
||||||
|
* Added support for dumping multi line strings as literal blocks.
|
||||||
|
|
||||||
|
* Added support for parsing base64 encoded binary data when they are tagged
|
||||||
|
with the `!!binary` tag.
|
||||||
|
|
||||||
|
* Added support for parsing timestamps as `\DateTime` objects:
|
||||||
|
|
||||||
|
```php
|
||||||
|
Yaml::parse('2001-12-15 21:59:43.10 -5', Yaml::PARSE_DATETIME);
|
||||||
|
```
|
||||||
|
|
||||||
|
* `\DateTime` and `\DateTimeImmutable` objects are dumped as YAML timestamps.
|
||||||
|
|
||||||
|
* Deprecated usage of `%` at the beginning of an unquoted string.
|
||||||
|
|
||||||
|
* Added support for customizing the YAML parser behavior through an optional bit field:
|
||||||
|
|
||||||
|
```php
|
||||||
|
Yaml::parse('{ "foo": "bar", "fiz": "cat" }', Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE | Yaml::PARSE_OBJECT | Yaml::PARSE_OBJECT_FOR_MAP);
|
||||||
|
```
|
||||||
|
|
||||||
|
* Added support for customizing the dumped YAML string through an optional bit field:
|
||||||
|
|
||||||
|
```php
|
||||||
|
Yaml::dump(['foo' => new A(), 'bar' => 1], 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE | Yaml::DUMP_OBJECT);
|
||||||
|
```
|
||||||
|
|
||||||
|
3.0.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Yaml::parse() now throws an exception when a blackslash is not escaped
|
||||||
|
in double-quoted strings
|
||||||
|
|
||||||
|
2.8.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Deprecated usage of a colon in an unquoted mapping value
|
||||||
|
* Deprecated usage of @, \`, | and > at the beginning of an unquoted string
|
||||||
|
* When surrounding strings with double-quotes, you must now escape `\` characters. Not
|
||||||
|
escaping those characters (when surrounded by double-quotes) is deprecated.
|
||||||
|
|
||||||
|
Before:
|
||||||
|
|
||||||
|
```yml
|
||||||
|
class: "Foo\Var"
|
||||||
|
```
|
||||||
|
|
||||||
|
After:
|
||||||
|
|
||||||
|
```yml
|
||||||
|
class: "Foo\\Var"
|
||||||
|
```
|
||||||
|
|
||||||
|
2.1.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Yaml::parse() does not evaluate loaded files as PHP files by default
|
||||||
|
anymore (call Yaml::enablePhpParsing() to get back the old behavior)
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue