1 | <?php |
||
5 | class Local implements ContainerInterface |
||
6 | { |
||
7 | |||
8 | protected $baseDirectory; |
||
9 | |||
10 | 20 | public function __construct($baseDirectory) |
|
11 | { |
||
12 | 20 | $this->baseDirectory = $this->normalizePath($baseDirectory) . DIRECTORY_SEPARATOR; |
|
13 | 20 | $this->ensureDirectory($this->baseDirectory); |
|
14 | 20 | } |
|
15 | |||
16 | 20 | protected function normalizePath($path) |
|
17 | { |
||
18 | 20 | $path = dirname(rtrim($path, '\\/') . DIRECTORY_SEPARATOR . 'xxx'); |
|
19 | |||
20 | 20 | return rtrim($path, DIRECTORY_SEPARATOR); |
|
21 | } |
||
22 | |||
23 | 20 | protected function ensureDirectory($directory) |
|
24 | { |
||
25 | 20 | if (!is_dir($directory)) { |
|
26 | 7 | mkdir($directory, 0766, true); |
|
27 | 7 | } |
|
28 | |||
29 | 20 | return is_dir($directory) && $this->isWritable(); |
|
30 | } |
||
31 | |||
32 | /** |
||
33 | * Check if the container is writable |
||
34 | */ |
||
35 | 20 | public function isWritable() |
|
39 | |||
40 | /** |
||
41 | * This will check if a file is in the container |
||
42 | * |
||
43 | * @param string $file |
||
44 | * @return bool |
||
45 | */ |
||
46 | 10 | public function has($file) |
|
50 | |||
51 | /** |
||
52 | * Saves the $content string as a file |
||
53 | * |
||
54 | * @param string $file |
||
55 | * @param string $content |
||
56 | * @return bool |
||
57 | */ |
||
58 | 11 | public function save($file, $content) |
|
68 | |||
69 | /** |
||
70 | * Delete the file from the container |
||
71 | * |
||
72 | * @param string $file |
||
73 | * @return bool |
||
74 | */ |
||
75 | 6 | public function delete($file) |
|
84 | |||
85 | /** |
||
86 | * Moves a temporary uploaded file to a destination in the container |
||
87 | * |
||
88 | * @param string $localFile local path |
||
89 | * @param string $destination |
||
90 | * @return bool |
||
91 | */ |
||
92 | 11 | public function moveUploadedFile($localFile, $destination) |
|
109 | |||
110 | } |
||
111 |
If you suppress an error, we recommend checking for the error condition explicitly: