/** * @param CM_OutputStream_Interface $output * @param CM_File_Filesystem $backupFilesystem */ public function verifyExport(CM_OutputStream_Interface $output, CM_File_Filesystem $backupFilesystem) { $asserter = new S3Export_Asserter(); $sourceFilesystem = $this->_getFilesystemOriginal(); $filePaths = $this->_getRandomFiles($backupFilesystem, 100, 100000); foreach ($filePaths as $path) { $backupFile = new CM_File($path, $backupFilesystem); $sourceFile = new CM_File($path, $sourceFilesystem); $asserter->assertThat($sourceFile->exists(), function () use($output) { $output->write("."); }, function () use($output, $backupFile) { $output->writeln('E'); $output->writeln("Integrity mismatch: Corresponding backup file does not exist for {$backupFile->getPath()}"); }); if ($sourceFile->exists()) { $asserter->assertThat($sourceFile->getHash() === $backupFile->getHash(), function () use($output) { $output->write('.'); }, function () use($output, $backupFile) { $output->writeln('E'); $output->writeln("Integrity mismatch: Different hashes for {$backupFile->getPath()}"); }); } } $output->writeln(''); $output->writeln(join(', ', ["Assertions run: {$asserter->getAssertionCount()}", "succeeded: {$asserter->getAssertionSuccessCount()}", "failed: {$asserter->getAssertionFailCount()}"])); }
/** * @param CM_File $file * @param string|null $content * @param bool|null $overwrite */ public function createFile(CM_File $file, $content = null, $overwrite = null) { $parentDirectory = $file->getParentDirectory(); if (!$parentDirectory->exists()) { $this->createDirectory($parentDirectory); } if ($file->exists()) { if (!$overwrite) { $this->notify('skip', $file->getPath()); } else { $this->notify('modify', $file->getPath()); $file->write($content); } } else { $this->notify('create', $file->getPath()); $file->write($content); } }
public function testFetchDescriptionKeywordsConsiderNamespaceWideLocation() { $dirTmp = CM_Bootloader::getInstance()->getDirTmp(); $render = $this->getMockBuilder('CM_Frontend_Render')->setMethods(array('getTemplatePath', 'getLayoutPath'))->getMock(); $render->expects($this->any())->method('getTemplatePath')->will($this->returnValue(null)); $render->expects($this->exactly(2))->method('getLayoutPath')->will($this->returnCallback(function ($templateName) use($dirTmp) { $templateFile = new CM_File($dirTmp . $templateName); $templateFile->ensureParentDirectory(); $templateFile->write('test-' . $templateName); return $templateFile->getPath(); })); /** @var CM_Frontend_Render $render */ $page = $this->getMockBuilder('CM_Page_Abstract')->getMockForAbstractClass(); /** @var CM_Page_Abstract $page */ $renderAdapter = new CM_RenderAdapter_Page($render, $page); $this->assertSame('test-Page/Abstract/meta-description.tpl', $renderAdapter->fetchDescription()); $this->assertSame('test-Page/Abstract/meta-keywords.tpl', $renderAdapter->fetchKeywords()); }
public function generateConfigInternal() { $indentation = ' '; $indent = function ($content) use($indentation) { return preg_replace('/(:?^|[\\n])/', '$1' . $indentation, $content); }; $generator = new CM_Config_Generator(); $classTypesConfig = $generator->getConfigClassTypes()->exportAsString('$config'); $actionVerbsConfig = $generator->getConfigActionVerbs()->exportAsString('$config'); foreach ($generator->getClassTypesRemoved() as $classRemoved) { $this->_getStreamOutput()->writeln('Removed `' . $classRemoved . '`'); } foreach ($generator->getClassTypesAdded() as $type => $classAdded) { $this->_getStreamOutput()->writeln('Added `' . $classAdded . '` with type `' . $type . '`'); } // Create model class types and action verbs config PHP $configPhp = new CM_File(DIR_ROOT . 'resources/config/internal.php'); $configPhp->ensureParentDirectory(); $configPhp->truncate(); $configPhp->appendLine('<?php'); $configPhp->appendLine('// This is autogenerated config file. You should not change it manually.'); $configPhp->appendLine(); $configPhp->appendLine('return function (CM_Config_Node $config) {'); $configPhp->appendLine($indent($classTypesConfig)); $configPhp->appendLine($indent($actionVerbsConfig)); $configPhp->appendLine('};'); $this->_getStreamOutput()->writeln('Created `' . $configPhp->getPath() . '`'); // Create model class types and action verbs config JS $configJs = new CM_File(DIR_ROOT . 'resources/config/js/internal.js'); $configJs->ensureParentDirectory(); $configJs->truncate(); $classTypes = $generator->getNamespaceTypes(); $configJs->appendLine('cm.model.types = ' . CM_Util::jsonEncode(array_flip($classTypes['CM_Model_Abstract']), true) . ';'); $configJs->appendLine('cm.action.types = ' . CM_Util::jsonEncode(array_flip($classTypes['CM_Action_Abstract']), true) . ';'); $this->_getStreamOutput()->writeln('Created `' . $configJs->getPath() . '`'); }
public function testGetFileNonexistent() { $fileNonexistent = new CM_File('foo/bar'); $params = new CM_Params(array('nonexistent' => $fileNonexistent->getPath())); $this->assertEquals($fileNonexistent, $params->getFile('nonexistent')); }
/** * @param string $template Template file name * @param string|null $module * @param string|null $theme * @param bool|null $absolute * @param bool|null $needed * @throws CM_Exception_Invalid * @return string Layout path based on theme */ public function getLayoutPath($template, $module = null, $theme = null, $absolute = null, $needed = true) { $moduleList = $this->getSite()->getModules(); if ($module !== null) { $moduleList = array((string) $module); } $themeList = $this->getSite()->getThemes(); if ($theme !== null) { $themeList = array((string) $theme); } foreach ($moduleList as $module) { foreach ($themeList as $theme) { $file = new CM_File($this->getThemeDir(true, $theme, $module) . $template); if ($file->exists()) { if ($absolute) { return $file->getPath(); } else { return $this->getThemeDir(false, $theme, $module) . $template; } } } } if ($needed) { throw new CM_Exception_Invalid('Cannot find `' . $template . '` in modules `' . implode('`, `', $moduleList) . '` and themes `' . implode('`, `', $this->getSite()->getThemes()) . '`'); } return null; }
/** * @param CM_File $configFile * @return Closure|null * @throws CM_Exception_Invalid */ private function _getConfigClosure(CM_File $configFile) { if (!$configFile->exists()) { return null; } $configSetter = (require $configFile->getPath()); if (!$configSetter instanceof Closure) { throw new CM_Exception_Invalid('Invalid config file. `' . $configFile->getPath() . '` must return closure'); } return $configSetter; }
/** * @return CM_File */ public static function createTmpDir() { $filesystem = CM_Service_Manager::getInstance()->getFilesystems()->getTmp(); $dir = new CM_File(uniqid(), $filesystem); $filesystem->ensureDirectory($dir->getPath()); return $dir; }
/** * @param int $version * @param string|null $moduleName * @return string * @throws CM_Exception_Invalid */ public function _getUpdateScriptPath($version, $moduleName = null) { $path = DIR_ROOT; if ($moduleName) { $path = CM_Util::getModulePath($moduleName); } $file = new CM_File($path . 'resources/db/update/' . $version . '.php'); if (!$file->exists()) { throw new CM_Exception_Invalid('Update script `' . $version . '` does not exist for `' . $moduleName . '` namespace.'); } return $file->getPath(); }
/** * @param CM_File $configFile * @throws CM_Exception_Invalid */ public function extendWithFile(CM_File $configFile) { $configSetter = (require $configFile->getPath()); if (!$configSetter instanceof Closure) { throw new CM_Exception_Invalid('Invalid config file. Path must return closure', null, ['path' => $configFile->getPath()]); } $configSetter($this); }
/** * @param string $dbName * @param CM_File $dump */ public static function runDump($dbName, CM_File $dump) { $client = CM_Service_Manager::getInstance()->getDatabases()->getMaster(); $args = array(); $args[] = '--host=' . $client->getHost(); $args[] = '--port=' . $client->getPort(); $args[] = '--user='******'--password='******'mysql', $args, null, $dump->getPath()); }
/** * @param string $template Template file name * @param string|null $module * @param string|null $theme * @param bool|null $absolute * @param bool|null $needed * @param CM_Site_Abstract|null $site * @return string * @throws CM_Exception_Invalid */ public function getLayoutPath($template, $module = null, $theme = null, $absolute = null, $needed = null, CM_Site_Abstract $site = null) { if (null === $needed) { $needed = true; } if (null === $site) { $site = $this->getSite(); } $moduleList = $site->getModules(); if ($module !== null) { $moduleList = array((string) $module); } $themeList = $site->getThemes(); if ($theme !== null) { $themeList = array((string) $theme); } foreach ($moduleList as $module) { foreach ($themeList as $theme) { $file = new CM_File($this->getThemeDir(true, $theme, $module) . $template); if ($file->exists()) { if ($absolute) { return $file->getPath(); } else { return $this->getThemeDir(false, $theme, $module) . $template; } } } } if ($needed) { throw new CM_Exception_Invalid('Can\'t find template', null, ['template' => $template, 'modules' => implode('`, `', $moduleList), 'themes' => implode('`, `', $site->getThemes())]); } return null; }
/** * @param CM_File $file * @param CM_File_Filesystem_Adapter $adapterSource * @param CM_File_Filesystem_Adapter $adapterDestination * @throws CM_Exception */ protected function _copyToFileStreaming(CM_File $file, CM_File_Filesystem_Adapter $adapterSource, CM_File_Filesystem_Adapter $adapterDestination) { /** @var CM_File_Filesystem_Adapter_StreamInterface $adapterSource */ /** @var CM_File_Filesystem_Adapter_StreamInterface $adapterDestination */ $streamSource = $adapterSource->getStreamRead($this->getPath()); $adapterDestination->writeStream($file->getPath(), $streamSource); $sizeDestination = $file->getSize(); $sizeSource = $this->getSize(); if ($sizeSource !== $sizeDestination) { throw new CM_Exception('Copy failed', null, ['Source' => $this->getPath(), 'Destination' => $file->getPath(), 'Original size' => $sizeSource, 'Bytes copied' => $sizeDestination, 'Source adapter' => get_class($adapterSource), 'Destination adapter' => get_class($adapterDestination)]); } if (is_resource($streamSource) && !@fclose($streamSource)) { throw new CM_Exception('Could not close stream for path', null, ['path' => $this->getPath(), 'Source adapter' => get_class($adapterSource)]); } }