[FEATURE] add base ns class locator

This commit is contained in:
Marco Stoll 2019-06-21 09:51:04 +02:00
parent 1f34fba0ee
commit f2d5348833
2 changed files with 163 additions and 0 deletions

View File

@ -0,0 +1,80 @@
<?php
/**
* Definition of BaseNamespaceClassLocator
*
* @author Marco Stoll <marco@fast-forward-encoding.de>
* @copyright 2019-forever Marco Stoll
* @filesource
*/
declare(strict_types=1);
namespace FF\Factories\ClassLocators;
use FF\Utils\ClassUtils;
/**
* Class BaseNamespaceClassLocator
*
* Expects a class name relative to any of the registered namespaces followed bei the common suffix as class identifier.
*
* Example 1:
* Registered namespace: 'MyProject'
* $commonSuffix: 'Events'
* valid $classIdentifiers:
* - 'Error' => finds MyProject\Events\Error
*
* Example 2:
* Registered namespace: 'FF'
* $prefix: 'Events'
* valid $classIdentifiers:
* - 'Runtime\Error' => finds FF\Events\Runtime\Error
*
* "Going up" in the namespace path by using '..\\' will not work.
*
* @package FF\Factories\ClassLocators
*/
class BaseNamespaceClassLocator extends NamespaceClassLocator
{
/**
* @var string
*/
protected $commonSuffix;
/**
* @param string $commonSuffix
* @param string[] $namespaces
*/
public function __construct(string $commonSuffix, string ...$namespaces)
{
parent::__construct(...$namespaces);
$this->commonSuffix = $commonSuffix;
}
/**
* @return string
*/
public function getCommonSuffix(): string
{
return $this->commonSuffix;
}
/**
* @param string $commonSuffix
* @return $this
*/
public function setCommonSuffix(string $commonSuffix)
{
$this->commonSuffix = ClassUtils::normalizeNamespace($commonSuffix);
return $this;
}
/**
* @param string $ns
* @param string $classIdentifier
* @return string
*/
protected function buildFqClassName(string $ns, string $classIdentifier): string
{
return parent::buildFqClassName($ns . '\\' . $this->commonSuffix, $classIdentifier);
}
}

View File

@ -0,0 +1,83 @@
<?php
/**
* Definition of BaseNamespaceClassLocatorTest
*
* @author Marco Stoll <marco@fast-forward-encoding.de>
* @copyright 2019-forever Marco Stoll
* @filesource
*/
declare(strict_types=1);
namespace FF\Tests\Factories\ClassLocators
{
use FF\Factories\ClassLocators\BaseNamespaceClassLocator;
use FF\Tests\Factories\ClassLocators\Sub\MyBaseSubObject;
use PHPUnit\Framework\TestCase;
/**
* Test BaseNamespaceClassLocatorTest
*
* @package FF\Tests
*/
class BaseNamespaceClassLocatorTest extends TestCase
{
/**
* @var BaseNamespaceClassLocator
*/
protected $uut;
/**
* {@inheritdoc}
*/
protected function setUp(): void
{
$this->uut = new BaseNamespaceClassLocator('ClassLocators', 'FF\Tests\Factories');
}
/**
* Tests the namesake method/feature
*/
public function testLocateClass()
{
$result = $this->uut->locateClass('MyBaseObject');
$this->assertEquals(MyBaseObject::class, $result);
$result = $this->uut->locateClass('Sub\MyBaseSubObject');
$this->assertEquals(MyBaseSubObject::class, $result);
}
/**
* Tests the namesake method/feature
*/
public function testLocateClassNotFound()
{
$result = $this->uut->locateClass('Foo');
$this->assertNull($result);
}
/**
* Tests the namesake method/feature
*/
public function testLocateClassNotFoundUpFailure()
{
$result = $this->uut->locateClass('Sub\\..\\MyBaseObject');
$this->assertNull($result);
}
}
class MyBaseObject
{
}
}
namespace FF\Tests\Factories\ClassLocators\Sub {
use FF\Tests\Factories\ClassLocators\MyBaseObject;
class MyBaseSubObject extends MyBaseObject
{
}
}