Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FilePermissions.php
Go to the documentation of this file.
1 <?php
7 
14 
19 {
23  protected $filesystem;
24 
28  protected $directoryList;
29 
33  private $state;
34 
41 
48 
55 
62 
69 
75  public function __construct(
78  State $state = null
79  ) {
80  $this->filesystem = $filesystem;
81  $this->directoryList = $directoryList;
82  $this->state = $state ?: ObjectManager::getInstance()->get(State::class);
83  }
84 
91  {
92  if (!$this->installationWritableDirectories) {
93  $data = [
98  ];
99  if ($this->state->getMode() !== State::MODE_PRODUCTION) {
101  }
102  foreach ($data as $code) {
103  $this->installationWritableDirectories[$code] = $this->directoryList->getPath($code);
104  }
105  }
106  return array_values($this->installationWritableDirectories);
107  }
108 
115  {
116  if (!$this->applicationNonWritableDirectories) {
117  $data = [
119  ];
120  foreach ($data as $code) {
121  $this->applicationNonWritableDirectories[$code] = $this->directoryList->getPath($code);
122  }
123  }
124  return array_values($this->applicationNonWritableDirectories);
125  }
126 
133  {
134  if (!$this->installationCurrentWritableDirectories) {
135  foreach ($this->installationWritableDirectories as $code => $path) {
136  if ($this->isWritable($code)) {
137  if ($this->checkRecursiveDirectories($path)) {
138  $this->installationCurrentWritableDirectories[] = $path;
139  }
140  } else {
141  $this->nonWritablePathsInDirectories[$path] = [$path];
142  }
143  }
144  }
146  }
147 
154  private function checkRecursiveDirectories($directory)
155  {
157  $directoryIterator = new \RecursiveIteratorIterator(
158  new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS),
159  \RecursiveIteratorIterator::CHILD_FIRST
160  );
161  $noWritableFilesFolders = [
162  $this->directoryList->getPath(DirectoryList::GENERATED_CODE) . '/',
163  $this->directoryList->getPath(DirectoryList::GENERATED_METADATA) . '/',
164  ];
165 
166  $directoryIterator = new Filter($directoryIterator, $noWritableFilesFolders);
167 
168  $directoryIterator = new ExcludeFilter(
169  $directoryIterator,
170  [
171  $this->directoryList->getPath(DirectoryList::SESSION) . '/',
172  ]
173  );
174 
175  $directoryIterator->setMaxDepth(1);
176 
177  $foundNonWritable = false;
178 
179  try {
180  foreach ($directoryIterator as $subDirectory) {
181  if (!$subDirectory->isWritable() && !$subDirectory->isLink()) {
182  $this->nonWritablePathsInDirectories[$directory][] = $subDirectory->getPathname();
183  $foundNonWritable = true;
184  }
185  }
186  } catch (\UnexpectedValueException $e) {
187  return false;
188  }
189  return !$foundNonWritable;
190  }
191 
198  {
199  if (!$this->applicationCurrentNonWritableDirectories) {
200  foreach ($this->applicationNonWritableDirectories as $code => $path) {
201  if ($this->isNonWritable($code)) {
202  $this->applicationCurrentNonWritableDirectories[] = $path;
203  }
204  }
205  }
207  }
208 
215  protected function isWritable($code)
216  {
217  $directory = $this->filesystem->getDirectoryWrite($code);
218  return $this->isReadableDirectory($directory) && $directory->isWritable();
219  }
220 
227  protected function isNonWritable($code)
228  {
229  $directory = $this->filesystem->getDirectoryWrite($code);
230  return $this->isReadableDirectory($directory) && !$directory->isWritable();
231  }
232 
239  protected function isReadableDirectory($directory)
240  {
241  if (!$directory->isExist() || !$directory->isDirectory() || !$directory->isReadable()) {
242  return false;
243  }
244  return true;
245  }
246 
253  public function getMissingWritablePathsForInstallation($associative = false)
254  {
256  $current = $this->getInstallationCurrentWritableDirectories();
257  $missingPaths = [];
258  foreach (array_diff($required, $current) as $missingPath) {
259  if (isset($this->nonWritablePathsInDirectories[$missingPath])) {
260  if ($associative) {
261  $missingPaths[$missingPath] = $this->nonWritablePathsInDirectories[$missingPath];
262  } else {
263  $missingPaths = array_merge(
264  $missingPaths,
265  $this->nonWritablePathsInDirectories[$missingPath]
266  );
267  }
268  }
269  }
270  if ($associative) {
271  $required = array_flip($required);
272  $missingPaths = array_merge($required, $missingPaths);
273  }
274  return $missingPaths;
275  }
276 
283  {
284  $writableDirectories = [
287  ];
288 
289  $requireWritePermission = [];
290  foreach ($writableDirectories as $code) {
291  if (!$this->isWritable($code)) {
292  $path = $this->directoryList->getPath($code);
293  if (!$this->checkRecursiveDirectories($path)) {
294  $requireWritePermission[] = $path;
295  }
296  }
297  }
298 
299  return $requireWritePermission;
300  }
301 
310  {
312  $current = $this->getInstallationCurrentWritableDirectories();
313  return array_diff($required, $current);
314  }
315 
322  {
325  return array_diff($required, $current);
326  }
327 }
__construct(Filesystem $filesystem, DirectoryList $directoryList, State $state=null)
getMissingWritablePathsForInstallation($associative=false)
$code
Definition: info.phtml:12
$required
Definition: wrapper.phtml:8