-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove doctrine annotation reader #203
Merged
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
c41bf5f
Remove minimum-stability": "alpha"
koriym 71e2851
Support doctrine/annotations v2
koriym 9cc7ee7
Remove ./vendor/bin path
koriym 89044bf
Ignore vendor-bin diff
koriym 9500b22
Remove composer-bin script
koriym 28d763e
Fix CS
koriym fbadefc
Bump psalm version
koriym a9bcf17
Psalm monochrome output
koriym 8edf561
Soothe psalm
koriym bf9775d
Remove instatioation
koriym 890ce9e
Add inherited methods in ReflectionClass
koriym 3313081
Create ReflectionClassTest
koriym 5eab86f
Soothe phpstan
koriym e450ce0
Remove all doctrine/reader from Ray/Aop expect ReflectClass and Refle…
koriym 87da6cd
Add annotation read in custom matcher section in README
koriym bc3f5ee
Change createCodeGen method private
koriym d9703e2
Import PsrCachedReader from doctrine/annotation
koriym 958446d
Create CacheReader
koriym e362a00
Bump QA tools
koriym bbfcb4a
Show respect
koriym a805754
Analyse Ray\ServiceLocator with phpstan/psalm
koriym 2f3950c
Change method name string to constant
koriym c406736
Remove from Testing section from README
koriym eb89d47
Add performance section
koriym File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ray\ServiceLocator; | ||
|
||
use Ray\ServiceLocator\Exception\DirectoryNotWritableException; | ||
|
||
use function file_exists; | ||
use function file_put_contents; | ||
use function hash; | ||
use function is_writable; | ||
use function mkdir; | ||
use function pathinfo; | ||
use function rename; | ||
use function serialize; | ||
use function substr; | ||
use function tempnam; | ||
use function unlink; | ||
use function unserialize; | ||
|
||
use const DIRECTORY_SEPARATOR; | ||
use const PATHINFO_DIRNAME; | ||
|
||
/** | ||
* Minimal cache | ||
*/ | ||
final class Cache | ||
{ | ||
/** @var string */ | ||
private $tmpDir; | ||
|
||
public function __construct(string $tmpDir) | ||
{ | ||
$this->tmpDir = $tmpDir; | ||
} | ||
|
||
/** | ||
* @psalm-param callable():array<object> $callback | ||
* | ||
* @return array<object> | ||
* | ||
* @psalm-suppress MixedInferredReturnType | ||
*/ | ||
public function get(string $key, callable $callback): array | ||
{ | ||
$filename = $this->getFilename($key); | ||
if (! file_exists($filename)) { | ||
$value = $callback(); | ||
$this->writeFile($this->getFilename($key), serialize($value)); | ||
|
||
return $value; | ||
} | ||
|
||
/** @psalm-suppress MixedAssignment, MixedArgument, MixedReturnStatement */ | ||
return unserialize(require $filename); // @phpstan-ignore-line | ||
} | ||
|
||
private function getFilename(string $id): string | ||
{ | ||
$hash = hash('crc32', $id); | ||
|
||
$dir = $this->tmpDir | ||
. DIRECTORY_SEPARATOR | ||
. substr($hash, 0, 2); | ||
if (! is_writable($dir)) { | ||
mkdir($dir, 0777, true); | ||
} | ||
|
||
return $dir | ||
. DIRECTORY_SEPARATOR | ||
. $hash | ||
. '.php'; | ||
} | ||
|
||
private function writeFile(string $filename, string $value): void | ||
{ | ||
$filepath = pathinfo($filename, PATHINFO_DIRNAME); | ||
if (! is_writable($filepath)) { | ||
// @codeCoverageIgnoreStart | ||
throw new DirectoryNotWritableException($filepath); | ||
// @codeCoverageIgnoreEnd | ||
} | ||
|
||
$tmpFile = (string) tempnam($filepath, 'swap'); | ||
$valueWithSingileQuote = "'{$value}'"; | ||
|
||
$content = '<?php return ' . $valueWithSingileQuote . ';'; | ||
if (file_put_contents($tmpFile, $content) !== false) { | ||
if (@rename($tmpFile, $filename)) { | ||
return; | ||
} | ||
|
||
// @codeCoverageIgnoreStart | ||
@unlink($tmpFile); | ||
} | ||
|
||
throw new DirectoryNotWritableException($filepath); | ||
// @codeCoverageIgnoreEnd | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ray\ServiceLocator\Exception; | ||
|
||
use RuntimeException; | ||
|
||
final class DirectoryNotWritableException extends RuntimeException | ||
{ | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cache も interface があれば、例えば APCu に変更したいときに APCuCache だけを実装すればOKになるかなと思いましたがどうですかね。。?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
アノテーションリーダーからアトリビュートの過渡期で、より高速を望むなら、
A) rectorなどでアトリビュートにするか または B)オリジナルのキャッシュリーダー PsrCachedReader を使うか
という選択肢があり、ここでB)を選択しなかったのは「他のライブラリに依存するより最小で自己完結的にした方が良い」と考えたからです。ここで新しいインターフェイスを導入するのは"自己完結"に背くような気もして入れてない感じです。いつもは可能な限り汎用品を使って租結合にしてますが、ここは違うところでは?と考えました。どうでしょうか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"シナリオ"をパフォーマンスに含めました。
eb89d47