Skip to content

Commit

Permalink
Merge 83d77ca into cd20d42
Browse files Browse the repository at this point in the history
  • Loading branch information
gennadigennadigennadi committed Oct 7, 2020
2 parents cd20d42 + 83d77ca commit 351fa82
Show file tree
Hide file tree
Showing 88 changed files with 864 additions and 847 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/laminas-mkdoc-theme/
/phpunit.xml
/vendor/
/.phpcs-cache
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"require-dev": {
"container-interop/container-interop": "^1.2.0",
"laminas/laminas-coding-standard": "~1.0.0",
"laminas/laminas-coding-standard": "^2",
"laminas/laminas-servicemanager": "^3.4",
"mikey179/vfsstream": "^1.6.7",
"phpstan/phpstan": "^0.12",
Expand Down
14 changes: 12 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<?xml version="1.0"?>
<ruleset name="Laminas Coding Standard">
<rule ref="./vendor/laminas/laminas-coding-standard/ruleset.xml"/>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">

<arg name="basepath" value="."/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>

<!-- Show progress -->
<arg value="p"/>


<!--
Don't make it bail at the file level deprecation of AbstactInjection (Intented side effect).
Expand All @@ -18,4 +27,5 @@
<file>test</file>

<exclude-pattern><![CDATA[*/test/_files/*]]></exclude-pattern>
<rule ref="LaminasCodingStandard"/>
</ruleset>
35 changes: 14 additions & 21 deletions src/CodeGenerator/AbstractInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,24 @@
*/
abstract class AbstractInjector implements InjectorInterface
{
/**
* @var string[]|FactoryInterface[]
*/
/** @var string[]|FactoryInterface[] */
protected $factories = [];

/**
* @var FactoryInterface[]
*/
/** @var FactoryInterface[] */
private $factoryInstances = [];

/**
* @var ContainerInterface
*/
/** @var ContainerInterface */
private $container;

/**
* @var InjectorInterface
*/
/** @var InjectorInterface */
private $injector;

/**
* {@inheritDoc}
*/
public function __construct(InjectorInterface $injector, ContainerInterface $container = null)
public function __construct(InjectorInterface $injector, ?ContainerInterface $container = null)
{
$this->injector = $injector;
$this->injector = $injector;
$this->container = $container ?: new DefaultContainer($this);

$this->loadFactoryList();
Expand All @@ -53,32 +45,33 @@ public function __construct(InjectorInterface $injector, ContainerInterface $con
/**
* Init factory list
*/
abstract protected function loadFactoryList() : void;
abstract protected function loadFactoryList(): void;

private function setFactory(string $type, FactoryInterface $factory) : void
private function setFactory(string $type, FactoryInterface $factory): void
{
$this->factoryInstances[$type] = $factory;
}

private function getFactory($type) : FactoryInterface
private function getFactory(string $type): FactoryInterface
{
if (isset($this->factoryInstances[$type])) {
return $this->factoryInstances[$type];
}

$factoryClass = $this->factories[$type];
$factory = ($factoryClass instanceof FactoryInterface) ? $factoryClass : new $factoryClass();
$factory = $factoryClass instanceof FactoryInterface ? $factoryClass : new $factoryClass();

$this->setFactory($type, $factory);

return $factory;
}

public function canCreate(string $name) : bool
public function canCreate(string $name): bool
{
return (isset($this->factories[$name]) || $this->injector->canCreate($name));
return isset($this->factories[$name]) || $this->injector->canCreate($name);
}

/** @return mixed */
public function create(string $name, array $options = [])
{
if (isset($this->factories[$name])) {
Expand Down
21 changes: 10 additions & 11 deletions src/CodeGenerator/AutoloadGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use function array_keys;
use function array_map;
use function assert;
use function file_get_contents;
use function implode;
use function is_string;
Expand All @@ -29,19 +30,17 @@ class AutoloadGenerator
use GeneratorTrait;

private const CLASS_TEMPLATE = __DIR__ . '/../../templates/autoloader-class.template';
private const FILE_TEMPLATE = __DIR__ . '/../../templates/autoloader-file.template';
private const FILE_TEMPLATE = __DIR__ . '/../../templates/autoloader-file.template';

/**
* @var string
*/
/** @var string */
private $namespace;

public function __construct(string $namespace)
{
$this->namespace = $namespace;
}

private function writeFile(string $filename, string $code) : void
private function writeFile(string $filename, string $code): void
{
try {
$file = new SplFileObject($filename, 'w');
Expand All @@ -51,7 +50,7 @@ private function writeFile(string $filename, string $code) : void
}
}

private function buildFromTemplate(string $templateFile, string $outputFile, array $replacements) : void
private function buildFromTemplate(string $templateFile, string $outputFile, array $replacements): void
{
$template = file_get_contents($templateFile);

Expand All @@ -66,7 +65,7 @@ private function buildFromTemplate(string $templateFile, string $outputFile, arr
);
}

private function generateClassmapCode(array &$classmap) : string
private function generateClassmapCode(array &$classmap): string
{
$lines = array_map(
function (string $class, string $file): string {
Expand All @@ -84,15 +83,15 @@ function (string $class, string $file): string {
return implode($indentation, $lines);
}

private function generateAutoloaderClass(array &$classmap) : void
private function generateAutoloaderClass(array &$classmap): void
{
$this->buildFromTemplate(self::CLASS_TEMPLATE, 'Autoloader.php', [
'%namespace%' => $this->namespace ? sprintf("namespace %s;\n", $this->namespace) : '',
'%classmap%' => $this->generateClassmapCode($classmap),
'%classmap%' => $this->generateClassmapCode($classmap),
]);
}

private function generateAutoloadFile() : void
private function generateAutoloadFile(): void
{
$this->buildFromTemplate(self::FILE_TEMPLATE, 'autoload.php', [
'%namespace%' => $this->namespace ? sprintf("namespace %s;\n", $this->namespace) : '',
Expand All @@ -102,7 +101,7 @@ private function generateAutoloadFile() : void
/**
* @param string[] $classmap
*/
public function generate(array &$classmap) : void
public function generate(array &$classmap): void
{
$this->ensureOutputDirectory();
$this->generateAutoloaderClass($classmap);
Expand Down
78 changes: 36 additions & 42 deletions src/CodeGenerator/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@
use Laminas\Di\Resolver\TypeInjection;
use SplFileObject;

use function assert;
use function dirname;
use function file_get_contents;
use function implode;
use function is_string;
use function preg_replace;
use function sprintf;
use function str_repeat;
use function str_replace;
use function strrpos;
use function strtr;
use function substr;
use function var_export;

/**
* Generates factory classes
Expand All @@ -30,9 +38,9 @@ class FactoryGenerator
{
use GeneratorTrait;

private const INDENTATION_SPACES = 4;
private const TEMPLATE_FILE = __DIR__ . '/../../templates/factory.template';
private const PARAMETERS_TEMPLATE = <<< '__CODE__'
private const INDENTATION_SPACES = 4;
private const TEMPLATE_FILE = __DIR__ . '/../../templates/factory.template';
private const PARAMETERS_TEMPLATE = <<<'__CODE__'
$args = empty($options)
? [
Expand All @@ -44,67 +52,56 @@ class FactoryGenerator
__CODE__;

/**
* @var string
*/
/** @var string */
private $namespace;

/**
* @var DependencyResolverInterface
*/
/** @var DependencyResolverInterface */
private $resolver;

/**
* @var ConfigInterface
*/
/** @var ConfigInterface */
private $config;

/**
* @var array
*/
/** @var array */
private $classmap = [];

/**
* @param DependencyResolverInterface $resolver
*/
public function __construct(
ConfigInterface $config,
DependencyResolverInterface $resolver,
?string $namespace = null
) {
$this->resolver = $resolver;
$this->config = $config;
$this->resolver = $resolver;
$this->config = $config;
$this->namespace = $namespace ?: 'LaminasDiGenerated';
}

protected function buildClassName(string $name) : string
protected function buildClassName(string $name): string
{
return preg_replace('~[^a-z0-9\\\\]+~i', '_', $name) . 'Factory';
}

protected function buildFileName(string $name) : string
protected function buildFileName(string $name): string
{
return str_replace('\\', '/', $this->buildClassName($name)) . '.php';
}

/**
* @return string[] The resulting parts as [$namspace, $unqualifiedClassName]
*/
private function splitFullyQualifiedClassName(string $class) : array
private function splitFullyQualifiedClassName(string $class): array
{
$pos = strrpos($class, '\\');

if ($pos === false) {
return ['', $class];
}

$namespace = substr($class, 0, $pos);
$namespace = substr($class, 0, $pos);
$unqualifiedClassName = substr($class, $pos + 1);

return [$namespace, $unqualifiedClassName];
}

private function getClassName(string $type) : string
private function getClassName(string $type): string
{
if ($this->config->isAlias($type)) {
return $this->config->getClassForAlias($type) ?? $type;
Expand All @@ -116,7 +113,7 @@ private function getClassName(string $type) : string
/**
* @param InjectionInterface[] $injections
*/
private function canGenerateForParameters(iterable $injections) : bool
private function canGenerateForParameters(iterable $injections): bool
{
foreach ($injections as $injection) {
if (! $injection->isExportable()) {
Expand All @@ -132,9 +129,9 @@ private function canGenerateForParameters(iterable $injections) : bool
*
* @param InjectionInterface[] $injections
*/
private function buildParametersCode(iterable $injections) : ?string
private function buildParametersCode(iterable $injections): ?string
{
$withOptions = [];
$withOptions = [];
$withoutOptions = [];

foreach ($injections as $name => $injection) {
Expand Down Expand Up @@ -172,11 +169,11 @@ private function buildParametersCode(iterable $injections) : ?string
}

/**
* @throws RuntimeException When generating the factory failed
* @throws RuntimeException When generating the factory failed.
*/
public function generate(string $class): string
{
$className = $this->getClassName($class);
$className = $this->getClassName($class);
$injections = $this->resolver->resolveParameters($className);

if (! $this->canGenerateForParameters($injections)) {
Expand All @@ -187,10 +184,10 @@ public function generate(string $class): string
));
}

$paramsCode = $this->buildParametersCode($injections);
$absoluteClassName = '\\' . $className;
$factoryClassName = $this->namespace . '\\' . $this->buildClassName($class);
list($namespace, $unqualifiedFactoryClassName) = $this->splitFullyQualifiedClassName($factoryClassName);
$paramsCode = $this->buildParametersCode($injections);
$absoluteClassName = '\\' . $className;
$factoryClassName = $this->namespace . '\\' . $this->buildClassName($class);
[$namespace, $unqualifiedFactoryClassName] = $this->splitFullyQualifiedClassName($factoryClassName);

$filename = $this->buildFileName($class);
$filepath = $this->outputDirectory . '/' . $filename;
Expand All @@ -201,12 +198,12 @@ public function generate(string $class): string
$code = strtr(
$template,
[
'%class%' => $absoluteClassName,
'%namespace%' => $namespace ? "namespace $namespace;\n" : '',
'%factory_class%' => $unqualifiedFactoryClassName,
'%class%' => $absoluteClassName,
'%namespace%' => $namespace ? "namespace $namespace;\n" : '',
'%factory_class%' => $unqualifiedFactoryClassName,
'%options_to_args_code%' => $paramsCode,
'%use_array_key_exists%' => $paramsCode ? "\nuse function array_key_exists;" : '',
'%args%' => $paramsCode ? '...$args' : '',
'%args%' => $paramsCode ? '...$args' : '',
]
);

Expand All @@ -219,10 +216,7 @@ public function generate(string $class): string
return $factoryClassName;
}

/**
* @return array
*/
public function getClassmap() : array
public function getClassmap(): array
{
return $this->classmap;
}
Expand Down
1 change: 0 additions & 1 deletion src/CodeGenerator/FactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ interface FactoryInterface
/**
* Create an instance
*
* @param array $options
* @return object
*/
public function create(ContainerInterface $container, array $options);
Expand Down
Loading

0 comments on commit 351fa82

Please sign in to comment.