Skip to content

Commit

Permalink
Add more options and bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
rozsival committed Oct 16, 2019
1 parent 53b668c commit a496216
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 12 deletions.
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions src/DIServiceAnnotation/Bootstrap.php
@@ -0,0 +1,35 @@
<?php declare (strict_types = 1);

namespace Wavevision\DIServiceAnnotation;

use Nette\StaticClass;
use Wavevision\Utils\Path;

class Bootstrap
{

use StaticClass;

/**
* @var string
*/
private static $rootDir;

/**
* @param string $rootDir
* @param array<string, string> $mappings
*/
public static function boot(string $rootDir, array $mappings): void
{
self::$rootDir = $rootDir;
foreach ($mappings as $source => $output) {
(new ExtractServices(new Configuration(self::fromRoot($source), self::fromRoot($output))))->run();
}
}

private static function fromRoot(string $path): string
{
return Path::join(self::$rootDir, $path);
}

}
12 changes: 11 additions & 1 deletion src/DIServiceAnnotation/DIService.php
Expand Up @@ -10,10 +10,20 @@ final class DIService
{

/**
* @var array<string>
* @var bool
*/
public $inject = true;

/**
* @var string[]
*/
public $params = [];

/**
* @var string[]
*/
public $tags = [];

/**
* @var bool
*/
Expand Down
42 changes: 36 additions & 6 deletions src/DIServiceAnnotation/ExtractServices.php
Expand Up @@ -8,6 +8,7 @@
use Nette\Utils\Strings;
use ReflectionClass;
use SplFileInfo;
use Wavevision\Utils\Arrays;
use Wavevision\Utils\Tokenizer\Tokenizer;

class ExtractServices
Expand Down Expand Up @@ -72,7 +73,6 @@ private function saveFile(array $services, string $outputFile): void
$annotation = $service->getAnnotation();
$tokenizeResult = $service->getTokenizeResult();
$className = $tokenizeResult->getFullyQualifiedName();
$params = $annotation->params;
$tokenId = $tokenizeResult->getToken();
$file = $service->getFile();
$generateFactory = $annotation->generateFactory || $annotation->generateComponent;
Expand All @@ -90,11 +90,7 @@ private function saveFile(array $services, string $outputFile): void
if ($annotation->generateComponent) {
$this->generateComponent($className, $file, $orginalName);
}
$lines[] = sprintf("- %s: %s", self::TOKEN_TO_FACTORY[$tokenId], $className);
if (count($params) > 0) {
$lines[] = " arguments: [" . implode(', ', $params) . ']';
}
$lines[] = " inject: on";
$lines = $this->generateConfig($annotation, $className, $tokenId, $lines);
}
FileSystem::write(
$outputFile,
Expand Down Expand Up @@ -133,6 +129,40 @@ private function getAnnotation(string $className): ?DIService
return null;
}

/**
* @param DIService $annotation
* @param string $className
* @param int $token
* @param string[] $lines
* @return string[]
*/
private function generateConfig(DIService $annotation, string $className, int $token, array $lines): array
{
$params = $annotation->params;
$tags = $annotation->tags;
$lines[] = sprintf("- %s: %s", self::TOKEN_TO_FACTORY[$token], $className);
if (!Arrays::isEmpty($params)) {
$lines[] = $this->generateAttributes('arguments', $params);
}
if (!Arrays::isEmpty($tags)) {
$lines[] = $this->generateAttributes('tags', $tags);
}
if ($annotation->inject) {
$lines[] = " inject: on";
}
return $lines;
}

/**
* @param string $name
* @param string[] $attributes
* @return string
*/
private function generateAttributes(string $name, array $attributes): string
{
return " $name: [" . implode(', ', $attributes) . ']';
}

private function generateFactory(string $className, SplFileInfo $file): string
{
$reflectionClass = $this->getReflection($className);
Expand Down

0 comments on commit a496216

Please sign in to comment.