PK dw3VH psalm-baseline.xmlnu ٘
getName$parentClass === false
PK dw3V"|pv v README.mdnu ٘ # Doctrine Persistence
[![Build Status](https://travis-ci.org/doctrine/persistence.svg)](https://travis-ci.org/doctrine/persistence)
[![Code Coverage](https://codecov.io/gh/doctrine/persistence/branch/2.1.x/graph/badge.svg)](https://codecov.io/gh/doctrine/persistence/branch/2.1.x)
The Doctrine Persistence project is a library that provides common abstractions for object mapper persistence.
## More resources:
* [Website](https://www.doctrine-project.org/)
* [Documentation](https://www.doctrine-project.org/projects/doctrine-persistence/en/latest/index.html)
* [Downloads](https://github.com/doctrine/persistence/releases)
PK dw3Vnm` ` CONTRIBUTING.mdnu ٘ # Circular dependency
This package has a development dependency on `doctrine/common`, which has a
regular dependency on this package (`^2.0` at the time of writing).
To be able to use Composer, one has to let it understand that this is version 2
(even when developing on 3.0.x), as follows:
```shell
COMPOSER_ROOT_VERSION=2.0 composer update -v
```
PK dw3V
UPGRADE.mdnu ٘ 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 3.1
## Added method `Proxy::__setInitialized()`
Classes implementing `Doctrine\Persistence\Proxy` should implement the new
method. This method will be added to the interface in 4.0.
## Deprecated `RuntimePublicReflectionProperty`
Use `RuntimeReflectionProperty` instead.
# Upgrade to 3.0
## Removed `OnClearEventArgs::clearsAllEntities()` and `OnClearEventArgs::getEntityClass()`
These methods only make sense when partially clearing the object manager, which
is no longer possible.
The second argument of the constructor of `OnClearEventArgs` is removed as well.
## BC Break: removed `ObjectManagerAware`
Implement active record style functionality directly in your application, by
using a `postLoad` event.
## BC Break: removed `AnnotationDriver`
Use `ColocatedMappingDriver` instead.
## BC Break: Removed `MappingException::pathRequired()`
Use `MappingException::pathRequiredForDriver()` instead.
## BC Break: removed `LifecycleEventArgs::getEntity()`
Use `LifecycleEventArgs::getObject()` instead.
## BC Break: removed support for short namespace aliases
- `AbstractClassMetadataFactory::getFqcnFromAlias()` is removed.
- `ClassMetadataFactory` methods now require their `$className` argument to be an
actual FQCN.
## BC Break: removed `ObjectManager::merge()`
`ObjectManagerDecorator::merge()` is removed without replacement.
## BC Break: removed support for `doctrine/cache`
Removed support for using doctrine/cache for metadata caching. The
`setCacheDriver` and `getCacheDriver` methods have been removed from
`Doctrine\Persistence\Mapping\AbstractMetadata`. Please use `getCache` and
`setCache` with a PSR-6 implementation instead.
## BC Break: changed signatures
`$objectName` has been dropped from the signature of `ObjectManager::clear()`.
```diff
- public function clear($objectName = null)
+ public function clear(): void
```
Also, native parameter type declarations have been added on all public APIs.
Native return type declarations have not been added so that it is possible to
implement types compatible with both 2.x and 3.x.
## BC Break: Removed `PersistentObject`
Please implement this functionality directly in your application if you want
ActiveRecord style functionality.
# Upgrade to 2.5
## Deprecated `OnClearEventArgs::clearsAllEntities()` and `OnClearEventArgs::getEntityClass()`
These methods only make sense when partially clearing the object manager, which
is deprecated.
Passing a second argument to the constructor of `OnClearEventArgs` is
deprecated as well.
## Deprecated `ObjectManagerAware`
Along with deprecating `PersistentObject`, deprecating `ObjectManagerAware`
means deprecating support for active record, which already came with a word of
warning. Please implement this directly in your application with a `postLoad`
event if you need active record style functionality.
## Deprecated `MappingException::pathRequired()`
`MappingException::pathRequiredForDriver()` should be used instead.
# Upgrade to 2.4
## Deprecated `AnnotationDriver`
Since attributes were introduced in PHP 8.0, annotations are deprecated.
`AnnotationDriver` is an abstract class that is used when implementing concrete
annotation drivers in dependent packages. It is deprecated in favor of using
`ColocatedMappingDriver` to implement both annotation and attribute based
drivers. This will involve implementing `isTransient()` as well as
`__construct()` and `getReader()` to retain backward compatibility.
# Upgrade to 2.3
## Deprecated using short namespace alias syntax in favor of `::class` syntax.
Before:
```php
$objectManager->find('MyPackage:MyClass', $id);
$objectManager->createQuery('SELECT u FROM MyPackage:MyClass');
```
After:
```php
$objectManager->find(MyClass::class, $id);
$objectManager->createQuery('SELECT u FROM '. MyClass::class);
```
# Upgrade to 2.2
## Deprecated `doctrine/cache` usage for metadata caching
The `setCacheDriver` and `getCacheDriver` methods in
`Doctrine\Persistence\Mapping\AbstractMetadata` have been deprecated. Please
use `getCache` and `setCache` with a PSR-6 implementation instead. Note that
even after switching to PSR-6, `getCacheDriver` will return a cache instance
that wraps the PSR-6 cache. Note that if you use a custom implementation of
doctrine/cache, the library may not be able to provide a forward compatibility
layer. The cache implementation MUST extend the
`Doctrine\Common\Cache\CacheProvider` class.
# Upgrade to 1.2
## Deprecated `ObjectManager::merge()` and `ObjectManager::detach()`
Please handle merge operations in your application, and use
`ObjectManager::clear()` instead.
## Deprecated `PersistentObject`
Please implement this functionality directly in your application if you want
ActiveRecord style functionality.
PK dw3V#4a
# src/Persistence/ManagerRegistry.phpnu ٘ An array of ObjectManager instances
*/
public function getManagers();
/**
* Resets a named object manager.
*
* This method is useful when an object manager has been closed
* because of a rollbacked transaction AND when you think that
* it makes sense to get a new one to replace the closed one.
*
* Be warned that you will get a brand new object manager as
* the existing one is not useable anymore. This means that any
* other object with a dependency on this object manager will
* hold an obsolete reference. You can inject the registry instead
* to avoid this problem.
*
* @param string|null $name The object manager name (null for the default one).
*
* @return ObjectManager
*/
public function resetManager(?string $name = null);
/**
* Gets all object manager names and associated service IDs. A service ID
* is a string that allows to obtain an object manager, typically from a
* PSR-11 container.
*
* @return array An array with object manager names as keys,
* and service IDs as values.
*/
public function getManagerNames();
/**
* Gets the ObjectRepository for a persistent object.
*
* @param string $persistentObject The name of the persistent object.
* @param string|null $persistentManagerName The object manager name (null for the default one).
* @psalm-param class-string $persistentObject
*
* @return ObjectRepository
* @psalm-return ObjectRepository
*
* @template T of object
*/
public function getRepository(
string $persistentObject,
?string $persistentManagerName = null
);
/**
* Gets the object manager associated with a given class.
*
* @param class-string $class A persistent object class name.
*
* @return ObjectManager|null
*/
public function getManagerForClass(string $class);
}
PK dw3VS+ src/Persistence/Proxy.phpnu ٘ */
private $connections;
/** @var array */
private $managers;
/** @var string */
private $defaultConnection;
/** @var string */
private $defaultManager;
/**
* @var string
* @psalm-var class-string
*/
private $proxyInterfaceName;
/**
* @param array $connections
* @param array $managers
* @psalm-param class-string $proxyInterfaceName
*/
public function __construct(
string $name,
array $connections,
array $managers,
string $defaultConnection,
string $defaultManager,
string $proxyInterfaceName
) {
$this->name = $name;
$this->connections = $connections;
$this->managers = $managers;
$this->defaultConnection = $defaultConnection;
$this->defaultManager = $defaultManager;
$this->proxyInterfaceName = $proxyInterfaceName;
}
/**
* Fetches/creates the given services.
*
* A service in this context is connection or a manager instance.
*
* @param string $name The name of the service.
*
* @return ObjectManager The instance of the given service.
*/
abstract protected function getService(string $name);
/**
* Resets the given services.
*
* A service in this context is connection or a manager instance.
*
* @param string $name The name of the service.
*
* @return void
*/
abstract protected function resetService(string $name);
/**
* Gets the name of the registry.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* {@inheritdoc}
*/
public function getConnection(?string $name = null)
{
if ($name === null) {
$name = $this->defaultConnection;
}
if (! isset($this->connections[$name])) {
throw new InvalidArgumentException(
sprintf('Doctrine %s Connection named "%s" does not exist.', $this->name, $name)
);
}
return $this->getService($this->connections[$name]);
}
/**
* {@inheritdoc}
*/
public function getConnectionNames()
{
return $this->connections;
}
/**
* {@inheritdoc}
*/
public function getConnections()
{
$connections = [];
foreach ($this->connections as $name => $id) {
$connections[$name] = $this->getService($id);
}
return $connections;
}
/**
* {@inheritdoc}
*/
public function getDefaultConnectionName()
{
return $this->defaultConnection;
}
/**
* {@inheritdoc}
*/
public function getDefaultManagerName()
{
return $this->defaultManager;
}
/**
* {@inheritdoc}
*
* @throws InvalidArgumentException
*/
public function getManager(?string $name = null)
{
if ($name === null) {
$name = $this->defaultManager;
}
if (! isset($this->managers[$name])) {
throw new InvalidArgumentException(
sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name)
);
}
return $this->getService($this->managers[$name]);
}
/**
* {@inheritDoc}
*/
public function getManagerForClass(string $class)
{
$proxyClass = new ReflectionClass($class);
if ($proxyClass->isAnonymous()) {
return null;
}
if ($proxyClass->implementsInterface($this->proxyInterfaceName)) {
$parentClass = $proxyClass->getParentClass();
if ($parentClass === false) {
return null;
}
$class = $parentClass->getName();
}
foreach ($this->managers as $id) {
$manager = $this->getService($id);
if (! $manager->getMetadataFactory()->isTransient($class)) {
return $manager;
}
}
return null;
}
/**
* {@inheritdoc}
*/
public function getManagerNames()
{
return $this->managers;
}
/**
* {@inheritdoc}
*/
public function getManagers()
{
$managers = [];
foreach ($this->managers as $name => $id) {
$manager = $this->getService($id);
$managers[$name] = $manager;
}
return $managers;
}
/**
* {@inheritdoc}
*/
public function getRepository(
string $persistentObject,
?string $persistentManagerName = null
) {
return $this
->selectManager($persistentObject, $persistentManagerName)
->getRepository($persistentObject);
}
/**
* {@inheritdoc}
*/
public function resetManager(?string $name = null)
{
if ($name === null) {
$name = $this->defaultManager;
}
if (! isset($this->managers[$name])) {
throw new InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
}
// force the creation of a new document manager
// if the current one is closed
$this->resetService($this->managers[$name]);
return $this->getManager($name);
}
/** @psalm-param class-string $persistentObject */
private function selectManager(
string $persistentObject,
?string $persistentManagerName = null
): ObjectManager {
if ($persistentManagerName !== null) {
return $this->getManager($persistentManagerName);
}
return $this->getManagerForClass($persistentObject) ?? $this->getManager();
}
}
PK dw3VL\ > src/Persistence/Reflection/RuntimePublicReflectionProperty.phpnu ٘ getName()] ?? null : parent::getValue();
}
/**
* {@inheritDoc}
*
* Avoids triggering lazy loading via `__set` if the provided object
* is a {@see \Doctrine\Common\Proxy\Proxy}.
*
* @link https://bugs.php.net/bug.php?id=63463
*
* @param object|null $object
* @param mixed $value
*
* @return void
*/
#[ReturnTypeWillChange]
public function setValue($object, $value = null)
{
if (! ($object instanceof Proxy && ! $object->__isInitialized())) {
parent::setValue($object, $value);
return;
}
$originalInitializer = $object->__getInitializer();
$object->__setInitializer(null);
parent::setValue($object, $value);
$object->__setInitializer($originalInitializer);
}
}
PK dw3V0G 8 src/Persistence/Reflection/RuntimeReflectionProperty.phpnu ٘ key = $this->isPrivate() ? "\0" . $class . "\0" . $name : ($this->isProtected() ? "\0*\0" . $name : $name);
}
/**
* {@inheritDoc}
*
* @return mixed
*/
#[ReturnTypeWillChange]
public function getValue($object = null)
{
if ($object === null) {
return parent::getValue($object);
}
return ((array) $object)[$this->key] ?? null;
}
/**
* {@inheritDoc}
*
* @param object|null $object
* @param mixed $value
*
* @return void
*/
#[ReturnTypeWillChange]
public function setValue($object, $value = null)
{
if (! ($object instanceof Proxy && ! $object->__isInitialized())) {
parent::setValue($object, $value);
return;
}
if ($object instanceof CommonProxy) {
$originalInitializer = $object->__getInitializer();
$object->__setInitializer(null);
parent::setValue($object, $value);
$object->__setInitializer($originalInitializer);
return;
}
if (! method_exists($object, '__setInitialized')) {
return;
}
$object->__setInitialized(true);
parent::setValue($object, $value);
$object->__setInitialized(false);
}
}
PK dw3V22 5 src/Persistence/Reflection/EnumReflectionProperty.phpnu ٘ */
private $enumType;
/** @param class-string $enumType */
public function __construct(ReflectionProperty $originalReflectionProperty, string $enumType)
{
$this->originalReflectionProperty = $originalReflectionProperty;
$this->enumType = $enumType;
}
/**
* {@inheritDoc}
*
* Converts enum instance to its value.
*
* @param object|null $object
*
* @return int|string|null
*/
#[ReturnTypeWillChange]
public function getValue($object = null)
{
if ($object === null) {
return null;
}
$enum = $this->originalReflectionProperty->getValue($object);
if ($enum === null) {
return null;
}
return $enum->value;
}
/**
* Converts enum value to enum instance.
*
* @param object $object
* @param mixed $value
*/
public function setValue($object, $value = null): void
{
if ($value !== null) {
$value = $this->enumType::from($value);
}
$this->originalReflectionProperty->setValue($object, $value);
}
}
PK dw3Voy( ( C src/Persistence/Reflection/TypedNoDefaultReflectionPropertyBase.phpnu ٘ isInitialized($object) ? parent::getValue($object) : null;
}
/**
* {@inheritDoc}
*
* Works around the problem with setting typed no default properties to
* NULL which is not supported, instead unset() to uninitialize.
*
* @link https://github.com/doctrine/orm/issues/7999
*
* @param object|null $object
*
* @return void
*/
#[ReturnTypeWillChange]
public function setValue($object, $value = null)
{
if ($value === null && $this->hasType() && ! $this->getType()->allowsNull()) {
$propertyName = $this->getName();
$unsetter = function () use ($propertyName): void {
unset($this->$propertyName);
};
$unsetter = $unsetter->bindTo($object, $this->getDeclaringClass()->getName());
assert($unsetter instanceof Closure);
$unsetter();
return;
}
parent::setValue($object, $value);
}
}
PK dw3Vz L src/Persistence/Reflection/TypedNoDefaultRuntimePublicReflectionProperty.phpnu ٘ supportsTypedPropertiesWorkaround = version_compare(phpversion(), '7.4.0') >= 0;
}
/**
* {@inheritDoc}
*/
public function getParentClasses(string $class)
{
if (! class_exists($class)) {
throw MappingException::nonExistingClass($class);
}
$parents = class_parents($class);
assert($parents !== false);
return $parents;
}
/**
* {@inheritDoc}
*/
public function getClassShortName(string $class)
{
$reflectionClass = new ReflectionClass($class);
return $reflectionClass->getShortName();
}
/**
* {@inheritDoc}
*/
public function getClassNamespace(string $class)
{
$reflectionClass = new ReflectionClass($class);
return $reflectionClass->getNamespaceName();
}
/**
* @psalm-param class-string $class
*
* @return ReflectionClass
* @psalm-return ReflectionClass
*
* @template T of object
*/
public function getClass(string $class)
{
return new ReflectionClass($class);
}
/**
* {@inheritDoc}
*/
public function getAccessibleProperty(string $class, string $property)
{
$reflectionProperty = new RuntimeReflectionProperty($class, $property);
if ($this->supportsTypedPropertiesWorkaround && ! array_key_exists($property, $this->getClass($class)->getDefaultProperties())) {
$reflectionProperty = new TypedNoDefaultReflectionProperty($class, $property);
}
$reflectionProperty->setAccessible(true);
return $reflectionProperty;
}
/**
* {@inheritDoc}
*/
public function hasPublicMethod(string $class, string $method)
{
try {
$reflectionMethod = new ReflectionMethod($class, $method);
} catch (ReflectionException $e) {
return false;
}
return $reflectionMethod->isPublic();
}
}
PK dw3Vc 3 src/Persistence/Mapping/StaticReflectionService.phpnu ٘
*/
public function getAllMetadata();
/**
* Gets the class metadata descriptor for a class.
*
* @param class-string $className The name of the class.
*
* @return ClassMetadata
* @psalm-return T
*/
public function getMetadataFor(string $className);
/**
* Checks whether the factory has the metadata for a class loaded already.
*
* @param class-string $className
*
* @return bool TRUE if the metadata of the class in question is already loaded, FALSE otherwise.
*/
public function hasMetadataFor(string $className);
/**
* Sets the metadata descriptor for a specific class.
*
* @param class-string $className
* @psalm-param T $class
*
* @return void
*/
public function setMetadataFor(string $className, ClassMetadata $class);
/**
* Returns whether the class with the specified name should have its metadata loaded.
* This is only the case if it is either mapped directly or as a MappedSuperclass.
*
* @psalm-param class-string $className
*
* @return bool
*/
public function isTransient(string $className);
}
PK dw3V̟' ' , src/Persistence/Mapping/MappingException.phpnu ٘ $namespaces
*
* @return self
*/
public static function classNotFoundInNamespaces(
string $className,
array $namespaces
) {
return new self(sprintf(
"The class '%s' was not found in the chain configured namespaces %s",
$className,
implode(', ', $namespaces)
));
}
/** @param class-string $driverClassName */
public static function pathRequiredForDriver(string $driverClassName): self
{
return new self(sprintf(
'Specifying the paths to your entities is required when using %s to retrieve all class names.',
$driverClassName
));
}
/** @return self */
public static function fileMappingDriversRequireConfiguredDirectoryPath(
?string $path = null
) {
if ($path !== null) {
$path = '[' . $path . ']';
}
return new self(sprintf(
'File mapping drivers must have a valid directory path, ' .
'however the given path %s seems to be incorrect!',
(string) $path
));
}
/** @return self */
public static function mappingFileNotFound(string $entityName, string $fileName)
{
return new self(sprintf(
"No mapping file found named '%s' for class '%s'.",
$fileName,
$entityName
));
}
/** @return self */
public static function invalidMappingFile(string $entityName, string $fileName)
{
return new self(sprintf(
"Invalid mapping file '%s' for class '%s'.",
$fileName,
$entityName
));
}
/** @return self */
public static function nonExistingClass(string $className)
{
return new self(sprintf("Class '%s' does not exist", $className));
}
/** @param class-string $className */
public static function classIsAnonymous(string $className): self
{
return new self(sprintf('Class "%s" is anonymous', $className));
}
}
PK dw3V[5 5 ) src/Persistence/Mapping/ClassMetadata.phpnu ٘
*/
public function getName();
/**
* Gets the mapped identifier field name.
*
* The returned structure is an array of the identifier field names.
*
* @return array
* @psalm-return list
*/
public function getIdentifier();
/**
* Gets the ReflectionClass instance for this mapped class.
*
* @return ReflectionClass
*/
public function getReflectionClass();
/**
* Checks if the given field name is a mapped identifier for this class.
*
* @return bool
*/
public function isIdentifier(string $fieldName);
/**
* Checks if the given field is a mapped property for this class.
*
* @return bool
*/
public function hasField(string $fieldName);
/**
* Checks if the given field is a mapped association for this class.
*
* @return bool
*/
public function hasAssociation(string $fieldName);
/**
* Checks if the given field is a mapped single valued association for this class.
*
* @return bool
*/
public function isSingleValuedAssociation(string $fieldName);
/**
* Checks if the given field is a mapped collection valued association for this class.
*
* @return bool
*/
public function isCollectionValuedAssociation(string $fieldName);
/**
* A numerically indexed list of field names of this persistent class.
*
* This array includes identifier fields if present on this class.
*
* @return array
*/
public function getFieldNames();
/**
* Returns an array of identifier field names numerically indexed.
*
* @return array
*/
public function getIdentifierFieldNames();
/**
* Returns a numerically indexed list of association names of this persistent class.
*
* This array includes identifier associations if present on this class.
*
* @return array
*/
public function getAssociationNames();
/**
* Returns a type name of this field.
*
* This type names can be implementation specific but should at least include the php types:
* integer, string, boolean, float/double, datetime.
*
* @return string|null
*/
public function getTypeOfField(string $fieldName);
/**
* Returns the target class name of the given association.
*
* @return string|null
* @psalm-return class-string|null
*/
public function getAssociationTargetClass(string $assocName);
/**
* Checks if the association is the inverse side of a bidirectional association.
*
* @return bool
*/
public function isAssociationInverseSide(string $assocName);
/**
* Returns the target field of the owning side of the association.
*
* @return string
*/
public function getAssociationMappedByTargetField(string $assocName);
/**
* Returns the identifier of this object as an array with field name as key.
*
* Has to return an empty array if no identifier isset.
*
* @return array
*/
public function getIdentifierValues(object $object);
}
PK dw3Vd - src/Persistence/Mapping/ReflectionService.phpnu ٘ $class
*
* @return ReflectionClass|null
* @psalm-return ReflectionClass|null
*
* @template T of object
*/
public function getClass(string $class);
/**
* Returns an accessible property (setAccessible(true)) or null.
*
* @psalm-param class-string $class
*
* @return ReflectionProperty|null
*/
public function getAccessibleProperty(string $class, string $property);
/**
* Checks if the class have a public method with the given name.
*
* @psalm-param class-string $class
*
* @return bool
*/
public function hasPublicMethod(string $class, string $method);
}
PK dw3V4! 2 src/Persistence/Mapping/Driver/StaticPHPDriver.phpnu ٘
*/
private $paths = [];
/**
* Map of all class names.
*
* @var array
* @psalm-var list
*/
private $classNames;
/** @param array|string $paths */
public function __construct($paths)
{
$this->addPaths((array) $paths);
}
/**
* @param array $paths
*
* @return void
*/
public function addPaths(array $paths)
{
$this->paths = array_unique(array_merge($this->paths, $paths));
}
/**
* {@inheritdoc}
*/
public function loadMetadataForClass(string $className, ClassMetadata $metadata)
{
$className::loadMetadata($metadata);
}
/**
* {@inheritDoc}
*
* @todo Same code exists in ColocatedMappingDriver, should we re-use it
* somehow or not worry about it?
*/
public function getAllClassNames()
{
if ($this->classNames !== null) {
return $this->classNames;
}
if ($this->paths === []) {
throw MappingException::pathRequiredForDriver(static::class);
}
$classes = [];
$includedFiles = [];
foreach ($this->paths as $path) {
if (! is_dir($path)) {
throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
}
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($iterator as $file) {
if ($file->getBasename('.php') === $file->getBasename()) {
continue;
}
$sourceFile = realpath($file->getPathName());
require_once $sourceFile;
$includedFiles[] = $sourceFile;
}
}
$declared = get_declared_classes();
foreach ($declared as $className) {
$rc = new ReflectionClass($className);
$sourceFile = $rc->getFileName();
if (! in_array($sourceFile, $includedFiles, true) || $this->isTransient($className)) {
continue;
}
$classes[] = $className;
}
$this->classNames = $classes;
return $classes;
}
/**
* {@inheritdoc}
*/
public function isTransient(string $className)
{
return ! method_exists($className, 'loadMetadata');
}
}
PK dw3VK K 5 src/Persistence/Mapping/Driver/SymfonyFileLocator.phpnu ٘
*/
protected $paths = [];
/**
* A map of mapping directory path to namespace prefix used to expand class shortnames.
*
* @var array
*/
protected $prefixes = [];
/**
* File extension that is searched for.
*
* @var string|null
*/
protected $fileExtension;
/**
* Represents PHP namespace delimiters when looking for files
*
* @var string
*/
private $nsSeparator;
/**
* @param array $prefixes
* @param string $nsSeparator String which would be used when converting FQCN
* to filename and vice versa. Should not be empty
*/
public function __construct(
array $prefixes,
string $fileExtension = '',
string $nsSeparator = '.'
) {
$this->addNamespacePrefixes($prefixes);
$this->fileExtension = $fileExtension;
if ($nsSeparator === '') {
throw new InvalidArgumentException('Namespace separator should not be empty');
}
$this->nsSeparator = $nsSeparator;
}
/**
* Adds Namespace Prefixes.
*
* @param array $prefixes
*
* @return void
*/
public function addNamespacePrefixes(array $prefixes)
{
$this->prefixes = array_merge($this->prefixes, $prefixes);
$this->paths = array_merge($this->paths, array_keys($prefixes));
}
/**
* Gets Namespace Prefixes.
*
* @return string[]
*/
public function getNamespacePrefixes()
{
return $this->prefixes;
}
/**
* {@inheritDoc}
*/
public function getPaths()
{
return $this->paths;
}
/**
* {@inheritDoc}
*/
public function getFileExtension()
{
return $this->fileExtension;
}
/**
* Sets the file extension used to look for mapping files under.
*
* @param string $fileExtension The file extension to set.
*
* @return void
*/
public function setFileExtension(string $fileExtension)
{
$this->fileExtension = $fileExtension;
}
/**
* {@inheritDoc}
*/
public function fileExists(string $className)
{
$defaultFileName = str_replace('\\', $this->nsSeparator, $className) . $this->fileExtension;
foreach ($this->paths as $path) {
if (! isset($this->prefixes[$path])) {
// global namespace class
if (is_file($path . DIRECTORY_SEPARATOR . $defaultFileName)) {
return true;
}
continue;
}
$prefix = $this->prefixes[$path];
if (strpos($className, $prefix . '\\') !== 0) {
continue;
}
$filename = $path . '/' . strtr(substr($className, strlen($prefix) + 1), '\\', $this->nsSeparator) . $this->fileExtension;
if (is_file($filename)) {
return true;
}
}
return false;
}
/**
* {@inheritDoc}
*/
public function getAllClassNames(?string $globalBasename = null)
{
if ($this->paths === []) {
return [];
}
$classes = [];
foreach ($this->paths as $path) {
if (! is_dir($path)) {
throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
}
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($iterator as $file) {
$fileName = $file->getBasename($this->fileExtension);
if ($fileName === $file->getBasename() || $fileName === $globalBasename) {
continue;
}
// NOTE: All files found here means classes are not transient!
if (isset($this->prefixes[$path])) {
// Calculate namespace suffix for given prefix as a relative path from basepath to file path
$nsSuffix = strtr(
substr($this->realpath($file->getPath()), strlen($this->realpath($path))),
$this->nsSeparator,
'\\'
);
/** @psalm-var class-string */
$class = $this->prefixes[$path] . str_replace(DIRECTORY_SEPARATOR, '\\', $nsSuffix) . '\\' . str_replace($this->nsSeparator, '\\', $fileName);
} else {
/** @psalm-var class-string */
$class = str_replace($this->nsSeparator, '\\', $fileName);
}
$classes[] = $class;
}
}
return $classes;
}
/**
* {@inheritDoc}
*/
public function findMappingFile(string $className)
{
$defaultFileName = str_replace('\\', $this->nsSeparator, $className) . $this->fileExtension;
foreach ($this->paths as $path) {
if (! isset($this->prefixes[$path])) {
if (is_file($path . DIRECTORY_SEPARATOR . $defaultFileName)) {
return $path . DIRECTORY_SEPARATOR . $defaultFileName;
}
continue;
}
$prefix = $this->prefixes[$path];
if (strpos($className, $prefix . '\\') !== 0) {
continue;
}
$filename = $path . '/' . strtr(substr($className, strlen($prefix) + 1), '\\', $this->nsSeparator) . $this->fileExtension;
if (is_file($filename)) {
return $filename;
}
}
$pos = strrpos($className, '\\');
assert(is_int($pos));
throw MappingException::mappingFileNotFound(
$className,
substr($className, $pos + 1) . $this->fileExtension
);
}
private function realpath(string $path): string
{
$realpath = realpath($path);
if ($realpath === false) {
throw new RuntimeException(sprintf('Could not get realpath for %s', $path));
}
return $realpath;
}
}
PK dw3V 9 src/Persistence/Mapping/Driver/ColocatedMappingDriver.phpnu ٘
*/
protected $paths = [];
/**
* The paths excluded from path where to look for mapping files.
*
* @var array
*/
protected $excludePaths = [];
/**
* The file extension of mapping documents.
*
* @var string
*/
protected $fileExtension = '.php';
/**
* Cache for getAllClassNames().
*
* @var array|null
* @psalm-var list|null
*/
protected $classNames;
/**
* Appends lookup paths to metadata driver.
*
* @param array $paths
*
* @return void
*/
public function addPaths(array $paths)
{
$this->paths = array_unique(array_merge($this->paths, $paths));
}
/**
* Retrieves the defined metadata lookup paths.
*
* @return array
*/
public function getPaths()
{
return $this->paths;
}
/**
* Append exclude lookup paths to metadata driver.
*
* @param string[] $paths
*
* @return void
*/
public function addExcludePaths(array $paths)
{
$this->excludePaths = array_unique(array_merge($this->excludePaths, $paths));
}
/**
* Retrieve the defined metadata lookup exclude paths.
*
* @return array
*/
public function getExcludePaths()
{
return $this->excludePaths;
}
/**
* Gets the file extension used to look for mapping files under.
*
* @return string
*/
public function getFileExtension()
{
return $this->fileExtension;
}
/**
* Sets the file extension used to look for mapping files under.
*
* @return void
*/
public function setFileExtension(string $fileExtension)
{
$this->fileExtension = $fileExtension;
}
/**
* {@inheritDoc}
*
* Returns whether the class with the specified name is transient. Only non-transient
* classes, that is entities and mapped superclasses, should have their metadata loaded.
*
* @psalm-param class-string $className
*
* @return bool
*/
abstract public function isTransient(string $className);
/**
* Gets the names of all mapped classes known to this driver.
*
* @return string[] The names of all mapped classes known to this driver.
* @psalm-return list
*/
public function getAllClassNames()
{
if ($this->classNames !== null) {
return $this->classNames;
}
if ($this->paths === []) {
throw MappingException::pathRequiredForDriver(static::class);
}
$classes = [];
$includedFiles = [];
foreach ($this->paths as $path) {
if (! is_dir($path)) {
throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
}
$iterator = new RegexIterator(
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::LEAVES_ONLY
),
'/^.+' . preg_quote($this->fileExtension) . '$/i',
RecursiveRegexIterator::GET_MATCH
);
foreach ($iterator as $file) {
$sourceFile = $file[0];
if (preg_match('(^phar:)i', $sourceFile) === 0) {
$sourceFile = realpath($sourceFile);
}
foreach ($this->excludePaths as $excludePath) {
$realExcludePath = realpath($excludePath);
assert($realExcludePath !== false);
$exclude = str_replace('\\', '/', $realExcludePath);
$current = str_replace('\\', '/', $sourceFile);
if (strpos($current, $exclude) !== false) {
continue 2;
}
}
require_once $sourceFile;
$includedFiles[] = $sourceFile;
}
}
$declared = get_declared_classes();
foreach ($declared as $className) {
$rc = new ReflectionClass($className);
$sourceFile = $rc->getFileName();
if (! in_array($sourceFile, $includedFiles, true) || $this->isTransient($className)) {
continue;
}
$classes[] = $className;
}
$this->classNames = $classes;
return $classes;
}
}
PK dw3VΘ/ 5 src/Persistence/Mapping/Driver/MappingDriverChain.phpnu ٘ */
private $drivers = [];
/**
* Gets the default driver.
*
* @return MappingDriver|null
*/
public function getDefaultDriver()
{
return $this->defaultDriver;
}
/**
* Set the default driver.
*
* @return void
*/
public function setDefaultDriver(MappingDriver $driver)
{
$this->defaultDriver = $driver;
}
/**
* Adds a nested driver.
*
* @return void
*/
public function addDriver(MappingDriver $nestedDriver, string $namespace)
{
$this->drivers[$namespace] = $nestedDriver;
}
/**
* Gets the array of nested drivers.
*
* @return array $drivers
*/
public function getDrivers()
{
return $this->drivers;
}
/**
* {@inheritDoc}
*/
public function loadMetadataForClass(string $className, ClassMetadata $metadata)
{
foreach ($this->drivers as $namespace => $driver) {
if (strpos($className, $namespace) === 0) {
$driver->loadMetadataForClass($className, $metadata);
return;
}
}
if ($this->defaultDriver !== null) {
$this->defaultDriver->loadMetadataForClass($className, $metadata);
return;
}
throw MappingException::classNotFoundInNamespaces($className, array_keys($this->drivers));
}
/**
* {@inheritDoc}
*/
public function getAllClassNames()
{
$classNames = [];
$driverClasses = [];
foreach ($this->drivers as $namespace => $driver) {
$oid = spl_object_hash($driver);
if (! isset($driverClasses[$oid])) {
$driverClasses[$oid] = $driver->getAllClassNames();
}
foreach ($driverClasses[$oid] as $className) {
if (strpos($className, $namespace) !== 0) {
continue;
}
$classNames[$className] = true;
}
}
if ($this->defaultDriver !== null) {
foreach ($this->defaultDriver->getAllClassNames() as $className) {
$classNames[$className] = true;
}
}
return array_keys($classNames);
}
/**
* {@inheritDoc}
*/
public function isTransient(string $className)
{
foreach ($this->drivers as $namespace => $driver) {
if (strpos($className, $namespace) === 0) {
return $driver->isTransient($className);
}
}
if ($this->defaultDriver !== null) {
return $this->defaultDriver->isTransient($className);
}
return true;
}
}
PK dw3V" , src/Persistence/Mapping/Driver/PHPDriver.phpnu ٘
*/
protected $metadata;
/** @param string|array|FileLocator $locator */
public function __construct($locator)
{
parent::__construct($locator, '.php');
}
/**
* {@inheritDoc}
*/
public function loadMetadataForClass(string $className, ClassMetadata $metadata)
{
$this->metadata = $metadata;
$this->loadMappingFile($this->locator->findMappingFile($className));
}
/**
* {@inheritDoc}
*/
protected function loadMappingFile(string $file)
{
$metadata = $this->metadata;
include $file;
return [$metadata->getName() => $metadata];
}
}
PK dw3VFAz - src/Persistence/Mapping/Driver/FileDriver.phpnu ٘ >|null
*/
protected $classCache;
/** @var string */
protected $globalBasename = '';
/**
* Initializes a new FileDriver that looks in the given path(s) for mapping
* documents and operates in the specified operating mode.
*
* @param string|array|FileLocator $locator A FileLocator or one/multiple paths
* where mapping documents can be found.
*/
public function __construct($locator, ?string $fileExtension = null)
{
if ($locator instanceof FileLocator) {
$this->locator = $locator;
} else {
$this->locator = new DefaultFileLocator((array) $locator, $fileExtension);
}
}
/**
* Sets the global basename.
*
* @return void
*/
public function setGlobalBasename(string $file)
{
$this->globalBasename = $file;
}
/**
* Retrieves the global basename.
*
* @return string|null
*/
public function getGlobalBasename()
{
return $this->globalBasename;
}
/**
* Gets the element of schema meta data for the class from the mapping file.
* This will lazily load the mapping file if it is not loaded yet.
*
* @psalm-param class-string $className
*
* @return ClassMetadata The element of schema meta data.
* @psalm-return ClassMetadata