Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to ignore fatal erorrs in scanned services
* add caching option * refactor * update readme
- Loading branch information
Showing
16 changed files
with
432 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
use Wavevision\DIServiceAnnotation\ClassValidator; | ||
|
||
require_once $argv[1]; | ||
exit((new ClassValidator())->validate($argv[2])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Wavevision\DIServiceAnnotation; | ||
|
||
use Nette\SmartObject; | ||
use Nette\Utils\FileSystem; | ||
use Wavevision\Utils\Path; | ||
|
||
class Cache | ||
{ | ||
|
||
public const FILE = '.di-service-annotation.cache'; | ||
|
||
use SmartObject; | ||
|
||
private bool $enabled; | ||
|
||
/** | ||
* @var array<mixed> | ||
*/ | ||
private array $cache; | ||
|
||
private string $cacheFile; | ||
|
||
public function __construct(Configuration $configuration) | ||
{ | ||
$tempDir = $configuration->getTempDir(); | ||
$this->enabled = $tempDir !== null; | ||
if ($this->enabled) { | ||
$this->cacheFile = Path::join($tempDir, self::FILE); | ||
if (is_file($this->cacheFile)) { | ||
$this->cache = unserialize(FileSystem::read($this->cacheFile)); | ||
} else { | ||
$this->cache = []; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function get(string $key) | ||
{ | ||
if ($this->enabled) { | ||
if (isset($this->cache[$key])) { | ||
return $this->cache[$key]; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* @param array<mixed> $value | ||
*/ | ||
public function set(string $key, array $value): void | ||
{ | ||
if ($this->enabled) { | ||
$this->cache[$key] = $value; | ||
} | ||
} | ||
|
||
public function flush(): void | ||
{ | ||
if ($this->enabled) { | ||
FileSystem::write($this->cacheFile, serialize($this->cache)); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Wavevision\DIServiceAnnotation; | ||
|
||
use Nette\SmartObject; | ||
use ReflectionClass; | ||
|
||
class ClassValidator | ||
{ | ||
|
||
use SmartObject; | ||
|
||
public function validate(string $class): int | ||
{ | ||
new ReflectionClass($class); | ||
return 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Wavevision\DIServiceAnnotation; | ||
|
||
use Nette\SmartObject; | ||
|
||
class ConsoleOutput implements Output | ||
{ | ||
|
||
use SmartObject; | ||
|
||
public function writeln(string $string): void | ||
{ | ||
echo $string . "\n"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Wavevision\DIServiceAnnotation; | ||
|
||
use Nette\SmartObject; | ||
use Wavevision\Utils\ExternalProgram\Executor; | ||
use Wavevision\Utils\Path; | ||
|
||
class FileValidator | ||
{ | ||
|
||
use SmartObject; | ||
|
||
private Configuration $configuration; | ||
|
||
private Cache $cache; | ||
|
||
public function __construct(Configuration $configuration) | ||
{ | ||
$this->cache = new Cache($configuration); | ||
$this->configuration = $configuration; | ||
} | ||
|
||
public function containsErrors(string $pathname, string $className): bool | ||
{ | ||
if ($autoload = $this->configuration->getAutoloadFile()) { | ||
$hash = md5_file($pathname); | ||
$cacheItem = $this->cache->get($pathname); | ||
if ($cacheItem !== null && $cacheItem['hash'] === $hash) { | ||
if ($cacheItem['error']) { | ||
$this->configuration->getOutput()->writeln( | ||
"Loading from cache: Fatal error in class $className" | ||
); | ||
return true; | ||
} | ||
} else { | ||
$classValidator = Path::join(__DIR__, '..', '..', 'cli', 'validate-class.php'); | ||
$result = Executor::executeUnchecked("php '$classValidator' '$autoload' '$className'"); | ||
if ($result->getReturnValue() !== 0) { | ||
$this->configuration->getOutput()->writeln( | ||
"Fatal error in class $className\n" . $result->getOutputAsString() | ||
); | ||
$this->cache->set( | ||
$pathname, | ||
[ | ||
'hash' => $hash, | ||
'error' => true, | ||
] | ||
); | ||
return true; | ||
} | ||
} | ||
$this->cache->set( | ||
$pathname, | ||
[ | ||
'hash' => $hash, | ||
'error' => false, | ||
] | ||
); | ||
} | ||
return false; | ||
} | ||
|
||
public function flushCache(): void | ||
{ | ||
$this->cache->flush(); | ||
} | ||
|
||
} |
Oops, something went wrong.