public function testGetSetRuntime() { $defaultTimeZoneBackup = date_default_timezone_get(); $interval = '1 day'; $timezone = new DateTimeZone('Europe/Berlin'); $event1 = new CM_Clockwork_Event('foo', $interval); $event2 = new CM_Clockwork_Event('bar', $interval); $date1 = new DateTime('2014-10-31 08:00:00', $timezone); $date2 = new DateTime('2014-10-31 08:02:03', $timezone); $context = 'persistence-test'; $storage = new CM_Clockwork_Storage_FileSystem($context); $serviceManager = CM_Service_Manager::getInstance(); $storage->setServiceManager($serviceManager); $filepath = 'clockwork/' . md5($context) . '.json'; $file = new CM_File($filepath, $serviceManager->getFilesystems()->getData()); $this->assertFalse($file->exists()); $this->assertFalse($file->getParentDirectory()->exists()); $storage->setRuntime($event1, $date1); $this->assertTrue($file->getParentDirectory()->exists()); $this->assertTrue($file->exists()); $storage->setRuntime($event2, $date2); date_default_timezone_set('Antarctica/Vostok'); $storage = new CM_Clockwork_Storage_FileSystem($context); $storage->setServiceManager($serviceManager); $this->assertEquals($date1, $storage->getLastRuntime($event1)); $this->assertEquals($date2, $storage->getLastRuntime($event2)); date_default_timezone_set($defaultTimeZoneBackup); }
/** * @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_Frontend_Render $render * @throws CM_Exception */ public function __construct(CM_Frontend_Render $render) { parent::__construct($render); $this->addVariables(); $file = new CM_File(DIR_PUBLIC . 'static/css/library/icon.less'); if ($file->exists()) { $this->add($file->read()); } foreach (array_reverse($render->getSite()->getModules()) as $moduleName) { foreach (array_reverse($render->getSite()->getThemes()) as $theme) { foreach (CM_Util::rglob('*.less', $render->getThemeDir(true, $theme, $moduleName) . 'css/') as $path) { $file = new CM_File($path); $this->add($file->read()); } } } $viewClasses = CM_View_Abstract::getClassChildren(true); foreach ($viewClasses as $viewClassName) { $validModule = in_array(CM_Util::getNamespace($viewClassName), $render->getSite()->getModules()); $validViewClass = $this->_isValidViewClass($viewClassName); if ($validModule && $validViewClass) { $asset = new CM_Asset_Css_View($this->_render, $viewClassName); $this->add($asset->_getContent()); } } }
/** * @param CM_Site_Abstract $site * @return string * @throws CM_Exception_Invalid */ private function _getAppClassName(CM_Site_Abstract $site) { foreach ($site->getModules() as $moduleName) { $file = new CM_File(DIR_ROOT . CM_Bootloader::getInstance()->getModulePath($moduleName) . 'library/' . $moduleName . '/App.js'); if ($file->exists()) { return $moduleName . '_App'; } } throw new CM_Exception_Invalid('No App class found'); }
public function addVariables() { foreach (array_reverse($this->_render->getSite()->getModules()) as $moduleName) { foreach (array_reverse($this->_render->getSite()->getThemes()) as $theme) { $file = new CM_File($this->_render->getThemeDir(true, $theme, $moduleName) . 'variables.less'); if ($file->exists()) { $this->add($file->read()); } } } }
public function testDeleteByPrefix() { $filesystem = new CM_File_Filesystem(new CM_File_Filesystem_Adapter_Local()); $dirTmp = CM_Bootloader::getInstance()->getDirTmp(); $pathList = array('foo/foobar/bar', 'foo/bar2', 'foo/bar'); /** @var CM_File[] $fileList */ $fileList = array(); foreach ($pathList as $path) { $file = new CM_File($dirTmp . $path, $filesystem); $file->ensureParentDirectory(); $file->write('hello'); $fileList[] = $file; $fileList[] = $file->getParentDirectory(); } foreach ($fileList as $file) { $this->assertTrue($file->exists()); } $filesystem->deleteByPrefix($dirTmp); foreach ($fileList as $file) { $this->assertFalse($file->exists()); } $this->assertTrue((new CM_File($dirTmp))->exists()); }
/** * @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); } }
/** * @param string $filename * @param string $configJson * @param bool|null $merge */ public function setConfig($filename, $configJson, $merge = null) { $merge = (bool) $merge; $configJson = (object) CM_Util::jsonDecode($configJson); $configFile = new CM_File(DIR_ROOT . 'resources/config/' . $filename . '.php'); $config = new CM_Config_Node(); if ($merge && $configFile->exists()) { $config->extendWithFile($configFile); } $config->extendWithConfig($configJson); $configStr = $config->exportAsString('$config'); $indentation = ' '; $indent = function ($content) use($indentation) { return preg_replace('/(:?^|[\\n])/', '$1' . $indentation, $content); }; $configFile->ensureParentDirectory(); $configFile->write(join(PHP_EOL, ['<?php', '// This is autogenerated config file. You should not change it manually.', '', 'return function (CM_Config_Node $config) {', $indent($configStr), '};', ''])); $this->_getStreamOutput()->writeln('Created `' . $configFile->getPath() . '`'); }
/** * @return array */ private function _getColorStyles() { $site = $this->getParams()->getSite('site'); $style = ''; foreach (array_reverse($site->getModules()) as $moduleName) { $file = new CM_File(CM_Util::getModulePath($moduleName) . 'layout/default/variables.less'); if ($file->exists()) { $style .= $file->read() . PHP_EOL; } } preg_match_all('#@(color\\w+)#', $style, $matches); $colors = array_unique($matches[1]); foreach ($colors as $variableName) { $style .= '.' . $variableName . ' { background-color: @' . $variableName . '; }' . PHP_EOL; } $lessCompiler = new lessc(); $style = $lessCompiler->compile($style); preg_match_all('#.(color\\w+)\\s+\\{([^}]+)\\}#', $style, $matches); return array_combine($matches[1], $matches[2]); }
/** * @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; }
/** * @param CM_File $file * @param string $url * @throws CM_Exception * @codeCoverageIgnore */ private function _download(CM_File $file, $url) { $url = (string) $url; if ($file->exists()) { $modificationTime = $file->getModified(); if (time() - $modificationTime > self::CACHE_LIFETIME) { $file->delete(); } } if (!$file->exists()) { $path = $file->getPathOnLocalFilesystem(); $client = new \GuzzleHttp\Client(); $client->get($url, ['timeout' => 600, 'save_to' => $path]); } $this->_streamOutput->writeln('Download completed.'); }
/** * @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(); }
/** * @return string */ protected function _getMachineId() { // Global machine-id from systemd https://www.freedesktop.org/software/systemd/man/machine-id.html $file = new CM_File('/etc/machine-id'); if (!$file->exists()) { // Local machine-id as a backup $serviceManager = CM_Service_Manager::getInstance(); $file = new CM_File('machine-id', $serviceManager->getFilesystems()->getData()); if (!$file->exists()) { $uuid = Ramsey\Uuid\Uuid::uuid4()->toString(); $file->write($uuid); } } return trim($file->read()); }
/** * @param string $pathRelative * @return CM_File[] */ public static function getResourceFiles($pathRelative) { $pathRelative = (string) $pathRelative; $paths = array(); foreach (CM_Bootloader::getInstance()->getModules() as $moduleName) { $paths[] = CM_Util::getModulePath($moduleName) . 'resources/' . $pathRelative; } $paths[] = DIR_ROOT . 'resources/' . $pathRelative; $files = array(); foreach (array_unique($paths) as $path) { $file = new CM_File($path); if ($file->exists()) { $files[] = $file; } } return $files; }
/** * @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; }