-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #203 from koriym/support_doctrine_annotation_v2
Remove doctrine annotation reader
- Loading branch information
Showing
55 changed files
with
955 additions
and
646 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
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
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
parameters: | ||
level: max | ||
paths: | ||
- sl-src | ||
- src | ||
- tests | ||
excludePaths: | ||
|
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 | ||
} | ||
} |
Oops, something went wrong.