/** * @param string $content * @return void */ public function updateTranslationFileContent($content) { $translationDir = $this->directoryList->getPath(DirectoryList::STATIC_VIEW) . \DIRECTORY_SEPARATOR . $this->assetRepo->getStaticViewFileContext()->getPath(); if (!$this->driverFile->isExists($this->getTranslationFileFullPath())) { $this->driverFile->createDirectory($translationDir, \Magento\Framework\Filesystem\Driver\File::WRITEABLE_DIRECTORY_MODE); } $this->driverFile->filePutContents($this->getTranslationFileFullPath(), $content); }
/** * @param string $content * @return void */ public function updateTranslationFileContent($content) { $translationDir = $this->directoryList->getPath(DirectoryList::STATIC_VIEW) . \DIRECTORY_SEPARATOR . $this->assetRepo->getStaticViewFileContext()->getPath(); if (!$this->driverFile->isExists($this->getTranslationFileFullPath())) { $this->driverFile->createDirectory($translationDir); } $this->driverFile->filePutContents($this->getTranslationFileFullPath(), $content); }
public function testGetData() { $this->filesystem->expects($this->any())->method('isExists')->will($this->returnValue(true)); $dataSerialized = 'a:1:{s:6:"object";a:1:{s:9:"integrity";b:1;}}'; $this->filesystem->expects($this->once())->method('fileGetContents')->will($this->returnValue($dataSerialized)); $data = $this->file->getData(); $this->assertEquals(['object' => ['integrity' => true]], $data); }
/** * gets current js-translation.json timestamp * * @return string|void */ public function getTranslationFileTimestamp() { $translationFilePath = $this->getTranslationFileFullPath(); if ($this->driverFile->isExists($translationFilePath)) { $statArray = $this->driverFile->stat($translationFilePath); if (array_key_exists('mtime', $statArray)) { return $statArray['mtime']; } } }
public function tearDown() { if ($this->originalComposerHome) { putenv('COMPOSER_HOME=' . $this->originalComposerHome); } // Composer home directory is created one level up from where composer.json is. if (is_dir($this->testComposerHome)) { $this->fileDriver->deleteDirectory($this->testComposerHome); } }
public function testGetTranslationFileTimestamp() { $path = 'path'; $contextMock = $this->getMockForAbstractClass('\\Magento\\Framework\\View\\Asset\\ContextInterface', [], '', true, true, true, ['getPath']); $this->assetRepoMock->expects($this->atLeastOnce())->method('getStaticViewFileContext')->willReturn($contextMock); $contextMock->expects($this->atLeastOnce())->method('getPath')->willReturn($path); $this->directoryListMock->expects($this->atLeastOnce())->method('getPath')->willReturn($path); $this->driverFileMock->expects($this->once())->method('isExists')->with('path/path/js-translation.json')->willReturn(true); $this->driverFileMock->expects($this->once())->method('stat')->willReturn(['mtime' => 1445736974]); $this->assertEquals(1445736974, $this->model->getTranslationFileTimestamp()); }
protected function setUp() { $this->dirList = $this->getMock('Magento\\Framework\\App\\Filesystem\\DirectoryList', [], [], '', false); $this->dirList->expects($this->any())->method('getPath')->with(DirectoryList::CONFIG)->willReturn(__DIR__ . '/_files'); $this->fileDriver = $this->getMock('\\Magento\\Framework\\Filesystem\\Driver\\File', [], [], '', false); $this->fileDriver->expects($this->any())->method('isExists')->will($this->returnValueMap([[__DIR__ . '/_files/config.php', true], [__DIR__ . '/_files/custom.php', true], [__DIR__ . '/_files/duplicateConfig.php', true], [__DIR__ . '/_files/env.php', true], [__DIR__ . '/_files/mergeOne.php', true], [__DIR__ . '/_files/mergeTwo.php', true], [__DIR__ . '/_files/nonexistent.php', false]])); $this->driverPool = $this->getMock('\\Magento\\Framework\\Filesystem\\DriverPool', [], [], '', false); $this->driverPool->expects($this->any())->method('getDriver')->willReturn($this->fileDriver); $this->configFilePool = $this->getMock('Magento\\Framework\\Config\\File\\ConfigFilePool', [], [], '', false); $this->configFilePool->expects($this->any())->method('getPaths')->willReturn(['configKeyOne' => 'config.php', 'configKeyTwo' => 'env.php']); }
/** * Retrieve the shop version * * @return string */ public function getShopVersion() { $directory = $this->_filesystem->getDirectoryRead(DirectoryList::CONFIG); $path = $directory->getAbsolutePath() . 'version.txt'; if (!$this->filesystemDriver->isExists($path)) { return false; } $version = $this->filesystemDriver->fileGetContents($path); $version = trim($version); return $version; }
public function testCreateDirectory() { $generatedPath = $this->getTestPath('generated/roo/bar/baz/foo'); $generatedPathBase = $this->getTestPath('generated'); // Delete the generated directory if it already exists if (is_dir($generatedPath)) { $this->assertTrue($this->driver->deleteDirectory($generatedPathBase)); } $this->assertTrue($this->driver->createDirectory($generatedPath, DriverInterface::WRITEABLE_DIRECTORY_MODE)); $this->assertTrue(is_dir($generatedPath)); }
public function testCreateDirectory() { $generatedPath = $this->getTestPath('generated/roo/bar/baz/foo'); $generatedPathBase = $this->getTestPath('generated'); // Delete the generated directory if it already exists if (is_dir($generatedPath)) { $this->assertTrue($this->driver->deleteDirectory($generatedPathBase)); } $this->assertTrue($this->driver->createDirectory($generatedPath, '755')); $this->assertTrue(is_dir($generatedPath)); }
public function setUp() { $this->composerJsonFinder = $this->getMock('Magento\\Framework\\Composer\\ComposerJsonFinder', [], [], '', false); $this->composerJsonFinder->expects($this->once())->method('findComposerJson')->willReturn('composer.json'); $this->directoryList = $this->getMock('Magento\\Framework\\App\\Filesystem\\DirectoryList', [], [], '', false); $this->directoryList->expects($this->exactly(2))->method('getPath')->willReturn('var'); $this->reqUpdDryRunCommand = $this->getMock('Magento\\Composer\\RequireUpdateDryRunCommand', [], [], '', false); $this->file = $this->getMock('Magento\\Framework\\Filesystem\\Driver\\File', [], [], '', false); $this->file->expects($this->once())->method('copy')->with('composer.json', 'var/composer.json'); $composerAppFactory = $this->getMock('Magento\\Framework\\Composer\\MagentoComposerApplicationFactory', [], [], '', false); $composerAppFactory->expects($this->once())->method('createRequireUpdateDryRunCommand')->willReturn($this->reqUpdDryRunCommand); $this->dependencyReadinessCheck = new DependencyReadinessCheck($this->composerJsonFinder, $this->directoryList, $this->file, $composerAppFactory); }
/** * @return void */ public function testHandleSuccessWithoutBubble() { $extra = ['mode' => 'application mode']; $record = ['message' => $this->message, 'level' => $this->recordLevel, 'extra' => $extra]; $this->fileHandler->setLevel($this->handlerLevel); $this->fileHandler->setBubble(false); $file = 'file/path/file.log'; $this->file->expects($this->any())->method('filePutContents')->willReturn(1); $this->file->expects($this->any())->method('getRealPath')->willReturnMap([[$file, false], ['file/path', false], ['file', '/existing_path/file']]); $this->file->expects($this->once())->method('createDirectory')->willReturn(true); $this->config->expects($this->any())->method('getOption')->with('log_file')->willReturn($file); $result = $this->fileHandler->handle($record); $this->assertTrue($result); }
/** * @param string $logFile * @return string */ protected function getFilePath($logFile) { $logFileDir = dirname($logFile); if (!$this->filesystem->getRealPath($logFileDir)) { if (substr($logFileDir, 0, 1) != '/') { $logFileDir = __DIR__ . '/../../../' . $logFileDir; $logFile = $logFileDir . '/' . basename($logFile); } if (!$this->filesystem->isExists($logFileDir)) { $this->filesystem->createDirectory($logFileDir, $this->permissions); } } return $logFile; }
/** * @param string $logFile * @return string */ protected function getFilePath($logFile) { $logFileDir = dirname($logFile); if (!$this->file->getRealPath($logFileDir)) { if (substr($logFileDir, 0, 1) != '/') { $logFileDir = $this->filesystem->getDirectoryRead(DirectoryList::VAR_DIR)->getAbsolutePath() . $logFileDir; $logFile = $logFileDir . DIRECTORY_SEPARATOR . basename($logFile); } if (!$this->file->isExists($logFileDir)) { $this->file->createDirectory($logFileDir, $this->permissions); } } elseif ($logFileDir == '.') { $logFile = $this->filesystem->getDirectoryRead(DirectoryList::VAR_DIR)->getAbsolutePath() . basename($logFile); } return $logFile; }
/** * Roll back database * * @param string $rollbackFile * @return void * @throws LocalizedException */ public function dbRollback($rollbackFile) { if (preg_match('/[0-9]_(db)(.*?).(sql)$/', $rollbackFile) !== 1) { throw new LocalizedException(new Phrase('Invalid rollback file.')); } if (!$this->file->isExists($this->backupsDir . '/' . $rollbackFile)) { throw new LocalizedException(new Phrase('The rollback file does not exist.')); } /** @var \Magento\Framework\Backup\Db $dbRollback */ $dbRollback = $this->objectManager->create('Magento\\Framework\\Backup\\Db'); $dbRollback->setRootDir($this->directoryList->getRoot()); $dbRollback->setBackupsDir($this->backupsDir); $dbRollback->setBackupExtension('sql'); $time = explode('_', $rollbackFile); if (count($time) === 3) { $thirdPart = explode('.', $time[2]); $dbRollback->setName($thirdPart[0]); } $dbRollback->setTime($time[0]); $this->log->log('DB rollback is starting...'); $dbRollback->setResourceModel($this->objectManager->create('Magento\\Backup\\Model\\ResourceModel\\Db')); $dbRollback->rollback(); $this->log->log('DB rollback filename: ' . $dbRollback->getBackupFilename()); $this->log->log('DB rollback path: ' . $dbRollback->getBackupPath()); $this->log->logSuccess('DB rollback completed successfully.'); }
public function testDbRollback() { $this->setupDbBackupRollback(); $this->database->expects($this->once())->method('rollback'); $this->file->expects($this->once())->method('isExists')->with($this->path . '/backups/12345_db.gz')->willReturn(true); $this->model->dbRollback('12345_db.gz'); }
/** * Saving data row array into file * * @param string $file * @param array $data * @return $this */ public function saveData($file, $data) { $fh = fopen($file, 'w'); foreach ($data as $dataRow) { $this->file->filePutCsv($fh, $dataRow, $this->_delimiter, $this->_enclosure); } fclose($fh); return $this; }
/** * Determine whether a CLI command is for compilation, and if so, clear the directory * * @throws \Magento\Framework\Exception\FileSystemException * @return void */ public function handleCompilerEnvironment() { $compilationCommands = [DiCompileCommand::NAME, DiCompileMultiTenantCommand::NAME]; $cmdName = $this->input->getFirstArgument(); $isHelpOption = $this->input->hasParameterOption('--help') || $this->input->hasParameterOption('-h'); if (!in_array($cmdName, $compilationCommands) || $isHelpOption) { return; } $generationDir = $cmdName === DiCompileMultiTenantCommand::NAME ? $this->input->getParameterOption(DiCompileMultiTenantCommand::INPUT_KEY_GENERATION) : null; if (!$generationDir) { $mageInitParams = $this->serviceManager->get(InitParamListener::BOOTSTRAP_PARAM); $mageDirs = isset($mageInitParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS]) ? $mageInitParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] : []; $generationDir = (new DirectoryList(BP, $mageDirs))->getPath(DirectoryList::GENERATION); } if ($this->filesystemDriver->isExists($generationDir)) { $this->filesystemDriver->deleteDirectory($generationDir); } }
/** * Run Composer dependency check * * @param array $packages * @return array * @throws \Exception */ public function runReadinessCheck(array $packages) { $composerJson = $this->composerJsonFinder->findComposerJson(); $this->file->copy($composerJson, $this->directoryList->getPath(DirectoryList::VAR_DIR) . '/composer.json'); $workingDir = $this->directoryList->getPath(DirectoryList::VAR_DIR); try { foreach ($packages as $package) { if (strpos($package, 'magento/product-enterprise-edition') !== false) { $this->magentoComposerApplication->runComposerCommand(['command' => 'remove', 'packages' => ['magento/product-community-edition'], '--no-update' => true], $workingDir); } } $this->requireUpdateDryRunCommand->run($packages, $workingDir); return ['success' => true]; } catch (\RuntimeException $e) { $message = str_replace(PHP_EOL, '<br/>', htmlspecialchars($e->getMessage())); return ['success' => false, 'error' => $message]; } }
/** * Change permissions for directories by their code * * @param array $directoryCodeList * @param int $dirPermissions * @param int $filePermissions * @return void * @deprecated */ protected function changePermissions($directoryCodeList, $dirPermissions, $filePermissions) { foreach ($directoryCodeList as $code) { $directoryPath = $this->directoryList->getPath($code); if ($this->driverFile->isExists($directoryPath)) { $this->filesystem->getDirectoryWrite($code)->changePermissionsRecursively('', $dirPermissions, $filePermissions); } else { $this->driverFile->createDirectory($directoryPath, $dirPermissions); } } }
/** * Determine whether a CLI command is for compilation, and if so, clear the directory * * @throws \Magento\Framework\Exception\FileSystemException * @return void */ public function handleCompilerEnvironment() { $compilationCommands = [DiCompileCommand::NAME]; $cmdName = $this->input->getFirstArgument(); $isHelpOption = $this->input->hasParameterOption('--help') || $this->input->hasParameterOption('-h'); if (!in_array($cmdName, $compilationCommands) || $isHelpOption) { return; } $compileDirList = []; $mageInitParams = $this->serviceManager->get(InitParamListener::BOOTSTRAP_PARAM); $mageDirs = isset($mageInitParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS]) ? $mageInitParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] : []; $directoryList = new DirectoryList(BP, $mageDirs); $compileDirList[] = $directoryList->getPath(DirectoryList::GENERATION); $compileDirList[] = $directoryList->getPath(DirectoryList::DI); foreach ($compileDirList as $compileDir) { if ($this->filesystemDriver->isExists($compileDir)) { $this->filesystemDriver->deleteDirectory($compileDir); } } }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $backupsDir = $this->directoryList->getPath(DirectoryList::VAR_DIR) . '/' . BackupRollback::DEFAULT_BACKUP_DIRECTORY; if ($this->file->isExists($backupsDir)) { $contents = $this->file->readDirectoryRecursively($backupsDir); $tempTable = []; foreach ($contents as $path) { $partsOfPath = explode('/', str_replace('\\', '/', $path)); $fileName = $partsOfPath[count($partsOfPath) - 1]; // if filename starts with '.' skip, e.g. '.git' if (!(strpos($fileName, '.') === 0)) { $filenameWithoutExtension = explode('.', $fileName); // actually first part of $filenameWithoutExtension contains only the filename without extension // and filename contains the type of backup separated by '_' $fileNameParts = explode('_', $filenameWithoutExtension[0]); if (in_array(Factory::TYPE_MEDIA, $fileNameParts)) { $tempTable[$fileName] = Factory::TYPE_MEDIA; } elseif (in_array(Factory::TYPE_DB, $fileNameParts)) { $tempTable[$fileName] = Factory::TYPE_DB; } elseif (in_array('code', $fileNameParts)) { $tempTable[$fileName] = 'code'; } } } if (empty($tempTable)) { $output->writeln('<info>No backup files found.</info>'); return; } $output->writeln("<info>Showing backup files in {$backupsDir}.</info>"); /** @var \Symfony\Component\Console\Helper\Table $table */ $table = $this->getHelperSet()->get('table'); $table->setHeaders(['Backup Filename', 'Backup Type']); asort($tempTable); foreach ($tempTable as $key => $value) { $table->addRow([$key, $value]); } $table->render($output); } else { $output->writeln('<info>No backup files found.</info>'); } }
/** * Retrieve component paths and configs from composer.json files * * @return \Traversable */ public function getComponentsInfo() { $components = [\Magento\Framework\Component\ComponentRegistrar::THEME, \Magento\Framework\Component\ComponentRegistrar::MODULE]; foreach ($components as $component) { $paths = $this->registrar->getPaths($component); foreach ($paths as $name => $path) { if (!strstr($name, 'Swissup')) { continue; } $filePath = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, "{$path}/composer.json"); try { $config = $this->filesystemDriver->fileGetContents($filePath); $config = $this->jsonDecoder->decode($config); $config['path'] = $path; (yield [$config['name'], $config]); } catch (\Exception $e) { // skip module } } } }
/** * @param string $directory * @return bool */ private function _makeDirectory($directory) { if ($this->filesystemDriver->isWritable($directory)) { return true; } try { $this->filesystemDriver->createDirectory($directory, self::DIRECTORY_PERMISSION); return true; } catch (\Magento\Framework\Filesystem\FilesystemException $e) { return false; } }
/** * @param string $directory * @return bool */ private function _makeDirectory($directory) { if ($this->filesystemDriver->isWritable($directory)) { return true; } try { if (!$this->filesystemDriver->isDirectory($directory)) { $this->filesystemDriver->createDirectory($directory, DriverInterface::WRITEABLE_DIRECTORY_MODE); } return true; } catch (FileSystemException $e) { return false; } }
/** * @param string $directory * @return bool */ private function _makeDirectory($directory) { if ($this->filesystemDriver->isWritable($directory)) { return true; } try { if (!$this->filesystemDriver->isDirectory($directory)) { $this->filesystemDriver->createDirectory($directory, self::DIRECTORY_PERMISSION); } return true; } catch (FileSystemException $e) { return false; } }
/** * @dataProvider dataProviderForTestGetRelativePath */ public function testGetRelativePath($basePath, $path, $expected) { $file = new File(); $this->assertEquals($expected, $file->getRelativePath($basePath, $path)); }
/** * test exception * * @expectedException \Magento\Framework\Exception\FileSystemException */ public function testReadDirectoryRecursivelyFailure() { $this->driver->readDirectoryRecursively($this->getTestPath('not-existing-directory')); }