FileCache.php 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace Metadata\Cache;
  3. use Metadata\ClassMetadata;
  4. class FileCache implements CacheInterface
  5. {
  6. private $dir;
  7. public function __construct($dir)
  8. {
  9. if (!is_dir($dir)) {
  10. throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir));
  11. }
  12. if (!is_writable($dir)) {
  13. throw new \InvalidArgumentException(sprintf('The directory "%s" is not writable.', $dir));
  14. }
  15. $this->dir = rtrim($dir, '\\/');
  16. }
  17. public function loadClassMetadataFromCache(\ReflectionClass $class)
  18. {
  19. $path = $this->dir.'/'.strtr($class->getName(), '\\', '-').'.cache.php';
  20. if (!file_exists($path)) {
  21. return null;
  22. }
  23. return include $path;
  24. }
  25. public function putClassMetadataInCache(ClassMetadata $metadata)
  26. {
  27. $path = $this->dir.'/'.strtr($metadata->name, '\\', '-').'.cache.php';
  28. file_put_contents($path, '<?php return unserialize('.var_export(serialize($metadata), true).');');
  29. }
  30. public function evictClassMetadataFromCache(\ReflectionClass $class)
  31. {
  32. $path = $this->dir.'/'.strtr($class->getName(), '\\', '-').'.cache.php';
  33. if (file_exists($path)) {
  34. unlink($path);
  35. }
  36. }
  37. }