/** * Check var/generation read and write access * * @return bool */ public function check() { $initParams = $this->serviceManager->get(InitParamListener::BOOTSTRAP_PARAM); $filesystemDirPaths = isset($initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS]) ? $initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] : []; $directoryList = new DirectoryList(BP, $filesystemDirPaths); $generationDirectoryPath = $directoryList->getPath(DirectoryList::GENERATION); $driverPool = new DriverPool(); $fileWriteFactory = new WriteFactory($driverPool); /** @var \Magento\Framework\Filesystem\DriverInterface $driver */ $driver = $driverPool->getDriver(DriverPool::FILE); $directoryWrite = new Write($fileWriteFactory, $driver, $generationDirectoryPath); if ($directoryWrite->isExist()) { if ($directoryWrite->isDirectory() || $directoryWrite->isReadable()) { try { $probeFilePath = $generationDirectoryPath . DIRECTORY_SEPARATOR . uniqid(mt_rand()) . 'tmp'; $fileWriteFactory->create($probeFilePath, DriverPool::FILE, 'w'); $driver->deleteFile($probeFilePath); } catch (\Exception $e) { return false; } } else { return false; } } else { try { $directoryWrite->create(); } catch (\Exception $e) { return false; } } return true; }
public function testCreateWithMode() { $driverPool = $this->getMock('Magento\\Framework\\Filesystem\\DriverPool', ['getDriver']); $driverPool->expects($this->never())->method('getDriver'); $driver = $this->getMockForAbstractClass('Magento\\Framework\\Filesystem\\DriverInterface'); $driver->expects($this->any())->method('isExists')->willReturn(true); $factory = new WriteFactory($driverPool); $result = $factory->create('path', $driver, 'a+'); $this->assertInstanceOf('Magento\\Framework\\Filesystem\\File\\Write', $result); }
/** * Generate Catalog of URNs for the PhpStorm 9 * * @param string[] $dictionary * @param string $configFilePath relative path to the PhpStorm misc.xml * @return void */ public function generateCatalog(array $dictionary, $configFilePath) { $componentNode = null; $projectNode = null; try { $file = $this->fileWriteFactory->create($configFilePath, \Magento\Framework\Filesystem\DriverPool::FILE, 'r'); $dom = new \DOMDocument(); $dom->loadXML($file->readAll()); $xpath = new \DOMXPath($dom); $nodeList = $xpath->query('/project'); $projectNode = $nodeList->item(0); $file->close(); } catch (FileSystemException $f) { //create file if does not exists $dom = new \DOMDocument(); $projectNode = $dom->createElement('project'); //PhpStorm 9 version for component is "4" $projectNode->setAttribute('version', '4'); $dom->appendChild($projectNode); $rootComponentNode = $dom->createElement('component'); //PhpStorm 9 version for ProjectRootManager is "2" $rootComponentNode->setAttribute('version', '2'); $rootComponentNode->setAttribute('name', 'ProjectRootManager'); $projectNode->appendChild($rootComponentNode); } $xpath = new \DOMXPath($dom); $nodeList = $xpath->query("/project/component[@name='ProjectResources']"); $componentNode = $nodeList->item(0); if ($componentNode == null) { $componentNode = $dom->createElement('component'); $componentNode->setAttribute('name', 'ProjectResources'); $projectNode->appendChild($componentNode); } foreach ($dictionary as $urn => $xsdPath) { $node = $dom->createElement('resource'); $node->setAttribute('url', $urn); $node->setAttribute('location', $xsdPath); $componentNode->appendChild($node); } $dom->formatOutput = true; $file = $this->fileWriteFactory->create($configFilePath, \Magento\Framework\Filesystem\DriverPool::FILE, 'w'); $file->write($dom->saveXML()); $file->close(); }