Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1194 from njam/filesystem
Browse files Browse the repository at this point in the history
Filesystem for CM_File
  • Loading branch information
njam committed May 7, 2014
2 parents 21162c6 + 2dcdd98 commit 7e5cadf
Show file tree
Hide file tree
Showing 47 changed files with 1,716 additions and 703 deletions.
14 changes: 6 additions & 8 deletions library/CM/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ public static function getInstance() {
}

public function setupFilesystem() {
CM_Util::mkDir(CM_Bootloader::getInstance()->getDirData());
CM_Util::mkDir(CM_Bootloader::getInstance()->getDirUserfiles());
$dirTmp = CM_Bootloader::getInstance()->getDirTmp();
CM_Util::rmDirContents($dirTmp);
CM_Util::mkdir($dirTmp);
CM_ServiceManager::getInstance()->getFilesystem('filesystemTmp')->setup(true);
CM_ServiceManager::getInstance()->getFilesystem('filesystemData')->setup();
CM_ServiceManager::getInstance()->getFilesystem('filesystemUserfiles')->setup();
}

/**
Expand Down Expand Up @@ -215,10 +213,10 @@ private function _getUpdateScriptPath($version, $namespace = null) {
if ($namespace) {
$path = CM_Util::getNamespacePath($namespace);
}
$updateScript = $path . 'resources/db/update/' . $version . '.php';
if (!CM_File::exists($updateScript)) {
$file = new CM_File($path . 'resources/db/update/' . $version . '.php');
if (!$file->getExists()) {
throw new CM_Exception_Invalid('Update script `' . $version . '` does not exist for `' . $namespace . '` namespace.');
}
return $updateScript;
return $file->getPath();
}
}
11 changes: 5 additions & 6 deletions library/CM/App/Installation.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ public function getPackages() {
* @return integer
*/
public function getUpdateStamp() {
$composerJsonStamp = CM_File::getModified($this->_dirRoot . 'composer.json');
$installedJsonPath = $this->_dirRoot . $this->_getComposerVendorDir() . 'composer/installed.json';
$installedJsonStamp = CM_File::getModified($installedJsonPath);
return max($composerJsonStamp, $installedJsonStamp);
$fileComposerJson = new CM_File($this->_dirRoot . 'composer.json');
$fileInstalledJson = new CM_File($this->_dirRoot . $this->_getComposerVendorDir() . 'composer/installed.json');
return max($fileComposerJson->getModified(), $fileInstalledJson->getModified());
}

