FileStorage.php 3.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php

/**
 * @file
 * Definition of Drupal\Component\PhpStorage\FileStorage.
 */

namespace Drupal\Component\PhpStorage;

/**
 * Stores the code as regular PHP files.
 */
class FileStorage implements PhpStorageInterface {

  /**
   * The directory where the files should be stored.
   *
   * @var string
   */
  protected $directory;

  /**
   * Constructs this FileStorage object.
   *
25
   * @param array $configuration
26
27
   *   An associative array, containing at least these two keys:
   *   - directory: The directory where the files should be stored.
28
29
   *   - bin: The storage bin. Multiple storage objects can be instantiated with
   *     the same configuration, but for different bins..
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
   */
  public function __construct(array $configuration) {
    $this->directory = $configuration['directory'] . '/' . $configuration['bin'];
  }

  /**
   * Implements Drupal\Component\PhpStorage\PhpStorageInterface::exists().
   */
  public function exists($name) {
    return file_exists($this->getFullPath($name));
  }

  /**
   * Implements Drupal\Component\PhpStorage\PhpStorageInterface::load().
   */
  public function load($name) {
    // The FALSE returned on failure is enough for the caller to handle this,
    // we do not want a warning too.
    return (@include_once $this->getFullPath($name)) !== FALSE;
  }

  /**
   * Implements Drupal\Component\PhpStorage\PhpStorageInterface::save().
   */
  public function save($name, $code) {
    $path = $this->getFullPath($name);
56
57
58
59
    $dir = dirname($path);
    if (!file_exists($dir)) {
      mkdir($dir, 0700, TRUE);
    }
60
61
62
63
64
65
66
67
    return (bool) file_put_contents($path, $code);
  }

  /**
   * Implements Drupal\Component\PhpStorage\PhpStorageInterface::delete().
   */
  public function delete($name) {
    $path = $this->getFullPath($name);
68
69
70
71
    if (file_exists($path)) {
      return $this->unlink($path);
    }
    return FALSE;
72
73
74
75
76
77
78
79
  }

  /**
   * Returns the full path where the file is or should be stored.
   */
  protected function getFullPath($name) {
    return $this->directory . '/' . $name;
  }
80
81
82
83

  /**
   * Implements Drupal\Component\PhpStorage\PhpStorageInterface::writeable().
   */
84
  public function writeable() {
85
86
87
88
89
90
    return TRUE;
  }

  /**
   * Implements Drupal\Component\PhpStorage\PhpStorageInterface::deleteAll().
   */
91
92
  public function deleteAll() {
    return $this->unlink($this->directory);
93
94
95
  }

  /**
96
97
98
99
100
101
102
103
104
105
106
107
   * Deletes files and/or directories in the specified path.
   *
   * If the specified path is a directory the method will
   * call itself recursively to process the contents. Once the contents have
   * been removed the directory will also be removed.
   *
   * @param string $path
   *   A string containing either a file or directory path.
   *
   * @return boolean
   *   TRUE for success or if path does not exist, FALSE in the event of an
   *   error.
108
   */
109
  protected function unlink($path) {
110
    if (file_exists($path)) {
111
      // Ensure the file / folder is writable.
112
      chmod($path, 0700);
113
114
115
116
117
118
119
120
121
122
123
124
      if (is_dir($path)) {
        $dir = dir($path);
        while (($entry = $dir->read()) !== FALSE) {
          if ($entry == '.' || $entry == '..') {
            continue;
          }
          $this->unlink($path . '/' . $entry);
        }
        $dir->close();
        return @rmdir($path);
      }
      return @unlink($path);
125
    }
126
127
    // If there's nothing to delete return TRUE anyway.
    return TRUE;
128
  }
129
}