mirror of
https://github.com/marcostoll/FF-Factories.git
synced 2025-01-17 06:38:17 +01:00
[FEATURE] add base ns class locator
This commit is contained in:
parent
1f34fba0ee
commit
f2d5348833
80
src/ClassLocators/BaseNamespaceClassLocator.php
Normal file
80
src/ClassLocators/BaseNamespaceClassLocator.php
Normal 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);
|
||||
}
|
||||
}
|
83
tests/ClassLocators/BaseNamespaceClassLocatorTest.php
Normal file
83
tests/ClassLocators/BaseNamespaceClassLocatorTest.php
Normal 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
|
||||
{
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user