Skip to content

Commit

Permalink
Re-arrange files and fixed code complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
divineniiquaye committed Jun 6, 2020
1 parent 89129eb commit f681ad5
Show file tree
Hide file tree
Showing 39 changed files with 1,575 additions and 515 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"autoload": {
"psr-4": { "BiuradPHP\\Loader\\": "src/" },
"files": ["src/compatibility.php"],
"exclude-from-classmap": [
"/Tests/"
]
Expand All @@ -37,7 +38,7 @@
},
"minimum-stability": "dev",
"suggest": {
"doctrine/annotations": "Support to use annotations loading feature in project",
"biurad/doctrine-annotations-bridge": "Support to use annotations loading feature in project",
"symfony/yaml": "Support loading files in yml or yaml format, if native yaml extension is not avaliable",
"nette/neon": "Support loading files in neon format, since nette/neon is an advanced version of yaml"
}
Expand Down
54 changes: 54 additions & 0 deletions src/AliasLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/*
* This code is under BSD 3-Clause "New" or "Revised" License.
*
* PHP version 7 and above required
*
* @category LoaderManager
*
* @author Divine Niiquaye Ibok <divineibok@gmail.com>
* @copyright 2019 Biurad Group (https://biurad.com/)
* @license https://opensource.org/licenses/BSD-3-Clause License
*
* @link https://www.biurad.com/projects/biurad-loader
* @since Version 0.1
*/

namespace BiuradPHP\Loader;

use BiuradPHP\Loader\Interfaces\AliasTypeInterface;
use BiuradPHP\Loader\Interfaces\LoaderInterface;

/**
* AliasLoader loads namesapces and class aliases from ConfigLocator.
*
* @author Divine Niiquaye <divineibok@gmail.com>
* @license BSD-3-Cluase
*/
class AliasLoader implements LoaderInterface
{
/**
* {@inheritdoc}
*
* @return array|AliasTypeInterface
*/
public function load($resource, string $type = null)
{
return $resource;
}

/**
* {@inheritdoc}
*/
public function supports($resource, string $type = null): bool
{
if ('alias' === $type && is_array($resource)) {
return true;
}

return $resource instanceof AliasTypeInterface;
}
}
86 changes: 63 additions & 23 deletions src/Bridges/LoaderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,27 @@

namespace BiuradPHP\Loader\Bridges;

use BiuradPHP\Loader\Aliases\AliasLoader;
use BiuradPHP\Loader\Annotations\AnnotationLoader;
use BiuradPHP\Loader\Composer\ComposerLoader;
use BiuradPHP\Loader\DataLoader;
use BiuradPHP\Loader\Files\FileLoader;
use BiuradPHP\Loader\Resources\UniformResourceLocator;
use BiuradPHP\DependencyInjection\Concerns\Compiler;
use BiuradPHP\Loader\Locators\AliasLocator;
use BiuradPHP\Loader\AliasLoader;
use BiuradPHP\Loader\ConfigFileLoader;
use BiuradPHP\Loader\DelegatingLoader;
use BiuradPHP\Loader\DirectoryLoader;
use BiuradPHP\Loader\Files\DataLoader;
use BiuradPHP\Loader\GlobFileLoader;
use BiuradPHP\Loader\Interfaces\LoaderExtensionInterface;
use BiuradPHP\Loader\Locators\AnnotationLocator;
use BiuradPHP\Loader\Locators\ComposerLocator;
use BiuradPHP\Loader\Locators\FileLocator;
use BiuradPHP\Loader\Locators\UniformResourceLocator;
use Doctrine\Common\Annotations\Reader;
use Nette, BiuradPHP;
use Nette\DI\Container;
use Nette\DI\Definitions\Statement;
use Nette\Schema\Expect;
use Nette\PhpGenerator\PhpLiteral;
use Nette\PhpGenerator\ClassType as ClassTypeGenerator;

