Skip to content

Commit

Permalink
Change inject generator
Browse files Browse the repository at this point in the history
- do not regenerate inject if exists
  • Loading branch information
jfilla committed Oct 15, 2019
1 parent 89ea03b commit 53b668c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 12 deletions.
25 changes: 14 additions & 11 deletions src/DIServiceAnnotation/ExtractServices.php
Expand Up @@ -162,17 +162,20 @@ private function generateInject(string $className, SplFileInfo $file): void
$namespace = $reflectionClass->getNamespaceName();
$name = $reflectionClass->getShortName();
$injectName = sprintf($this->configuration->getInjectMask(), $name);
$this->renderTemplate(
'inject',
dirname($file->getPathname()) . "/$injectName.php",
[
$namespace,
$injectName,
$name,
lcfirst($injectName),
lcfirst($name),
]
);
$outputFile = dirname($file->getPathname()) . "/$injectName.php";
if (!is_file($outputFile)) {
$this->renderTemplate(
'inject',
$outputFile,
[
$namespace,
$injectName,
$name,
lcfirst($injectName),
lcfirst($name),
]
);
}
}

private function generateComponent(string $className, SplFileInfo $file, string $originalName): void
Expand Down
4 changes: 3 additions & 1 deletion tests/DIServiceAnnotationTests/ExtractServicesTest.php
Expand Up @@ -19,6 +19,7 @@ class ExtractServicesTest extends TestCase
public function testRun(): void
{
$filesToCreate = $this->getFilesToCreate();
$filesToCreate[] = $this->path('Services', 'InjectExistingInject.php');
$servicesDir = __DIR__ . '/Services';
$extractServices = new ExtractServices(
(new Configuration($servicesDir, $this->resultNeon(self::DEFAULT_NEON)))
Expand All @@ -32,13 +33,14 @@ public function testRun(): void
'Wavevision\DIServiceAnnotationTests\Services\Nested' => $this->resultNeon(self::NESTED_NEON),
]
)
->setComponentMask('%sComponent')
);
$extractServices->run();
$this->assertSameConfig(self::DEFAULT_NEON);
$this->assertSameConfig(self::NESTED_NEON);
foreach ($filesToCreate as $file) {
$this->assertFileExists($file);
$this->assertSameFileContent($file, Strings::replace($file, '/Services/', '/expected/Services/'));
$this->assertSameFileContent(Strings::replace($file, '/Services/', '/expected/Services/'), $file);
}
}

Expand Down
16 changes: 16 additions & 0 deletions tests/DIServiceAnnotationTests/Services/ExistingInject.php
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace Wavevision\DIServiceAnnotationTests\Services;

use Nette\SmartObject;
use Wavevision\DIServiceAnnotation\DIService;

/**
* @DIService(generateInject=true)
*/
class ExistingInject
{

use SmartObject;

}
@@ -0,0 +1,8 @@
<?php declare (strict_types = 1);

namespace Wavevision\DIServiceAnnotationTests\Services;

trait InjectExistingInject
{

}
@@ -0,0 +1,8 @@
<?php declare (strict_types = 1);

namespace Wavevision\DIServiceAnnotationTests\Services;

trait InjectExistingInject
{

}
2 changes: 2 additions & 0 deletions tests/DIServiceAnnotationTests/expected/default.neon
@@ -1,5 +1,7 @@
# generated file do not modify directly
services:
- factory: Wavevision\DIServiceAnnotationTests\Services\ExistingInject
inject: on
- implement: Wavevision\DIServiceAnnotationTests\Services\InterfaceService
arguments: [%wwwDir%, @name]
inject: on
Expand Down

0 comments on commit 53b668c

Please sign in to comment.