1 | <?php declare(strict_types=1); |
||
14 | class LocalFileManager implements FileManagerInterface |
||
15 | { |
||
16 | /** |
||
17 | * Wrapper for reading file. |
||
18 | * |
||
19 | * @param string $file Full path to file |
||
20 | * |
||
21 | * @return string Asset content |
||
22 | */ |
||
23 | 1 | public function read($file) |
|
27 | |||
28 | /** |
||
29 | * Wrapper for writing file. |
||
30 | * |
||
31 | * @param string $asset Full path to file |
||
32 | * |
||
33 | * @param string $content Asset content |
||
34 | * |
||
35 | * @throws \Exception @see self::mkdir() |
||
36 | */ |
||
37 | 3 | public function write($asset, $content) |
|
38 | { |
||
39 | 3 | $path = dirname($asset); |
|
40 | 3 | if (!file_exists($path)) { |
|
41 | 2 | $this->mkdir($path); |
|
42 | } |
||
43 | |||
44 | 3 | file_put_contents($asset, $content); |
|
45 | 3 | } |
|
46 | |||
47 | /** |
||
48 | * Create folder. |
||
49 | * |
||
50 | * @param string $path Full path to asset |
||
51 | * |
||
52 | * @throws \Exception If something went wrong on folder creation |
||
53 | */ |
||
54 | 4 | public function mkdir($path) |
|
55 | { |
||
56 | // Create cache path |
||
57 | 4 | if (!$this->exists($path)) { |
|
58 | try { |
||
59 | 3 | mkdir($path, 0777, true); |
|
60 | 1 | } catch (\Exception $e) { |
|
61 | 1 | throw new \Exception($e->getMessage() . ' ' . $path); |
|
62 | } |
||
63 | } |
||
64 | 3 | } |
|
65 | |||
66 | /** |
||
67 | * If path(file or folder) exists. |
||
68 | * |
||
69 | * @param string $path Path for validating existence |
||
70 | * |
||
71 | * @return bool True if path exists |
||
72 | */ |
||
73 | 4 | public function exists($path) |
|
77 | |||
78 | /** |
||
79 | * Wrapper for touching file. |
||
80 | * |
||
81 | * @param string $asset Full path to file |
||
82 | * @param int $timestamp Timestamp |
||
83 | */ |
||
84 | 1 | public function touch($asset, $timestamp) |
|
89 | |||
90 | /** |
||
91 | * Remove path/file recursively. |
||
92 | * |
||
93 | * @param string $path Path to be removed |
||
94 | */ |
||
95 | 1 | public function remove($path) |
|
96 | { |
||
97 | 1 | if (is_dir($path)) { |
|
98 | // Get folder content |
||
99 | 1 | foreach (glob($path . '*', GLOB_MARK) as $file) { |
|
100 | // Recursion |
||
101 | 1 | $this->remove($file); |
|
102 | } |
||
103 | |||
104 | // Remove folder after all internal sub-folders are clear |
||
105 | 1 | rmdir($path); |
|
106 | 1 | } elseif (is_file($path)) { |
|
107 | 1 | unlink($path); |
|
108 | } |
||
109 | 1 | } |
|
110 | |||
111 | /** |
||
112 | * Get last file modification timestamp. |
||
113 | * |
||
114 | * @param string $file Path to file |
||
115 | * |
||
116 | * @return int File modification timestamp |
||
117 | */ |
||
118 | 1 | public function lastModified($file) |
|
122 | |||
123 | /** |
||
124 | * Recursively scan collection of paths to find files with passed |
||
125 | * extensions. Method is based on linux find command so this method |
||
126 | * can face difficulties on other OS. |
||
127 | * |
||
128 | * |
||
129 | * @param array $paths Paths for files scanning |
||
130 | * @param array $extensions File extension filter |
||
131 | * @param array $excludeFolders Path patterns for excluding |
||
132 | * |
||
133 | * @return array Found files |
||
134 | */ |
||
135 | 1 | public function scan(array $paths, array $extensions, array $excludeFolders = []) |
|
168 | } |
||
169 |