Skip to content

Commit

Permalink
Merge branch 'master' of github.com:doctrine/common
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Aug 31, 2015
2 parents b3ae747 + 7d4f8e4 commit 0009b8f
Show file tree
Hide file tree
Showing 45 changed files with 752 additions and 160 deletions.
10 changes: 9 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
language: php

sudo: false

cache:
directory:
- $HOME/.composer/cache

php:
- 5.3.3
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

before_script:
Expand All @@ -15,4 +23,4 @@ script:

matrix:
allow_failures:
- php: hhvm
- php: 7.0
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ The Doctrine Common project is a library that provides extensions to core PHP fu
## More resources:

* [Website](http://www.doctrine-project.org)
* [Documentation](http://www.doctrine-project.org/projects/common/current/docs/en)
* [Documentation](http://docs.doctrine-project.org/projects/doctrine-common/en/latest/)
* [Issue Tracker](http://www.doctrine-project.org/jira/browse/DCOM)
* [Downloads](http://github.com/doctrine/common/downloads)
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.5.x-dev"
"dev-master": "2.6.x-dev"
}
},
"archive": {
Expand Down
64 changes: 28 additions & 36 deletions lib/Doctrine/Common/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
*
* @author Roman Borschel <roman@code-factory.org>
* @since 2.0
*
* @deprecated the ClassLoader is deprecated and will be removed in version 3.0 of doctrine/common.
*/
class ClassLoader
{
Expand Down Expand Up @@ -173,15 +175,19 @@ public function unregister()
*/
public function loadClass($className)
{
if ($this->namespace !== null && strpos($className, $this->namespace.$this->namespaceSeparator) !== 0) {
if (self::typeExists($className)) {
return true;
}

if (! $this->canLoadClass($className)) {
return false;
}

require ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '')
. str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $className)
. $this->fileExtension;

return true;
return self::typeExists($className);
}

/**
Expand Down Expand Up @@ -231,37 +237,7 @@ public function canLoadClass($className)
*/
public static function classExists($className)
{
if (class_exists($className, false) || interface_exists($className, false)) {
return true;
}

foreach (spl_autoload_functions() as $loader) {
if (is_array($loader)) { // array(???, ???)
if (is_object($loader[0])) {
if ($loader[0] instanceof ClassLoader) { // array($obj, 'methodName')
if ($loader[0]->canLoadClass($className)) {
return true;
}
} else if ($loader[0]->{$loader[1]}($className)) {
return true;
}
} else if ($loader[0]::$loader[1]($className)) { // array('ClassName', 'methodName')
return true;
}
} else if ($loader instanceof \Closure) { // function($className) {..}
if ($loader($className)) {
return true;
}
} else if (is_string($loader) && $loader($className)) { // "MyClass::loadClass"
return true;
}

if (class_exists($className, false) || interface_exists($className, false)) {
return true;
}
}

return false;
return self::typeExists($className, true);
}

/**
Expand All @@ -276,13 +252,29 @@ public static function getClassLoader($className)
{
foreach (spl_autoload_functions() as $loader) {
if (is_array($loader)
&& $loader[0] instanceof ClassLoader
&& $loader[0]->canLoadClass($className)
&& ($classLoader = reset($loader))
&& $classLoader instanceof ClassLoader
&& $classLoader->canLoadClass($className)
) {
return $loader[0];
return $classLoader;
}
}

return null;
}

/**
* Checks whether a given type exists
*
* @param string $type
* @param bool $autoload
*
* @return bool
*/
private static function typeExists($type, $autoload = false)
{
return class_exists($type, $autoload)
|| interface_exists($type, $autoload)
|| (function_exists('trait_exists') && trait_exists($type, $autoload));
}
}
4 changes: 1 addition & 3 deletions lib/Doctrine/Common/Persistence/AbstractManagerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

namespace Doctrine\Common\Persistence;

use Doctrine\Common\Persistence\ManagerRegistry;

/**
* Abstract implementation of the ManagerRegistry contract.
*
Expand Down Expand Up @@ -192,7 +190,7 @@ public function getManagerForClass($class)
{
// Check for namespace alias
if (strpos($class, ':') !== false) {
list($namespaceAlias, $simpleClassName) = explode(':', $class);
list($namespaceAlias, $simpleClassName) = explode(':', $class, 2);
$class = $this->getAliasNamespace($namespaceAlias) . '\\' . $simpleClassName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
namespace Doctrine\Common\Persistence\Event;

use Doctrine\Common\EventArgs;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\ObjectManager;

/**
* Class that holds event arguments for a loadMetadata event.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

namespace Doctrine\Common\Persistence\Event;

use Doctrine\Common\EventArgs;
use Doctrine\Common\Persistence\ObjectManager;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Util\ClassUtils;
use ReflectionException;

/**
* The ClassMetadataFactory is used to create ClassMetadata objects that contain all the
Expand Down Expand Up @@ -50,7 +51,7 @@ abstract class AbstractClassMetadataFactory implements ClassMetadataFactory
private $cacheDriver;

/**
* @var array
* @var ClassMetadata[]
*/
private $loadedMetadata = array();

Expand Down Expand Up @@ -89,7 +90,7 @@ public function getCacheDriver()
/**
* Returns an array of all the loaded metadata currently in memory.
*
* @return array
* @return ClassMetadata[]
*/
public function getLoadedMetadata()
{
Expand Down Expand Up @@ -178,47 +179,60 @@ abstract protected function isEntity(ClassMetadata $class);
*
* @param string $className The name of the class.
*
* @return \Doctrine\Common\Persistence\Mapping\ClassMetadata
* @return ClassMetadata
*
* @throws ReflectionException
* @throws MappingException
*/
public function getMetadataFor($className)
{
if (isset($this->loadedMetadata[$className])) {
return $this->loadedMetadata[$className];
}

$realClassName = $className;

// Check for namespace alias
if (strpos($className, ':') !== false) {
list($namespaceAlias, $simpleClassName) = explode(':', $className);
list($namespaceAlias, $simpleClassName) = explode(':', $className, 2);

$realClassName = $this->getFqcnFromAlias($namespaceAlias, $simpleClassName);
} else {
$realClassName = ClassUtils::getRealClass($realClassName);
$realClassName = ClassUtils::getRealClass($className);
}

if (isset($this->loadedMetadata[$realClassName])) {
// We do not have the alias name in the map, include it
$this->loadedMetadata[$className] = $this->loadedMetadata[$realClassName];

return $this->loadedMetadata[$realClassName];
return $this->loadedMetadata[$className] = $this->loadedMetadata[$realClassName];
}

if ($this->cacheDriver) {
if (($cached = $this->cacheDriver->fetch($realClassName . $this->cacheSalt)) !== false) {
$this->loadedMetadata[$realClassName] = $cached;
$this->wakeupReflection($cached, $this->getReflectionService());
} else {
foreach ($this->loadMetadata($realClassName) as $loadedClassName) {
$this->cacheDriver->save(
$loadedClassName . $this->cacheSalt, $this->loadedMetadata[$loadedClassName], null
);
$loadingException = null;

try {
if ($this->cacheDriver) {
if (($cached = $this->cacheDriver->fetch($realClassName . $this->cacheSalt)) !== false) {
$this->loadedMetadata[$realClassName] = $cached;

$this->wakeupReflection($cached, $this->getReflectionService());
} else {
foreach ($this->loadMetadata($realClassName) as $loadedClassName) {
$this->cacheDriver->save(
$loadedClassName . $this->cacheSalt,
$this->loadedMetadata[$loadedClassName],
null
);
}
}
} else {
$this->loadMetadata($realClassName);
}
} else {
$this->loadMetadata($realClassName);
} catch (MappingException $loadingException) {
if (! $fallbackMetadataResponse = $this->onNotFoundMetadata($realClassName)) {
throw $loadingException;
}

$this->loadedMetadata[$realClassName] = $fallbackMetadataResponse;
}

if ($className != $realClassName) {
if ($className !== $realClassName) {
// We do not have the alias name in the map, include it
$this->loadedMetadata[$className] = $this->loadedMetadata[$realClassName];
}
Expand Down Expand Up @@ -334,6 +348,20 @@ protected function loadMetadata($name)
return $loaded;
}

/**
* Provides a fallback hook for loading metadata when loading failed due to reflection/mapping exceptions
*
* Override this method to implement a fallback strategy for failed metadata loading
*
* @param string $className
*
* @return \Doctrine\Common\Persistence\Mapping\ClassMetadata|null
*/
protected function onNotFoundMetadata($className)
{
return null;
}

/**
* Actually loads the metadata from the underlying metadata.
*
Expand Down Expand Up @@ -367,7 +395,7 @@ public function isTransient($class)

// Check for namespace alias
if (strpos($class, ':') !== false) {
list($namespaceAlias, $simpleClassName) = explode(':', $class);
list($namespaceAlias, $simpleClassName) = explode(':', $class, 2);
$class = $this->getFqcnFromAlias($namespaceAlias, $simpleClassName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
namespace Doctrine\Common\Persistence\Mapping\Driver;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Persistence\Mapping\MappingException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DefaultFileLocator implements FileLocator
* documents and operates in the specified operating mode.
*
* @param string|array $paths One or multiple paths where mapping documents can be found.
* @param string|null $fileExtension The file extension of mapping documents.
* @param string|null $fileExtension The file extension of mapping documents, usually prefixed with a dot.
*/
public function __construct($paths, $fileExtension = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

namespace Doctrine\Common\Persistence\Mapping\Driver;

use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\Mapping\MappingException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
class PHPDriver extends FileDriver
{
/**
* {@inheritDoc}
* @var ClassMetadata
*/
protected $metadata;

Expand Down
Loading

0 comments on commit 0009b8f

Please sign in to comment.