FileReadOnlyStorage.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Drupal\Component\PhpStorage;
  3. /**
  4. * Reads code as regular PHP files, but won't write them.
  5. */
  6. class FileReadOnlyStorage implements PhpStorageInterface {
  7. /**
  8. * The directory where the files should be stored.
  9. *
  10. * @var string
  11. */
  12. protected $directory;
  13. /**
  14. * Constructs this FileStorage object.
  15. *
  16. * @param $configuration
  17. * An associative array, containing at least two keys (the rest are ignored):
  18. * - directory: The directory where the files should be stored.
  19. * - bin: The storage bin. Multiple storage objects can be instantiated with
  20. * the same configuration, but for different bins.
  21. */
  22. public function __construct(array $configuration) {
  23. $this->directory = $configuration['directory'] . '/' . $configuration['bin'];
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function exists($name) {
  29. return file_exists($this->getFullPath($name));
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function load($name) {
  35. // The FALSE returned on failure is enough for the caller to handle this,
  36. // we do not want a warning too.
  37. return (@include_once $this->getFullPath($name)) !== FALSE;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function save($name, $code) {
  43. return FALSE;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function delete($name) {
  49. return FALSE;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function getFullPath($name) {
  55. return $this->directory . '/' . $name;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function writeable() {
  61. return FALSE;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function deleteAll() {
  67. return FALSE;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function listAll() {
  73. $names = [];
  74. if (file_exists($this->directory)) {
  75. foreach (new \DirectoryIterator($this->directory) as $fileinfo) {
  76. if (!$fileinfo->isDot()) {
  77. $name = $fileinfo->getFilename();
  78. if ($name != '.htaccess') {
  79. $names[] = $name;
  80. }
  81. }
  82. }
  83. }
  84. return $names;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function garbageCollection() {
  90. }
  91. }