Skip to content

Commit

Permalink
Merge pull request #162 from jrjohnson/145-deduplicating-loader
Browse files Browse the repository at this point in the history
Add registerLoaderIfNotExists to AnnotationRegistry
  • Loading branch information
Ocramius committed Dec 6, 2017
2 parents e227dfe + ed4bbc4 commit c7f2050
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/Doctrine/Common/Annotations/AnnotationRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ public static function registerLoader(callable $callable) : void
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
*/
public static function registerUniqueLoader(callable $callable) : void
{
if ( ! in_array($callable, self::$loaders, true) ) {
self::registerLoader($callable);
}
}

/**
* Autoloads an annotation class silently.
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/Doctrine/Tests/Common/Annotations/AnnotationRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,19 @@ public function testResetClearsRegisteredAutoloaderFailures() : void

self::assertSame(2, $failures);
}

/**
* @runInSeparateProcess
*/
public function testRegisterLoaderIfNotExistsOnlyRegisteresSameLoaderOnce() : void
{
$className = 'autoloadedClassThatDoesNotExist';
AnnotationRegistry::reset();
$autoLoader = self::createPartialMock(\stdClass::class, ['__invoke']);
$autoLoader->expects($this->once())->method('__invoke');
AnnotationRegistry::registerUniqueLoader($autoLoader);
AnnotationRegistry::registerUniqueLoader($autoLoader);
AnnotationRegistry::loadAnnotationClass($className);
AnnotationRegistry::loadAnnotationClass($className);
}
}

0 comments on commit c7f2050

Please sign in to comment.