GenerationDirectoryAccess.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Console;
  7. use Magento\Framework\App\Bootstrap;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Framework\Filesystem\Directory\WriteFactory;
  10. use Magento\Framework\Filesystem\DriverPool;
  11. use Zend\ServiceManager\ServiceManager;
  12. use Magento\Setup\Mvc\Bootstrap\InitParamListener;
  13. /**
  14. * Check generated/code read and write access
  15. */
  16. class GenerationDirectoryAccess
  17. {
  18. /**
  19. * @var ServiceManager
  20. */
  21. private $serviceManager;
  22. /**
  23. * @param ServiceManager $serviceManager
  24. */
  25. public function __construct(
  26. ServiceManager $serviceManager
  27. ) {
  28. $this->serviceManager = $serviceManager;
  29. }
  30. /**
  31. * Check write permissions to generation folders
  32. *
  33. * @return bool
  34. */
  35. public function check()
  36. {
  37. $initParams = $this->serviceManager->get(InitParamListener::BOOTSTRAP_PARAM);
  38. $filesystemDirPaths = isset($initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS])
  39. ? $initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS]
  40. : [];
  41. $directoryList = new DirectoryList(BP, $filesystemDirPaths);
  42. $driverPool = new DriverPool();
  43. $fileWriteFactory = new WriteFactory($driverPool);
  44. $generationDirs = [
  45. DirectoryList::GENERATED,
  46. DirectoryList::GENERATED_CODE,
  47. DirectoryList::GENERATED_METADATA
  48. ];
  49. foreach ($generationDirs as $generationDirectory) {
  50. $directoryPath = $directoryList->getPath($generationDirectory);
  51. $directoryWrite = $fileWriteFactory->create($directoryPath);
  52. if (!$directoryWrite->isExist()) {
  53. try {
  54. $directoryWrite->create();
  55. } catch (\Exception $e) {
  56. return false;
  57. }
  58. }
  59. if (!$directoryWrite->isWritable()) {
  60. return false;
  61. }
  62. }
  63. return true;
  64. }
  65. }