class LoaderExtension extends Nette\DI\CompilerExtension
class LoaderExtension extends Nette\DI\CompilerExtension implements LoaderExtensionInterface
{
/**
* {@inheritDoc}
Expand All @@ -51,7 +60,10 @@ public function getConfigSchema(): Nette\Schema\Schema
})),
'data_path' => Nette\Schema\Expect::string(),
'composer_path' => Nette\Schema\Expect::string()->nullable(),
'aliases' => Nette\Schema\Expect::arrayOf(Expect::string())
'aliases' => Nette\Schema\Expect::arrayOf(Expect::string()),
'loaders' => Nette\Schema\Expect::listOf(Expect::object()->before(function ($value) {
return is_string($value) ? new Statement($value) : $value;
})),
])->castTo('array');
}

Expand All @@ -63,29 +75,56 @@ public function loadConfiguration()
$builder = $this->getContainerBuilder();

$builder->addDefinition($this->prefix('file'))
->setFactory(FileLoader::class)
->setFactory(FileLocator::class)
->setArguments([$this->config['locators']['paths'], $this->config['locators']['excludes']])
;

$builder->addDefinition($this->prefix('annotation'))
->setFactory(AnnotationLoader::class);
$builder->addDefinition($this->prefix('loader'))
->setFactory(DelegatingLoader::class)
->setArguments([
array_merge([
new Statement(AliasLoader::class),
new Statement(ConfigFileLoader::class),
new Statement(DirectoryLoader::class),
new Statement(GlobFileLoader::class),
], $this->config['loaders'])
]);

if (class_exists(Reader::class)) {
$builder->addDefinition($this->prefix('annotation'))
->setFactory(AnnotationLocator::class);
}

$builder->addDefinition($this->prefix('data'))
->setFactory(DataLoader::class)
->setArguments([$this->config['data_path']])
;

$builder->addDefinition($this->prefix('composer'))
->setFactory(ComposerLoader::class)
->setFactory(ComposerLocator::class)
->setArguments([$this->config['composer_path']])
;

$builder->addDefinition($this->prefix('locator'))
$builder->addDefinition($this->prefix('alias'))
->setFactory(AliasLocator::class, [$this->config['aliases']]);

$locator = $builder->addDefinition($this->prefix('locator'))
->setFactory(UniformResourceLocator::class)
->setArguments([$builder->parameters['path']['ROOT']])
->addSetup(
'foreach (? as $scheme => [$path, $lookup]) { ?->addPath($scheme, $path, $lookup); }', [$this->config['resources'], '@self']
);
->setArguments([$builder->parameters['path']['ROOT']]);

foreach ($this->config['resources'] as $scheme => [$path, $lookup]) {
$locator->addSetup('addPath', [$scheme, $path, $lookup]);
}

$builder->addAlias('locator', $this->prefix('locator'));
}

/**
* {@inheritdoc}
*/
public function addCompilerPasses(Compiler &$compiler): void
{
$compiler->addPass(new LoaderPassCompiler());
}

/**
Expand All @@ -95,13 +134,14 @@ public function afterCompile(ClassTypeGenerator $class): void
{
$init = $this->initialization ?? $class->getMethod('initialize');

if (empty($this->config['aliases'])) {
return;
}

// For Runtime.
$init->addBody(
"// The loader class aliases.\n" .
'$classAliases = new ?(?);' . "\n" .
'$classAliases->register(); // Class alias registered.' .
"\n\n",
[new PhpLiteral(AliasLoader::class), $this->config['aliases']]
'$this->?()->register(); // Class alias registered.'."\n\n",
[Container::getMethodName($this->prefix('alias'))]
);
}
}
43 changes: 43 additions & 0 deletions src/Bridges/LoaderPassCompiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/*
* This code is under BSD 3-Clause "New" or "Revised" License.
*
* PHP version 7 and above required
*
* @category LoaderManager
*
* @author Divine Niiquaye Ibok <divineibok@gmail.com>
* @copyright 2019 Biurad Group (https://biurad.com/)
* @license https://opensource.org/licenses/BSD-3-Clause License
*
* @link https://www.biurad.com/projects/biurad-loader
* @since Version 0.1
*/

namespace BiuradPHP\Loader\Bridges;

use BiuradPHP\DependencyInjection\Concerns\ContainerBuilder;
use BiuradPHP\DependencyInjection\Interfaces\CompilerPassInterface;
use BiuradPHP\Loader\DelegatingLoader;
use BiuradPHP\Loader\Interfaces\LoaderInterface;

class LoaderPassCompiler implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$loader = $container->getDefinitionByType(DelegatingLoader::class);

foreach ($container->findByType(LoaderInterface::class) as $name => $definition) {
$newStatement = $definition->getFactory();
$container->removeDefinition($name);

$loader->addSetup('addLoader', [$newStatement]);
}
}
}
59 changes: 59 additions & 0 deletions src/ConfigFileLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/*
* This code is under BSD 3-Clause "New" or "Revised" License.
*
* PHP version 7 and above required
*
* @category LoaderManager
*
* @author Divine Niiquaye Ibok <divineibok@gmail.com>
* @copyright 2019 Biurad Group (https://biurad.com/)
* @license https://opensource.org/licenses/BSD-3-Clause License
*
* @link https://www.biurad.com/projects/biurad-loader
* @since Version 0.1
*/