/**
Expand All @@ -106,10 +105,10 @@ protected function _getComposerPackages() {
* @return string
*/
protected function _getComposerVendorDir() {
$composerJsonStamp = CM_File::getModified($this->_dirRoot . 'composer.json');
$fileComposerJson = new CM_File($this->_dirRoot . 'composer.json');
$cacheKey = CM_CacheConst::ComposerVendorDir;
$fileCache = new CM_Cache_Storage_File();
if (false === ($vendorDir = $fileCache->get($cacheKey)) || $composerJsonStamp > $fileCache->getCreateStamp($cacheKey)) {
if (false === ($vendorDir = $fileCache->get($cacheKey)) || $fileComposerJson->getModified() > $fileCache->getCreateStamp($cacheKey)) {
$vendorDir = rtrim($this->_getComposer()->getConfig()->get('vendor-dir'), '/') . '/';
$fileCache->set($cacheKey, $vendorDir);
}
Expand Down
9 changes: 4 additions & 5 deletions library/CM/Asset/Css/Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ public function __construct(CM_Render $render) {

foreach (array_reverse($render->getSite()->getNamespaces()) as $namespace) {
foreach (array_reverse($render->getSite()->getThemes()) as $theme) {
$path = $render->getThemeDir(true, $theme, $namespace) . 'variables.less';
if (CM_File::exists($path)) {
$file = new CM_File($path);
$file = new CM_File($render->getThemeDir(true, $theme, $namespace) . 'variables.less');
if ($file->getExists()) {
$this->add($file->read());
}
}
}
if (CM_File::exists($path = DIR_PUBLIC . 'static/css/library/icon.less')) {
$file = new CM_File($path);
$file = new CM_File(DIR_PUBLIC . 'static/css/library/icon.less');
if ($file->getExists()) {
$this->add($file->read());
}
foreach (array_reverse($render->getSite()->getNamespaces()) as $namespace) {
Expand Down
4 changes: 2 additions & 2 deletions library/CM/Asset/Javascript/Internal.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public function __construct(CM_Site_Abstract $site) {
*/
private function _getAppClassName(CM_Site_Abstract $site) {
foreach ($site->getNamespaces() as $namespace) {
$appClassFilename = DIR_ROOT . CM_Bootloader::getInstance()->getNamespacePath($namespace) . 'library/' . $namespace . '/App.js';
if (CM_File::exists($appClassFilename)) {
$file = new CM_File(DIR_ROOT . CM_Bootloader::getInstance()->getNamespacePath($namespace) . 'library/' . $namespace . '/App.js');
if ($file->getExists()) {
return $namespace . '_App';
}
}
Expand Down
30 changes: 16 additions & 14 deletions library/CM/Bootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function load() {
$this->_constants();
$this->_exceptionHandler();
$this->_errorHandler();
$this->_registerServices();
$this->_defaults();
}

Expand Down Expand Up @@ -117,20 +118,6 @@ public function reloadNamespacePaths() {
$cache->delete($cacheKey);
}

/**
* @return string
*/
public function getDirUserfiles() {
return DIR_PUBLIC . 'userfiles/';
}

/**
* @return string
*/
public function getDirData() {
return DIR_ROOT . 'data/';
}

/**
* @return string
*/
Expand Down Expand Up @@ -177,6 +164,21 @@ protected function _exceptionHandler() {
});
}

protected function _registerServices() {
$serviceManager = CM_ServiceManager::getInstance();

$serviceManager->register('filesystemTmp', 'CM_Service_Filesystem', array(
'CM_File_Filesystem_Adapter_Local',
array(
'pathPrefix' => $this->getDirTmp(),
),
));

foreach (CM_Config::get()->services as $serviceKey => $serviceDefinition) {
$serviceManager->register($serviceKey, $serviceDefinition['class'], $serviceDefinition['arguments']);
}
}

protected function _defaults() {
date_default_timezone_set($this->getTimeZone()->getName());
CMService_Newrelic::getInstance()->setConfig();
Expand Down
14 changes: 0 additions & 14 deletions library/CM/Bootloader/Testing.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,4 @@ public function getDataPrefix() {
public function getDirTmp() {
return DIR_ROOT . 'tests/tmp/tmp/';
}

/**
* @return string
*/
public function getDirUserfiles() {
return DIR_ROOT . 'tests/tmp/userfiles/';
}

/**
* @return string
*/
public function getDirData() {
return DIR_ROOT . 'tests/tmp/data/';
}
}
34 changes: 17 additions & 17 deletions library/CM/Cache/Storage/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class CM_Cache_Storage_File extends CM_Cache_Storage_Abstract {
* @return int
*/
public function getCreateStamp($key) {
$path = $this->_getPath($this->_getKeyArmored($key));
if (!CM_File::exists($path)) {
$file = $this->_getFile($this->_getKeyArmored($key));
if (!$file->getExists()) {
return null;
}
return CM_File::getModified($path);
return $file->getModified();
}

protected function _getName() {
Expand All @@ -23,43 +23,43 @@ protected function _set($key, $value, $lifeTime = null) {
if (null !== $lifeTime) {
throw new CM_Exception_NotImplemented('Can\'t use lifetime for `CM_Cache_File`');
}
CM_Util::mkDir($this->_getDirStorage());
CM_File::create($this->_getPath($key), serialize($value));
$file = $this->_getFile($key);
$file->ensureParentDirectory();
$file->write(serialize($value));
}

protected function _get($key) {
$path = $this->_getPath($key);
if (!CM_File::exists($path)) {
$file = $this->_getFile($key);
if (!$file->getExists()) {
return false;
}
$file = new CM_File($path);
return unserialize($file->read());
}

protected function _delete($key) {
$path = $this->_getPath($key);
if (CM_File::exists($path)) {
$file = new CM_File($path);
$file = $this->_getFile($key);
if ($file->getExists()) {
$file->delete();
}
}

protected function _flush() {
CM_Util::rmDirContents($this->_getDirStorage());
$this->_getDirStorage()->delete(true);
}

/**
* @param string $key
* @return string
* @return CM_File
*/
private function _getPath($key) {
return self::_getDirStorage() . md5($key);
private function _getFile($key) {
return $this->_getDirStorage()->joinPath(md5($key));
}

/**
* @return string
* @return CM_File
*/
private function _getDirStorage() {
return CM_Bootloader::getInstance()->getDirTmp() . 'cache/';
$filesystemService = CM_ServiceManager::getInstance()->getFilesystem('filesystemTmp');
return new CM_File('cache', $filesystemService->getFilesystem());
}
}
21 changes: 11 additions & 10 deletions library/CM/Cli/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,22 @@ private function _getSynchronized() {
}

/**
* @return string
* @return CM_File
*/
private function _getPidFilePath() {
return CM_Bootloader::getInstance()->getDirData() . 'locks/' . $this->_class->getName() . ':' . $this->_method->getName();
private function _getPidFile() {
$filesystemService = CM_ServiceManager::getInstance()->getFilesystem('filesystemData');
return new CM_File('locks/' . $this->_class->getName() . ':' . $this->_method->getName(), $filesystemService->getFilesystem());
}

/**
* @return boolean
*/
private function _isRunning() {
$path = $this->_getPidFilePath();
if (!CM_File::exists($path)) {
$pidFile = $this->_getPidFile();
if (!$pidFile->getExists()) {
return false;
}
$file = new CM_File($path);
$pid = $file->read();
$pid = $pidFile->read();
if (!ctype_digit($pid) || posix_getsid($pid) === false) {
return false;
}
Expand All @@ -138,8 +138,9 @@ private function _isRunning() {
*/
private function _createPidFile() {
$pid = posix_getpid();
$pidFilePath = $this->_getPidFilePath();
CM_Util::mkDir(dirname($pidFilePath));
return CM_File::create($pidFilePath, $pid);
$pidFile = $this->_getPidFile();
$pidFile->ensureParentDirectory();
$pidFile->write($pid);
return $pidFile;
}
}
12 changes: 5 additions & 7 deletions library/CM/Component/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ public static function rpc_time() {
* @return string[]
*/
private function _getIcons() {
$path = DIR_PUBLIC . '/static/css/library/icon.less';
if (!CM_File::exists($path)) {
$file = new CM_File(DIR_PUBLIC . '/static/css/library/icon.less');
if (!$file->getExists()) {
return array();
}

$file = new CM_File($path);
preg_match_all('#\.icon-(.+?):before { content:#', $file->read(), $icons);
return $icons[1];
}
Expand All @@ -73,10 +72,9 @@ private function _getColorStyles() {
$site = $this->getParams()->getSite('site');
$style = '';
foreach (array_reverse($site->getNamespaces()) as $namespace) {
$path = CM_Util::getNamespacePath($namespace) . 'layout/default/variables.less';
if (CM_File::exists($path)) {
$file = new CM_File($path);
$style .= $file . PHP_EOL;
$file = new CM_File(CM_Util::getNamespacePath($namespace) . 'layout/default/variables.less');
if ($file->getExists()) {
$style .= $file->read() . PHP_EOL;
}
}
preg_match_all('#@(color\w+)#', $style, $matches);
Expand Down
17 changes: 9 additions & 8 deletions library/CM/Css/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@ public function iconRefresh() {
}
$this->_getOutput()->writeln('Processing ' . count($svgFileList) . ' unique icons...');

$dirWork = CM_Util::mkDirTmp();
$dirWork = CM_File::createTmpDir();
foreach ($svgFileList as $fontFile) {
$fontFile->copy($dirWork . $fontFile->getFileName());
$fontFile->copy($dirWork->getPath() . '/' . $fontFile->getFileName());
}

$dirBuild = $dirWork . 'build/';
CM_Util::exec('fontcustom', array('compile', $dirWork, '--no-hash', '--font-name=icon-webfont', '--output=' . $dirBuild));
$dirBuild = new CM_File($dirWork->getPath() . '/build');
CM_Util::exec('fontcustom',
array('compile', $dirWork->getPath(), '--no-hash', '--font-name=icon-webfont', '--output=' . $dirBuild->getPath()));

$cssFile = new CM_File($dirBuild . '/icon-webfont.css');
$cssFile = new CM_File($dirBuild->getPath() . '/icon-webfont.css');
$less = preg_replace('/url\("(?:.*?\/)(.+?)(\??#.+?)?"\)/', 'url(urlFont("\1") + "\2")', $cssFile->read());
CM_File::create(DIR_PUBLIC . 'static/css/library/icon.less', $less);

foreach (glob($dirBuild . 'icon-webfont.*') as $fontPath) {
foreach (glob($dirBuild->getPath() . '/icon-webfont.*') as $fontPath) {
$fontFile = new CM_File($fontPath);
$fontFile->move(DIR_PUBLIC . 'static/font/' . $fontFile->getFileName());
$fontFile->rename(DIR_PUBLIC . 'static/font/' . $fontFile->getFileName());
}

CM_Util::rmDir($dirWork);
$dirWork->delete(true);
$this->_getOutput()->writeln('Created web-font and stylesheet.');
}

Expand Down
26 changes: 18 additions & 8 deletions library/CM/ExceptionHandling/Handler/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,18 @@ public function handleErrorRaw($code, $message, $file, $line) {
* @param Exception $exception
*/
public function handleException(Exception $exception) {
$this->_logException($exception);
$printException = true;
if ($exception instanceof CM_Exception) {
$printException = $exception->getSeverity() >= $this->_getPrintSeverityMin();
}

try {
$this->_logException($exception);
} catch (Exception $e) {
$printException = true;
}

if (!$exception instanceof CM_Exception || $exception->getSeverity() >= $this->_getPrintSeverityMin()) {
if ($printException) {
$this->_printException($exception);
}

Expand Down Expand Up @@ -106,17 +115,18 @@ protected function _logException(Exception $exception) {
$logEntry .= $formatter->formatException($loggerException);
$logEntry .= '### Original Exception: ' . PHP_EOL;
$logEntry .= $formatter->formatException($exception) . PHP_EOL;
$logPath = $this->_getLogPath();
CM_Util::mkDir(dirname($logPath));
error_log($logEntry, 3, $logPath);
$logFile = $this->_getLogFile();
$logFile->ensureParentDirectory();
$logFile->append($logEntry);
}
}

/**
* @return string
* @return CM_File
*/
protected function _getLogPath() {
return CM_Bootloader::getInstance()->getDirData() . 'logs/error.log';
protected function _getLogFile() {
$filesystemService = CM_ServiceManager::getInstance()->getFilesystem('filesystemData');
return new CM_File('logs/error.log', $filesystemService->getFilesystem());
}

/**
Expand Down
Loading

0 comments on commit 7e5cadf

Please sign in to comment.