Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Implement generator factory
Browse files Browse the repository at this point in the history
  • Loading branch information
tux-rampage committed Dec 8, 2017
1 parent 429002c commit 8153e4d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/CodeGenerator/GeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,12 @@ public function setOutputDirectory(string $dir, ?int $mode = null) : self

return $this;
}

/**
* @return string
*/
public function getOutputDirectory(): ?string
{
return $this->outputDirectory;
}
}
8 changes: 8 additions & 0 deletions src/CodeGenerator/InjectorGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ private function generateAutoload()
$this->autoloadGenerator->generate($classmap);
}

/**
* Returns the namespace this generator uses
*/
public function getNamespace(): string
{
return $this->namespace;
}

/**
* Generate the injector
*
Expand Down
3 changes: 2 additions & 1 deletion src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function getDependencyConfig() : array
return [
'factories' => [
InjectorInterface::class => Container\InjectorFactory::class,
ConfigInterface::class => Container\ConfigFactory::class
ConfigInterface::class => Container\ConfigFactory::class,
CodeGenerator\InjectorGenerator::class => Container\GeneratorFactory::class,
],
'abstract_factories' => [
Container\ServiceManager\AutowireFactory::class
Expand Down
28 changes: 27 additions & 1 deletion src/Container/GeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,38 @@
namespace Zend\Di\Container;

use Psr\Container\ContainerInterface;
use Zend\Di\CodeGenerator\InjectorGenerator;
use Zend\Di\ConfigInterface;
use Zend\Di\Definition\RuntimeDefinition;
use Zend\Di\Resolver\DependencyResolver;

class GeneratorFactory
{
private function getConfig(ContainerInterface $container)
{
if ($container->has(ConfigInterface::class)) {
return $container->get(ConfigInterface::class);
}

return (new ConfigFactory())->create($container);
}

public function create(ContainerInterface $container)
{
// TODO: Implement the factory
$config = $container->has('config') ? $container->get('config') : [];
$diConfig = $this->getConfig($container);
$aotConfig = $config['dependencies']['auto']['aot'] ?? [];
$resolver = new DependencyResolver(new RuntimeDefinition(), $diConfig);
$namespace = $aotConfig['namespace'] ?? null;

$resolver->setContainer($container);
$generator = new InjectorGenerator($diConfig, $resolver, $namespace);

if (isset($aotConfig['directory'])) {
$generator->setOutputDirectory($aotConfig['directory']);
}

return $generator;
}

public function __invoke(ContainerInterface $container)
Expand Down

0 comments on commit 8153e4d

Please sign in to comment.