-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e158d38
commit 27e097e
Showing
20 changed files
with
258 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace WebComplete\core\package; | ||
|
||
abstract class AbstractPackage | ||
{ | ||
|
||
/** | ||
* @param $definitions | ||
* | ||
* @return void | ||
*/ | ||
abstract public function registerDependencies(array &$definitions); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace WebComplete\core\package; | ||
|
||
class PackageException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace WebComplete\core\package; | ||
|
||
use WebComplete\core\utils\helpers\ClassHelper; | ||
|
||
class PackageManager | ||
{ | ||
|
||
const FILENAME = 'Package.php'; | ||
|
||
/** | ||
* @var ClassHelper | ||
*/ | ||
protected $classHelper; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $registered = []; | ||
|
||
/** | ||
* @param ClassHelper $classHelper | ||
*/ | ||
public function __construct(ClassHelper $classHelper) | ||
{ | ||
$this->classHelper = $classHelper; | ||
} | ||
|
||
/** | ||
* @param $packageClassName | ||
* | ||
* @return AbstractPackage | ||
* @throws PackageException | ||
*/ | ||
public function getPackage($packageClassName): AbstractPackage | ||
{ | ||
if (!isset($this->registered[$packageClassName])) { | ||
throw new PackageException($packageClassName . ' is not registered'); | ||
} | ||
|
||
return $this->registered[$packageClassName]; | ||
} | ||
|
||
/** | ||
* @param $packageClassName | ||
* @param array $definitions | ||
* | ||
* @throws PackageException | ||
*/ | ||
public function register($packageClassName, array &$definitions) | ||
{ | ||
if (!isset($this->registered[$packageClassName])) { | ||
$package = new $packageClassName; | ||
if (!$package instanceof AbstractPackage) { | ||
throw new PackageException($packageClassName . ' is not an instance of ' . AbstractPackage::class); | ||
} | ||
|
||
$package->registerDependencies($definitions); | ||
$this->registered[$packageClassName] = $package; | ||
} | ||
} | ||
|
||
/** | ||
* @param string $directory | ||
* @param array $definitions | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function registerAll(string $directory, array &$definitions) | ||
{ | ||
$classMap = $this->findAll($directory); | ||
foreach ($classMap as $class) { | ||
$this->register($class, $definitions); | ||
} | ||
} | ||
|
||
/** | ||
* @param $directory | ||
* | ||
* @return array [file => class] | ||
* @throws \Exception | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function findAll($directory): array | ||
{ | ||
return $this->classHelper->getClassMap($directory, self::FILENAME); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace WebComplete\core\utils\helpers; | ||
|
||
use hanneskod\classtools\Iterator\ClassIterator; | ||
use Symfony\Component\Finder\Finder; | ||
|
||
class ClassHelper | ||
{ | ||
|
||
/** | ||
* @param string $directory | ||
* @param string $namePattern | ||
* | ||
* @return array | ||
* @throws \Exception | ||
*/ | ||
public function getClassMap(string $directory, string $namePattern = '*.php'): array | ||
{ | ||
$result = []; | ||
$finder = new Finder(); | ||
$finder->in($directory)->files()->name($namePattern); | ||
$iterator = new ClassIterator($finder); | ||
foreach ($iterator->getClassMap() as $class => $splFileInfo) { | ||
$result[$splFileInfo->getRealPath()] = $class; | ||
} | ||
return $result; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace WebComplete\core\package; | ||
|
||
use Mvkasatkin\mocker\Mocker; | ||
use WebComplete\core\utils\helpers\ClassHelper; | ||
|
||
class PackageManagerTest extends \CoreTestCase | ||
{ | ||
|
||
public function testInstance() | ||
{ | ||
$classHelper = new ClassHelper(); | ||
$pm = new PackageManager($classHelper); | ||
$this->assertInstanceOf(PackageManager::class, $pm); | ||
} | ||
|
||
public function testGetPackage() | ||
{ | ||
$p = Mocker::create(AbstractPackage::class); | ||
$classHelper = new ClassHelper(); | ||
$pm = new PackageManager($classHelper); | ||
$def = []; | ||
$pm->register(get_class($p), $def); | ||
$this->assertInstanceOf(get_class($p), $pm->getPackage(get_class($p))); | ||
} | ||
|
||
public function testGetPackageException() | ||
{ | ||
$this->expectException(PackageException::class); | ||
$classHelper = new ClassHelper(); | ||
$pm = new PackageManager($classHelper); | ||
$pm->getPackage('asd'); | ||
} | ||
|
||
public function testRegisterException() | ||
{ | ||
$this->expectException(PackageException::class); | ||
$p = Mocker::create(\stdClass::class); | ||
$classHelper = new ClassHelper(); | ||
$pm = new PackageManager($classHelper); | ||
$def = []; | ||
$pm->register(get_class($p), $def); | ||
} | ||
|
||
public function testRegisterAll() | ||
{ | ||
$dir = 'SomeDir'; | ||
$def = []; | ||
/** @var ClassHelper $classHelper */ | ||
$classHelper = Mocker::create(ClassHelper::class, [ | ||
Mocker::method('getClassMap', 1, [$dir, PackageManager::FILENAME])->returns([ | ||
'SomeDir/SomeFile' => 'SomeClass' | ||
]) | ||
]); | ||
/** @var PackageManager $pm */ | ||
$pm = Mocker::create(PackageManager::class, [ | ||
Mocker::method('register', 1, ['SomeClass', $def]) | ||
], [$classHelper]); | ||
$pm->registerAll($dir, $def); | ||
} | ||
|
||
public function testFindAll() | ||
{ | ||
$dir = 'SomeDir'; | ||
/** @var ClassHelper $classHelper */ | ||
$classHelper = Mocker::create(ClassHelper::class, [ | ||
Mocker::method('getClassMap', 1, [$dir, PackageManager::FILENAME])->returns([]) | ||
]); | ||
$pm = new PackageManager($classHelper); | ||
$pm->findAll($dir); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace WebComplete\core\utils\helpers; | ||
|
||
use ClassHelperFixNS1\ClassHelperFix1; | ||
use ClassHelperFixNS2\ClassHelperFix2; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ClassHelperTest extends TestCase | ||
{ | ||
|
||
public function testGetClassMap() | ||
{ | ||
$classHelper = new ClassHelper(); | ||
$expected = [ | ||
'class_helper/ClassHelperFix1.php' => ClassHelperFix1::class, | ||
'class_helper/sub/ClassHelperFix2.php' => ClassHelperFix2::class, | ||
]; | ||
$map = $classHelper->getClassMap(__DIR__ . '/../../../fixtures/class_helper'); | ||
$this->assertCount(2, $map); | ||
$this->assertEquals(array_values($expected), array_values($map)); | ||
$expected = array_keys($expected); | ||
$map = array_keys($map); | ||
$this->assertContains($expected[0], $map[0]); | ||
$this->assertContains($expected[1], $map[1]); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace ClassHelperFixNS1; | ||
|
||
class ClassHelperFix1 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace ClassHelperFixNS2; | ||
|
||
class ClassHelperFix2 | ||
{ | ||
} |