namespace BiuradPHP\Loader;

use BiuradPHP\Loader\Interfaces\LoaderInterface;
use BiuradPHP\Loader\Locators\ConfigLocator;

/**
* ConfigFileLoader loads files from ConfigLocator.
*
* @author Divine Niiquaye Ibok <divineibok@gmail.com>
*/
class ConfigFileLoader implements LoaderInterface
{
private $loader;

/**
* @param ConfigLocator $Locator
*/
public function __construct(ConfigLocator $Locator = null)
{
$this->loader = $Locator ?: new ConfigLocator();
}

/**
* {@inheritdoc}
*
* @return array
*/
public function load($resource, string $type = null): array
{
return $this->loader->loadFile($resource);
}

/**
* {@inheritdoc}
*/
public function supports($resource, string $type = null): bool
{
return 'config' === $type && (file_exists($resource) && is_file($resource));
}
}
81 changes: 81 additions & 0 deletions src/DelegatingLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

/*
* This code is under BSD 3-Clause "New" or "Revised" License.
*
* PHP version 7 and above required
*
* @category LoaderManager
*
* @author Divine Niiquaye Ibok <divineibok@gmail.com>
* @copyright 2019 Biurad Group (https://biurad.com/)
* @license https://opensource.org/licenses/BSD-3-Clause License
*
* @link https://www.biurad.com/projects/biurad-loader
* @since Version 0.1
*/

namespace BiuradPHP\Loader;

use BiuradPHP\Loader\Interfaces\LoaderInterface;

/**
* DelegatingLoader delegates loading to other loaders using a loader resolver.
*
* This loader acts as an array of LoaderInterface objects - each having
* a chance to load a given resource.
*
* @author Divine Niiquaye Ibok <divineibok@gmail.com>
*/
class DelegatingLoader implements LoaderInterface
{
/**
* @var LoaderInterface[] An array of LoaderInterface objects
*/
private $loaders = [];

/**
* @param LoaderInterface[] $loaders An array of loaders
*/
public function __construct(array $loaders = [])
{
foreach ($loaders as $loader) {
$this->addLoader($loader);
}
}

/**
* {@inheritdoc}
*/
public function load($resource, string $type = null)
{
foreach ($this->loaders as $loader) {
if ($loader->supports($resource, $type)) {
return $loader->load($resource, $type);
}
}

return false;
}

/**
* {@inheritdoc}
*/
public function supports($resource, string $type = null): bool
{
return false !== $this->load($resource, $type);
}

/**
* Add a LoaderInterface instance to $this class.
*
* @param LoaderInterface $loader
* @return void
*/
public function addLoader(LoaderInterface $loader): void
{
$this->loaders[] = $loader;
}
}
Loading

0 comments on commit f681ad5

Please sign in to comment.