Updating modules/cms/classes

This commit is contained in:
Stefan Talen 2014-10-11 01:22:03 +02:00
parent b64a744498
commit 202e8869b1
27 changed files with 637 additions and 354 deletions

View File

@ -41,8 +41,9 @@ class Asset extends CmsObject
$defaultTypes = ['css','js','less','sass','scss'];
$configTypes = Config::get('cms.editableAssetTypes');
if (!$configTypes)
if (!$configTypes) {
return $defaultTypes;
}
return $configTypes;
}
@ -64,5 +65,4 @@ class Asset extends CmsObject
{
return null;
}
}

View File

@ -67,8 +67,9 @@ class CmsCompoundObject extends CmsObject
*/
public static function load($theme, $fileName)
{
if (($obj = parent::load($theme, $fileName)) === null)
if (($obj = parent::load($theme, $fileName)) === null) {
return null;
}
CmsException::mask($obj, 200);
$parsedData = SectionParser::parse($obj->content);
@ -89,8 +90,9 @@ class CmsCompoundObject extends CmsObject
*/
public function __get($name)
{
if (is_array($this->settings) && array_key_exists($name, $this->settings))
if (is_array($this->settings) && array_key_exists($name, $this->settings)) {
return $this->settings[$name];
}
return parent::__get($name);
}
@ -103,8 +105,9 @@ class CmsCompoundObject extends CmsObject
*/
public function __isset($key)
{
if (parent::__isset($key) === true)
if (parent::__isset($key) === true) {
return true;
}
return isset($this->settings[$key]);
}
@ -124,8 +127,9 @@ class CmsCompoundObject extends CmsObject
public function runComponents()
{
foreach ($this->components as $component) {
if ($result = $component->onRun())
if ($result = $component->onRun()) {
return $result;
}
}
}
@ -139,13 +143,15 @@ class CmsCompoundObject extends CmsObject
$manager = ComponentManager::instance();
$components = [];
foreach ($this->settings as $setting => $value) {
if (!is_array($value))
if (!is_array($value)) {
continue;
}
$settingParts = explode(' ', $setting);
$settingName = $settingParts[0];
// if (!$manager->hasComponent($settingName))
// if (!$manager->hasComponent($settingName)) {
// continue;
// }
$components[$setting] = $value;
unset($this->settings[$setting]);
@ -182,25 +188,29 @@ class CmsCompoundObject extends CmsObject
$this->code = trim($this->code);
$this->markup = trim($this->markup);
$trim = function(&$values) use (&$trim) {
$trim = function (&$values) use (&$trim) {
foreach ($values as &$value) {
if (!is_array($value))
if (!is_array($value)) {
$value = trim($value);
else $trim($value);
} else {
$trim($value);
}
}
};
$trim($this->settings);
if (array_key_exists('components', $this->settings) && count($this->settings['components']) == 0)
if (array_key_exists('components', $this->settings) && count($this->settings['components']) == 0) {
unset($this->settings['components']);
}
$this->validate();
$content = [];
if ($this->settings)
if ($this->settings) {
$content[] = FileHelper::formatIniString($this->settings);
}
if ($this->code) {
$code = preg_replace('/^\<\?php/', '', $this->code);
@ -224,8 +234,9 @@ class CmsCompoundObject extends CmsObject
*/
public function getViewBag()
{
if ($this->viewBagCache !== false)
if ($this->viewBagCache !== false) {
return $this->viewBagCache;
}
$componentName = 'viewBag';
@ -248,13 +259,15 @@ class CmsCompoundObject extends CmsObject
*/
public function getComponent($componentName)
{
if (!$this->hasComponent($componentName))
if (!$this->hasComponent($componentName)) {
return null;
}
return ComponentManager::instance()->makeComponent(
$componentName,
null,
$this->settings['components'][$componentName]);
$this->settings['components'][$componentName]
);
}
/**
@ -283,31 +296,35 @@ class CmsCompoundObject extends CmsObject
$cached = Cache::get($key, false);
$unserialized = $cached ? @unserialize($cached) : false;
$objectComponentMap = $unserialized ? $unserialized : [];
if ($objectComponentMap)
if ($objectComponentMap) {
self::$objectComponentPropertyMap = $objectComponentMap;
}
}
$objectCode = $this->getBaseFileName();
if (array_key_exists($objectCode, $objectComponentMap)) {
if (array_key_exists($componentName, $objectComponentMap[$objectCode]))
if (array_key_exists($componentName, $objectComponentMap[$objectCode])) {
return $objectComponentMap[$objectCode][$componentName];
}
return [];
}
if (!isset($this->settings['components']))
if (!isset($this->settings['components'])) {
$objectComponentMap[$objectCode] = [];
else {
foreach ($this->settings['components'] as $componentName=>$componentSettings) {
} else {
foreach ($this->settings['components'] as $componentName => $componentSettings) {
$component = $this->getComponent($componentName);
if (!$component)
if (!$component) {
continue;
}
$componentProperties = [];
$propertyDefinitions = $component->defineProperties();
foreach ($propertyDefinitions as $propertyName=>$propertyInfo)
foreach ($propertyDefinitions as $propertyName => $propertyInfo) {
$componentProperties[$propertyName] = $component->property($propertyName);
}
$objectComponentMap[$objectCode][$componentName] = $componentProperties;
}
@ -317,8 +334,9 @@ class CmsCompoundObject extends CmsObject
Cache::put($key, serialize($objectComponentMap), Config::get('cms.parsedPageCacheTTL', 10));
if (array_key_exists($componentName, $objectComponentMap[$objectCode]))
if (array_key_exists($componentName, $objectComponentMap[$objectCode])) {
return $objectComponentMap[$objectCode][$componentName];
}
return [];
}
@ -338,7 +356,9 @@ class CmsCompoundObject extends CmsObject
* the content of the $settings property after the object
* is loaded from a file.
*/
protected function parseSettings() {}
protected function parseSettings()
{
}
/**
* Initializes the object properties from the cached data.
@ -368,14 +388,24 @@ class CmsCompoundObject extends CmsObject
*/
protected function validate()
{
$validation = Validator::make($this->settings, $this->settingsValidationRules, $this->settingsValidationMessages);
if ($validation->fails())
$validation = Validator::make(
$this->settings,
$this->settingsValidationRules,
$this->settingsValidationMessages
);
if ($validation->fails()) {
throw new ValidationException($validation);
}
if ($this->viewBagValidationRules && isset($this->settings['viewBag'])) {
$validation = Validator::make($this->settings['viewBag'], $this->viewBagValidationRules, $this->viewBagValidationMessages);
if ($validation->fails())
$validation = Validator::make(
$this->settings['viewBag'],
$this->viewBagValidationRules,
$this->viewBagValidationMessages
);
if ($validation->fails()) {
throw new ValidationException($validation);
}
}
}
}

View File

@ -50,8 +50,9 @@ class CmsException extends ApplicationException
$message = '';
}
if (isset(static::$errorCodes[$code]))
if (isset(static::$errorCodes[$code])) {
$this->errorType = static::$errorCodes[$code];
}
parent::__construct($message, $code, $previous);
}
@ -104,9 +105,15 @@ class CmsException extends ApplicationException
/*
* Expecting: syntax error, unexpected '!' in Unknown on line 4
*/
if (!starts_with($message, 'syntax error')) return false;
if (strpos($message, 'Unknown') === false) return false;
if (strpos($exception->getFile(), 'SectionParser.php') === false) return false;
if (!starts_with($message, 'syntax error')) {
return false;
}
if (strpos($message, 'Unknown') === false) {
return false;
}
if (strpos($exception->getFile(), 'SectionParser.php') === false) {
return false;
}
/*
* Line number from parse_ini_string() error.
@ -143,23 +150,28 @@ class CmsException extends ApplicationException
$check = false;
// Expected: */modules/cms/classes/CodeParser.php(165) : eval()'d code line 7
if (strpos($exception->getFile(), 'CodeParser.php')) $check = true;
if (strpos($exception->getFile(), 'CodeParser.php')) {
$check = true;
}
// Expected: */app/storage/cache/39/05/home.htm.php
if (strpos($exception->getFile(), $this->compoundObject->getFileName() . '.php')) $check = true;
if (strpos($exception->getFile(), $this->compoundObject->getFileName() . '.php')) {
$check = true;
}
if (!$check)
if (!$check) {
return false;
}
}
/*
* Errors occurring the PHP code base class (Cms\Classes\CodeBase)
*/
else {
} else {
$trace = $exception->getTrace();
if (isset($trace[1]['class'])) {
$class = $trace[1]['class'];
if (!is_subclass_of($class, 'Cms\Classes\CodeBase'))
if (!is_subclass_of($class, 'Cms\Classes\CodeBase')) {
return false;
}
}
}
@ -187,8 +199,9 @@ class CmsException extends ApplicationException
protected function processTwig(Exception $exception)
{
// Must be a Twig related exception
if (!$exception instanceof Twig_Error)
if (!$exception instanceof Twig_Error) {
return false;
}
$this->message = $exception->getRawMessage();
$this->line = $exception->getTemplateLine();
@ -220,5 +233,4 @@ class CmsException extends ApplicationException
return;
}
}
}

View File

@ -80,27 +80,32 @@ class CmsObject implements ArrayAccess
*/
public static function loadCached($theme, $fileName)
{
if (!FileHelper::validatePath($fileName, static::getMaxAllowedPathNesting()))
if (!FileHelper::validatePath($fileName, static::getMaxAllowedPathNesting())) {
throw new SystemException(Lang::get('cms::lang.cms_object.invalid_file', ['name'=>$fileName]));
}
if (!strlen(File::extension($fileName)))
if (!strlen(File::extension($fileName))) {
$fileName .= '.'.static::$defaultExtension;
}
$filePath = static::getFilePath($theme, $fileName);
if (array_key_exists($filePath, ObjectMemoryCache::$cache))
if (array_key_exists($filePath, ObjectMemoryCache::$cache)) {
return ObjectMemoryCache::$cache[$filePath];
}
$key = self::getObjectTypeDirName().crc32($filePath);
clearstatcache($filePath);
$cached = Cache::get($key, false);
if ($cached !== false && ($cached = @unserialize($cached)) !== false) {
if ($cached['mtime'] != @File::lastModified($filePath))
if ($cached['mtime'] != @File::lastModified($filePath)) {
$cached = false;
}
}
if ($cached && !File::isFile($filePath))
if ($cached && !File::isFile($filePath)) {
$cached = false;
}
if ($cached !== false) {
/*
@ -151,19 +156,23 @@ class CmsObject implements ArrayAccess
*/
public static function load($theme, $fileName)
{
if (!FileHelper::validatePath($fileName, static::getMaxAllowedPathNesting()))
if (!FileHelper::validatePath($fileName, static::getMaxAllowedPathNesting())) {
throw new SystemException(Lang::get('cms::lang.cms_object.invalid_file', ['name'=>$fileName]));
}
if (!strlen(File::extension($fileName)))
if (!strlen(File::extension($fileName))) {
$fileName .= '.'.static::$defaultExtension;
}
$fullPath = static::getFilePath($theme, $fileName);
if (!File::isFile($fullPath))
if (!File::isFile($fullPath)) {
return null;
}
if (($content = @File::get($fullPath)) === false)
if (($content = @File::get($fullPath)) === false) {
return null;
}
$obj = new static($theme);
$obj->fileName = $fileName;
@ -209,8 +218,9 @@ class CmsObject implements ArrayAccess
public function getBaseFileName()
{
$pos = strrpos($this->fileName, '.');
if ($pos === false)
if ($pos === false) {
return $this->fileName;
}
return substr($this->fileName, 0, $pos);
}
@ -258,8 +268,9 @@ class CmsObject implements ArrayAccess
]);
}
if (!strlen(File::extension($fileName)))
if (!strlen(File::extension($fileName))) {
$fileName .= '.htm';
}
$this->fileName = $fileName;
return $this;
@ -298,15 +309,20 @@ class CmsObject implements ArrayAccess
*/
public function fill(array $attributes)
{
foreach ($attributes as $key=>$value) {
if (!in_array($key, static::$fillable))
throw new ApplicationException(Lang::get('cms::lang.cms_object.invalid_property', ['name'=>$key]));
foreach ($attributes as $key => $value) {
if (!in_array($key, static::$fillable)) {
throw new ApplicationException(Lang::get(
'cms::lang.cms_object.invalid_property',
['name' => $key]
));
}
$methodName = 'set'.ucfirst($key);
if (method_exists($this, $methodName))
if (method_exists($this, $methodName)) {
$this->$methodName($value);
else
} else {
$this->$key = $value;
}
}
}
@ -317,31 +333,48 @@ class CmsObject implements ArrayAccess
{
$fullPath = static::getFilePath($this->theme, $this->fileName);
if (File::isFile($fullPath) && $this->originalFileName !== $this->fileName)
throw new ApplicationException(Lang::get('cms::lang.cms_object.file_already_exists', ['name'=>$this->fileName]));
if (File::isFile($fullPath) && $this->originalFileName !== $this->fileName) {
throw new ApplicationException(Lang::get(
'cms::lang.cms_object.file_already_exists',
['name'=>$this->fileName]
));
}
$dirPath = rtrim(static::getFilePath($this->theme, ''), '/');
if (!file_exists($dirPath) || !is_dir($dirPath)) {
if (!File::makeDirectory($dirPath, 0777, true, true))
throw new ApplicationException(Lang::get('cms::lang.cms_object.error_creating_directory', ['name'=>$dirPath]));
if (!File::makeDirectory($dirPath, 0777, true, true)) {
throw new ApplicationException(Lang::get(
'cms::lang.cms_object.error_creating_directory',
['name'=>$dirPath]
));
}
}
if (($pos = strpos($this->fileName, '/')) !== false) {
$dirPath = static::getFilePath($this->theme, dirname($this->fileName));
if (!is_dir($dirPath) && !File::makeDirectory($dirPath, 0777, true, true))
throw new ApplicationException(Lang::get('cms::lang.cms_object.error_creating_directory', ['name'=>$dirPath]));
if (!is_dir($dirPath) && !File::makeDirectory($dirPath, 0777, true, true)) {
throw new ApplicationException(Lang::get(
'cms::lang.cms_object.error_creating_directory',
['name'=>$dirPath]
));
}
}
$newFullPath = $fullPath;
if (@File::put($fullPath, $this->content) === false)
throw new ApplicationException(Lang::get('cms::lang.cms_object.error_saving', ['name'=>$this->fileName]));
if (@File::put($fullPath, $this->content) === false) {
throw new ApplicationException(Lang::get(
'cms::lang.cms_object.error_saving',
['name'=>$this->fileName]
));
}
if (strlen($this->originalFileName) && $this->originalFileName !== $this->fileName) {
$fullPath = static::getFilePath($this->theme, $this->originalFileName);
if (File::isFile($fullPath))
if (File::isFile($fullPath)) {
@unlink($fullPath);
}
}
clearstatcache();
@ -378,24 +411,27 @@ class CmsObject implements ArrayAccess
*/
public static function listInTheme($theme, $skipCache = false)
{
if (!$theme)
if (!$theme) {
throw new ApplicationException(Lang::get('cms::lang.theme.active.not_set'));
}
$dirPath = $theme->getPath().'/'.static::getObjectTypeDirName();
$result = [];
if (!File::isDirectory($dirPath))
if (!File::isDirectory($dirPath)) {
return $result;
}
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirPath));
$it->setMaxDepth(1); // Support only a single level of subdirectories
$it->rewind();
while($it->valid()) {
while ($it->valid()) {
if ($it->isFile() && in_array($it->getExtension(), static::$allowedExtensions)) {
$filePath = $it->getBasename();
if ($it->getDepth() > 0)
if ($it->getDepth() > 0) {
$filePath = basename($it->getPath()).'/'.$filePath;
}
$page = $skipCache ? static::load($theme, $filePath) : static::loadCached($theme, $filePath);
$result[] = $page;
@ -426,8 +462,9 @@ class CmsObject implements ArrayAccess
public function __get($name)
{
$methodName = 'get'.ucfirst($name);
if (method_exists($this, $methodName))
if (method_exists($this, $methodName)) {
return $this->$methodName();
}
return null;
}
@ -440,8 +477,9 @@ class CmsObject implements ArrayAccess
public function __isset($key)
{
$methodName = 'get'.ucfirst($key);
if (method_exists($this, $methodName))
if (method_exists($this, $methodName)) {
return true;
}
return false;
}
@ -540,18 +578,24 @@ class CmsObject implements ArrayAccess
* Initializes the object properties from the cached data.
* @param array $cached The cached data array.
*/
protected function initFromCache($cached) {}
protected function initFromCache($cached)
{
}
/**
* Initializes a cache item.
* @param array &$item The cached item array.
*/
protected function initCacheItem(&$item) {}
protected function initCacheItem(&$item)
{
}
/**
* Returns the directory name corresponding to the object type.
* For pages the directory name is "pages", for layouts - "layouts", etc.
* @return string
*/
public static function getObjectTypeDirName() {}
public static function getObjectTypeDirName()
{
}
}

View File

@ -69,13 +69,15 @@ class CmsObjectQuery
*/
public function find($fileName)
{
if (!$this->theme)
if (!$this->theme) {
$this->inEditTheme();
}
if ($this->useCache)
if ($this->useCache) {
return forward_static_call([$this->cmsObject, 'loadCached'], $this->theme, $fileName);
else
} else {
return forward_static_call([$this->cmsObject, 'load'], $this->theme, $fileName);
}
}
/**
@ -84,8 +86,9 @@ class CmsObjectQuery
*/
public function all()
{
if (!$this->theme)
if (!$this->theme) {
$this->inEditTheme();
}
$collection = forward_static_call([$this->cmsObject, 'listInTheme'], $this->theme, !$this->useCache);
$collection = new CmsObjectCollection($collection);
@ -108,5 +111,4 @@ class CmsObjectQuery
$className = get_class($this);
throw new \BadMethodCallException("Call to undefined method {$className}::{$method}()");
}
}

View File

@ -27,7 +27,10 @@ class CmsPropertyHelper
*/
public static function listPages()
{
Flash::warning("CmsPropertyHelper::listPages() is deprecated, use Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName') instead.");
Flash::warning(
"CmsPropertyHelper::listPages() is deprecated, use Page::sortBy('baseFileName')->lists('baseFileName',
'baseFileName') instead."
);
return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
}
}

View File

@ -45,19 +45,25 @@ class CodeBase extends Extendable implements ArrayAccess
* This event is triggered when all components are initialized and before AJAX is handled.
* The layout's onInit method triggers before the page's onInit method.
*/
public function onInit() {}
public function onInit()
{
}
/**
* This event is triggered in the beginning of the execution cycle.
* The layout's onStart method triggers before the page's onStart method.
*/
public function onStart() {}
public function onStart()
{
}
/**
* This event is triggered in the end of the execution cycle, but before the page is displayed.
* The layout's onEnd method triggers after the page's onEnd method.
*/
public function onEnd() {}
public function onEnd()
{
}
/**
* ArrayAccess implementation
@ -99,8 +105,9 @@ class CodeBase extends Extendable implements ArrayAccess
*/
public function __call($method, $parameters)
{
if (method_exists($this, $method))
if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], $parameters);
}
return call_user_func_array([$this->controller, $method], $parameters);
}
@ -115,11 +122,13 @@ class CodeBase extends Extendable implements ArrayAccess
*/
public function __get($name)
{
if (($value = $this->page->{$name}) !== null)
if (($value = $this->page->{$name}) !== null) {
return $value;
}
if (array_key_exists($name, $this->controller->vars))
if (array_key_exists($name, $this->controller->vars)) {
return $this[$name];
}
return null;
}

View File

@ -99,13 +99,15 @@ class CodeParser
$body = preg_replace($pattern, '', $body);
$parentClass = $this->object->getCodeClassParent();
if ($parentClass !== null)
if ($parentClass !== null) {
$parentClass = ' extends '.$parentClass;
}
$fileContents = '<?php '.PHP_EOL;
foreach ($namespaces[0] as $namespace)
foreach ($namespaces[0] as $namespace) {
$fileContents .= $namespace;
}
$fileContents .= 'class '.$className.$parentClass.PHP_EOL;
$fileContents .= '{'.PHP_EOL;
@ -115,15 +117,18 @@ class CodeParser
$this->validate($fileContents);
$dir = dirname($path);
if (!File::isDirectory($dir) && !@File::makeDirectory($dir, 0777, true))
if (!File::isDirectory($dir) && !@File::makeDirectory($dir, 0777, true)) {
throw new SystemException(Lang::get('system::lang.directory.create_fail', ['name'=>$dir]));
}
if (!@File::put($path, $fileContents))
if (!@File::put($path, $fileContents)) {
throw new SystemException(Lang::get('system::lang.file.create_fail', ['name'=>$dir]));
}
$cached = $this->getCachedInfo();
if (!$cached)
if (!$cached) {
$cached = [];
}
$result['className'] = $className;
$result['source'] = 'parser';
@ -148,8 +153,9 @@ class CodeParser
{
$data = $this->parse();
if (!class_exists($data['className']))
if (!class_exists($data['className'])) {
require_once $data['filePath'];
}
$className = $data['className'];
return new $className($page, $layout, $controller);
@ -186,8 +192,9 @@ class CodeParser
protected function getCachedInfo()
{
$cached = Cache::get($this->dataCacheKey, false);
if ($cached !== false && ($cached = @unserialize($cached)) !== false)
if ($cached !== false && ($cached = @unserialize($cached)) !== false) {
return $cached;
}
return null;
}
@ -200,8 +207,9 @@ class CodeParser
{
$cached = $this->getCachedInfo();
if ($cached !== null) {
if (array_key_exists($this->filePath, $cached))
if (array_key_exists($this->filePath, $cached)) {
return $cached[$this->filePath];
}
}
return null;

View File

@ -115,8 +115,9 @@ class CombineAssets
*/
public static function combine($assets = [], $path = null)
{
if (static::$instance === null)
if (static::$instance === null) {
static::$instance = new self();
}
return static::$instance->prepareRequest($assets, $path);
}
@ -128,8 +129,9 @@ class CombineAssets
public function getContents($cacheId)
{
$cacheInfo = $this->getCache($cacheId);
if (!$cacheInfo)
if (!$cacheInfo) {
throw new CmsException(Lang::get('cms::lang.combiner.not_found', ['name'=>$cacheId]));
}
$this->path = $cacheInfo['path'];
$this->storagePath = storage_path().'/combiner/cms';
@ -157,8 +159,9 @@ class CombineAssets
{
$extension = strtolower($extension);
if (!isset($this->aliases[$extension]))
if (!isset($this->aliases[$extension])) {
$this->aliases[$extension] = [];
}
$this->aliases[$extension][$alias] = $file;
@ -172,10 +175,11 @@ class CombineAssets
*/
public function resetAliases($extension = null)
{
if ($extension === null)
if ($extension === null) {
$this->aliases = [];
else
} else {
$this->aliases[$extension] = [];
}
return $this;
}
@ -187,12 +191,13 @@ class CombineAssets
*/
public function getAliases($extension = null)
{
if ($extension === null)
if ($extension === null) {
return $this->aliases;
elseif (isset($this->aliases[$extension]))
} elseif (isset($this->aliases[$extension])) {
return $this->aliases[$extension];
else
} else {
return null;
}
}
/**
@ -212,11 +217,13 @@ class CombineAssets
$extension = strtolower($extension);
if (!isset($this->filters[$extension]))
if (!isset($this->filters[$extension])) {
$this->filters[$extension] = [];
}
if ($filter !== null)
if ($filter !== null) {
$this->filters[$extension][] = $filter;
}
return $this;
}
@ -228,10 +235,11 @@ class CombineAssets
*/
public function resetFilters($extension = null)
{
if ($extension === null)
if ($extension === null) {
$this->filters = [];
else
} else {
$this->filters[$extension] = [];
}
return $this;
}
@ -243,12 +251,13 @@ class CombineAssets
*/
public function getFilters($extension = null)
{
if ($extension === null)
if ($extension === null) {
return $this->filters;
elseif (isset($this->filters[$extension]))
} elseif (isset($this->filters[$extension])) {
return $this->filters[$extension];
else
} else {
return null;
}
}
/**
@ -260,14 +269,16 @@ class CombineAssets
*/
protected function prepareRequest(array $assets, $path = null)
{
if (substr($path, -1) != '/')
if (substr($path, -1) != '/') {
$path = $path.'/';
}
$this->path = public_path().$path;
$this->storagePath = storage_path().'/combiner/cms';
if (!is_array($assets))
if (!is_array($assets)) {
$assets = [$assets];
}
/*
* Split assets in to groups.
@ -304,8 +315,7 @@ class CombineAssets
if (count($combineCss) > count($combineJs)) {
$extension = 'css';
$assets = $combineCss;
}
else {
} else {
$extension = 'js';
$assets = $combineJs;
}
@ -315,12 +325,14 @@ class CombineAssets
*/
if ($aliasMap = $this->getAliases($extension)) {
foreach ($assets as $key => $asset) {
if (substr($asset, 0, 1) !== '@')
if (substr($asset, 0, 1) !== '@') {
continue;
}
$_asset = substr($asset, 1);
if (isset($aliasMap[$_asset]))
if (isset($aliasMap[$_asset])) {
$assets[$key] = $aliasMap[$_asset];
}
}
}
@ -358,10 +370,11 @@ class CombineAssets
$combineAction = 'Cms\Classes\Controller@combine';
$actionExists = Route::getRoutes()->getByAction($combineAction) !== null;
if ($actionExists)
if ($actionExists) {
return URL::action($combineAction, [$outputFilename], false);
else
} else {
return Request::getBasePath().'/combine/'.$outputFilename;
}
}
/**
@ -408,8 +421,9 @@ class CombineAssets
$path = $baseUri.'/combine';
}
if (strpos($path, '/') === 0)
if (strpos($path, '/') === 0) {
$path = substr($path, 1);
}
$path = str_replace('.', '-', $path).'/';
return $path;
@ -426,8 +440,9 @@ class CombineAssets
{
$cacheId = 'combiner.'.$cacheId;
if (Cache::has($cacheId))
if (Cache::has($cacheId)) {
return false;
}
$this->putCacheIndex($cacheId);
Cache::forever($cacheId, serialize($cacheInfo));
@ -443,8 +458,9 @@ class CombineAssets
{
$cacheId = 'combiner.'.$cacheId;
if (!Cache::has($cacheId))
if (!Cache::has($cacheId)) {
return false;
}
return unserialize(Cache::get($cacheId));
}
@ -465,8 +481,9 @@ class CombineAssets
*/
public static function resetCache()
{
if (!Cache::has('combiner.index'))
if (!Cache::has('combiner.index')) {
return;
}
$index = unserialize(Cache::get('combiner.index'));
foreach ($index as $cacheId) {
@ -486,16 +503,17 @@ class CombineAssets
{
$index = [];
if (Cache::has('combiner.index'))
if (Cache::has('combiner.index')) {
$index = unserialize(Cache::get('combiner.index'));
}
if (in_array($cacheId, $index))
if (in_array($cacheId, $index)) {
return false;
}
$index[] = $cacheId;
Cache::forever('combiner.index', serialize($index));
return true;
}
}

View File

@ -113,19 +113,29 @@ abstract class ComponentBase extends Extendable
/**
* Executed when this component is first initialized, before AJAX requests.
*/
public function init() {}
public function onInit() {} // Deprecated: Remove ithis line if year >= 2015
public function init()
{
}
// @deprecated: Remove this line if year >= 2015
public function onInit()
{
}
/**
* Executed when this component is bound to a page or layout, part of
* the page life cycle.
*/
public function onRun() {}
public function onRun()
{
}
/**
* Executed when this component is rendered on a page or layout.
*/
public function onRender() {}
public function onRender()
{
}
/**
* Dynamically handle calls into the controller instance.
@ -135,11 +145,13 @@ abstract class ComponentBase extends Extendable
*/
public function __call($method, $parameters)
{
if (method_exists($this, $method))
if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], $parameters);
}
if (method_exists($this->controller, $method))
if (method_exists($this->controller, $method)) {
return call_user_func_array([$this->controller, $method], $parameters);
}
throw new CmsException(Lang::get('cms::lang.component.method_not_found', [
'name' => get_class($this),
@ -161,12 +173,13 @@ abstract class ComponentBase extends Extendable
* @param $default A default value to return if no value is found.
* @return string
*/
public function propertyOrParam($name, $default = null)
public function propertyOrParam($name, $default = null)
{
$value = $this->property($name, $default);
if (substr($value, 0, 1) == ':')
if (substr($value, 0, 1) == ':') {
return $this->param(substr($value, 1), $default);
}
return $value;
}
@ -202,5 +215,4 @@ abstract class ComponentBase extends Extendable
// return $this->pageUrl($page, $params);
// }
}

View File

@ -38,7 +38,9 @@ class ComponentHelpers
];
foreach ($params as $name => $value) {
if (isset($property[$name])) continue;
if (isset($property[$name])) {
continue;
}
$property[$name] = $value;
}
@ -47,7 +49,9 @@ class ComponentHelpers
*/
$translate = ['title', 'description'];
foreach ($property as $name => $value) {
if (!in_array($name, $translate)) continue;
if (!in_array($name, $translate)) {
continue;
}
$property[$name] = Lang::get($value);
}
@ -69,8 +73,9 @@ class ComponentHelpers
$result['oc.alias'] = $component->alias;
$properties = $component->defineProperties();
foreach ($properties as $name => $params)
foreach ($properties as $name => $params) {
$result[$name] = $component->property($name);
}
return json_encode($result);
}

View File

@ -61,8 +61,9 @@ class ComponentManager
foreach ($plugins as $plugin) {
$components = $plugin->registerComponents();
if (!is_array($components))
if (!is_array($components)) {
continue;
}
foreach ($components as $className => $code) {
$this->registerComponent($className, $code, $plugin);
@ -91,23 +92,31 @@ class ComponentManager
*/
public function registerComponent($className, $code = null, $plugin = null)
{
if (!$this->classMap)
if (!$this->classMap) {
$this->classMap = [];
}
if (!$this->codeMap)
if (!$this->codeMap) {
$this->codeMap = [];
}
if (!$code)
if (!$code) {
$code = Str::getClassId($className);
}
if ($code == 'viewBag' && $className != 'Cms\Classes\ViewBag')
throw new SystemException(sprintf('The component code viewBag is reserved. Please use another code for the component class %s.', $className));
if ($code == 'viewBag' && $className != 'Cms\Classes\ViewBag') {
throw new SystemException(sprintf(
'The component code viewBag is reserved. Please use another code for the component class %s.',
$className
));
}
$className = Str::normalizeClassName($className);
$this->codeMap[$code] = $className;
$this->classMap[$className] = $code;
if ($plugin !== null)
if ($plugin !== null) {
$this->pluginMap[$className] = $plugin;
}
}
/**
@ -116,8 +125,9 @@ class ComponentManager
*/
public function listComponents()
{
if ($this->codeMap === null)
if ($this->codeMap === null) {
$this->loadComponents();
}
return $this->codeMap;
}
@ -128,8 +138,9 @@ class ComponentManager
*/
public function listComponentDetails()
{
if ($this->detailsCache !== null)
if ($this->detailsCache !== null) {
return $this->detailsCache;
}
$details = [];
foreach ($this->listComponents() as $componentAlias => $componentClass) {
@ -148,12 +159,14 @@ class ComponentManager
{
$codes = $this->listComponents();
if (isset($codes[$name]))
if (isset($codes[$name])) {
return $codes[$name];
}
$name = Str::normalizeClassName($name);
if (isset($this->classMap[$name]))
if (isset($this->classMap[$name])) {
return $name;
}
return null;
}
@ -166,8 +179,9 @@ class ComponentManager
public function hasComponent($name)
{
$className = $this->resolve($name);
if (!$className)
if (!$className) {
return false;
}
return isset($this->classMap[$className]);
}
@ -182,11 +196,19 @@ class ComponentManager
public function makeComponent($name, $cmsObject = null, $properties = [])
{
$className = $this->resolve($name);
if (!$className)
throw new SystemException(sprintf('Class name is not registered for the component %s. Check the component plugin.', $name));
if (!$className) {
throw new SystemException(sprintf(
'Class name is not registered for the component %s. Check the component plugin.',
$name
));
}
if (!class_exists($className))
throw new SystemException(sprintf('Component class not found %s.Check the component plugin.', $className));
if (!class_exists($className)) {
throw new SystemException(sprintf(
'Component class not found %s.Check the component plugin.',
$className
));
}
$component = new $className($cmsObject, $properties);
$component->name = $name;
@ -202,8 +224,9 @@ class ComponentManager
public function findComponentPlugin($component)
{
$className = Str::normalizeClassName(get_class($component));
if (isset($this->pluginMap[$className]))
if (isset($this->pluginMap[$className])) {
return $this->pluginMap[$className];
}
return null;
}

View File

@ -59,8 +59,9 @@ class ComponentPartial extends CmsObject
if (!File::isFile($path)) {
$sharedDir = dirname($component->getPath()).'/partials';
$sharedPath = $sharedDir.'/'.$fileName;
if (File::isFile($sharedPath))
if (File::isFile($sharedPath)) {
return $sharedPath;
}
}
return $path;

View File

@ -37,8 +37,9 @@ class Content extends CmsCompoundObject
*/
public static function load($theme, $fileName)
{
if ($obj = parent::load($theme, $fileName))
if ($obj = parent::load($theme, $fileName)) {
$obj->parsedMarkup = $obj->parseMarkup();
}
return $obj;
}
@ -70,8 +71,9 @@ class Content extends CmsCompoundObject
{
$result = $this->markup;
if (strtolower(File::extension($this->fileName)) == 'md')
if (strtolower(File::extension($this->fileName)) == 'md') {
$result = Markdown::parse($this->markup);
}
return $result;
}

View File

@ -107,8 +107,9 @@ class Controller extends BaseController
public function __construct($theme = null)
{
$this->theme = $theme ? $theme : Theme::getActiveTheme();
if (!$this->theme)
if (!$this->theme) {
throw new CmsException(Lang::get('cms::lang.theme.active.not_found'));
}
$this->assetPath = Config::get('cms.themesDir').'/'.$this->theme->getDirName();
$this->router = new Router($this->theme);
@ -137,29 +138,34 @@ class Controller extends BaseController
*/
public function run($url = '/')
{
if ($url === null)
if ($url === null) {
$url = Request::path();
}
if (!strlen($url))
if (!strlen($url)) {
$url = '/';
}
/*
* Handle hidden pages
*/
$page = $this->router->findByUrl($url);
if ($page && $page->hidden) {
if (!BackendAuth::getUser())
if (!BackendAuth::getUser()) {
$page = null;
}
}
/*
* Extensibility
*/
if ($event = $this->fireEvent('page.beforeDisplay', [$url, $page], true))
if ($event = $this->fireEvent('page.beforeDisplay', [$url, $page], true)) {
return $event;
}
if ($event = Event::fire('cms.page.beforeDisplay', [$this, $url, $page], true))
if ($event = Event::fire('cms.page.beforeDisplay', [$this, $url, $page], true)) {
return $event;
}
/*
* If the page was not found, render the 404 page - either provided by the theme or the built-in one.
@ -168,11 +174,13 @@ class Controller extends BaseController
$this->setStatusCode(404);
// Log the 404 request
if (!App::runningUnitTests())
if (!App::runningUnitTests()) {
RequestLog::add();
}
if (!$page = $this->router->findByUrl('/404'))
if (!$page = $this->router->findByUrl('/404')) {
return Response::make(View::make('cms::404'), $this->statusCode);
}
}
$this->page = $page;
@ -181,10 +189,11 @@ class Controller extends BaseController
* If the page doesn't refer any layout, create the fallback layout.
* Otherwise load the layout specified in the page.
*/
if (!$page->layout)
if (!$page->layout) {
$layout = Layout::initFallback($this->theme);
elseif (($layout = Layout::loadCached($this->theme, $page->layout)) === null)
} elseif (($layout = Layout::loadCached($this->theme, $page->layout)) === null) {
throw new CmsException(Lang::get('cms::lang.layout.not_found', ['name'=>$page->layout]));
}
$this->layout = $layout;
@ -223,29 +232,38 @@ class Controller extends BaseController
/*
* Extensibility
*/
if ($event = $this->fireEvent('page.init', [$url, $page], true))
if ($event = $this->fireEvent('page.init', [$url, $page], true)) {
return $event;
}
if ($event = Event::fire('cms.page.init', [$this, $url, $page], true))
if ($event = Event::fire('cms.page.init', [$this, $url, $page], true)) {
return $event;
}
/*
* Execute AJAX event
*/
if ($ajaxResponse = $this->execAjaxHandlers())
if ($ajaxResponse = $this->execAjaxHandlers()) {
return $ajaxResponse;
}
/*
* Execute postback handler
*/
if (($handler = post('_handler')) && ($handlerResponse = $this->runAjaxHandler($handler)) && $handlerResponse !== true)
if (
($handler = post('_handler')) &&
($handlerResponse = $this->runAjaxHandler($handler)) &&
$handlerResponse !== true
) {
return $handlerResponse;
}
/*
* Execute page lifecycle
*/
if ($cycleResponse = $this->execPageCycle())
if ($cycleResponse = $this->execPageCycle()) {
return $cycleResponse;
}
/*
* Render the page
@ -268,14 +286,17 @@ class Controller extends BaseController
/*
* Extensibility
*/
if ($event = $this->fireEvent('page.display', [$url, $page], true))
if ($event = $this->fireEvent('page.display', [$url, $page], true)) {
return $event;
}
if ($event = Event::fire('cms.page.display', [$this, $url, $page], true))
if ($event = Event::fire('cms.page.display', [$this, $url, $page], true)) {
return $event;
}
if (!is_string($result))
if (!is_string($result)) {
return $result;
}
return Response::make($result, $this->statusCode);
}
@ -295,15 +316,17 @@ class Controller extends BaseController
'auto_reload' => true,
'debug' => $isDebugMode,
];
if (!Config::get('cms.twigNoCache'))
if (!Config::get('cms.twigNoCache')) {
$options['cache'] = storage_path().'/twig';
}
$this->twig = new Twig_Environment($this->loader, $options);
$this->twig->addExtension(new CmsTwigExtension($this));
$this->twig->addExtension(new SystemTwigExtension);
if ($isDebugMode)
if ($isDebugMode) {
$this->twig->addExtension(new DebugExtension($this));
}
}
/**
@ -335,13 +358,15 @@ class Controller extends BaseController
{
if (!$this->layout->isFallBack()) {
foreach ($this->layout->settings['components'] as $component => $properties) {
list($name, $alias) = strpos($component, ' ') ? explode(' ', $component) : array($component, $component);
list($name, $alias) = strpos($component, ' ') ?
explode(' ', $component) : array($component, $component);
$this->addComponent($name, $alias, $properties, true);
}
}
foreach ($this->page->settings['components'] as $component => $properties) {
list($name, $alias) = strpos($component, ' ') ? explode(' ', $component) : array($component, $component);
list($name, $alias) = strpos($component, ' ') ?
explode(' ', $component) : array($component, $component);
$this->addComponent($name, $alias, $properties);
}
}
@ -359,15 +384,16 @@ class Controller extends BaseController
$manager = ComponentManager::instance();
if ($addToLayout) {
if (!$componentObj = $manager->makeComponent($name, $this->layoutObj, $properties))
if (!$componentObj = $manager->makeComponent($name, $this->layoutObj, $properties)) {
throw new CmsException(Lang::get('cms::lang.component.not_found', ['name'=>$name]));
}
$componentObj->alias = $alias;
$this->vars[$alias] = $this->layout->components[$alias] = $componentObj;
}
else {
if (!$componentObj = $manager->makeComponent($name, $this->pageObj, $properties))
} else {
if (!$componentObj = $manager->makeComponent($name, $this->pageObj, $properties)) {
throw new CmsException(Lang::get('cms::lang.component.not_found', ['name'=>$name]));
}
$componentObj->alias = $alias;
$this->vars[$alias] = $this->page->components[$alias] = $componentObj;
@ -389,8 +415,9 @@ class Controller extends BaseController
/*
* Validate the handler name
*/
if (!preg_match('/^(?:\w+\:{2})?on[A-Z]{1}[\w+]*$/', $handler))
if (!preg_match('/^(?:\w+\:{2})?on[A-Z]{1}[\w+]*$/', $handler)) {
throw new CmsException(Lang::get('cms::lang.ajax_handler.invalid_name', ['name'=>$handler]));
}
/*
* Validate the handler partial list
@ -399,11 +426,11 @@ class Controller extends BaseController
$partialList = explode('&', $partialList);
foreach ($partialList as $partial) {
if (!preg_match('/^(?:\w+\:{2}|@)?[a-z0-9\_\-\.\/]+$/i', $partial))
if (!preg_match('/^(?:\w+\:{2}|@)?[a-z0-9\_\-\.\/]+$/i', $partial)) {
throw new CmsException(Lang::get('cms::lang.partial.invalid_name', ['name'=>$partial]));
}
}
}
else {
} else {
$partialList = [];
}
@ -412,23 +439,26 @@ class Controller extends BaseController
/*
* Execute the handler
*/
if (!$result = $this->runAjaxHandler($handler))
if (!$result = $this->runAjaxHandler($handler)) {
throw new CmsException(Lang::get('cms::lang.ajax_handler.not_found', ['name'=>$handler]));
}
/*
* If the handler returned an array, we should add it to output for rendering.
* If it is a string, add it to the array with the key "result".
*/
if (is_array($result))
if (is_array($result)) {
$responseContents = array_merge($responseContents, $result);
elseif (is_string($result))
} elseif (is_string($result)) {
$responseContents['result'] = $result;
}
/*
* Render partials and return the response as array that will be converted to JSON automatically.
*/
foreach ($partialList as $partial)
foreach ($partialList as $partial) {
$responseContents[$partial] = $this->renderPartial($partial);
}
/*
* If the handler returned a redirect, process it so framework.js knows to redirect
@ -439,27 +469,28 @@ class Controller extends BaseController
}
return Response::make()->setContent($responseContents);
}
catch (ValidationException $ex) {
} catch (ValidationException $ex) {
/*
* Handle validation errors
*/
$responseContents['X_OCTOBER_ERROR_FIELDS'] = $ex->getFields();
$responseContents['X_OCTOBER_ERROR_MESSAGE'] = $ex->getMessage();
return Response::make($responseContents, 406);
}
catch (ApplicationException $ex) {
} catch (ApplicationException $ex) {
return Response::make($ex->getMessage(), 500);
}
catch (Exception $ex) {
} catch (Exception $ex) {
/*
* Display a "dumbed down" error if custom page is activated
* otherwise display a more detailed error.
*/
if (Config::get('cms.customErrorPage', false))
if (Config::get('cms.customErrorPage', false)) {
return Response::make($ex->getMessage(), 500);
}
return Response::make(sprintf('"%s" on line %s of %s', $ex->getMessage(), $ex->getLine(), $ex->getFile()), 500);
return Response::make(
sprintf('"%s" on line %s of %s', $ex->getMessage(), $ex->getLine(), $ex->getFile()),
500
);
}
}
@ -486,11 +517,10 @@ class Controller extends BaseController
$result = $componentObj->$handlerName();
return ($result) ?: true;
}
}
/*
* Process code section handler
*/
else {
} else {
if (method_exists($this->pageObj, $handler)) {
$result = $this->pageObj->$handler();
return ($result) ?: true;
@ -533,11 +563,13 @@ class Controller extends BaseController
/*
* Extensibility
*/
if ($event = $this->fireEvent('page.start', [], true))
if ($event = $this->fireEvent('page.start', [], true)) {
return $event;
}
if ($event = Event::fire('cms.page.start', [$this], true))
if ($event = Event::fire('cms.page.start', [$this], true)) {
return $event;
}
/*
* Run layout functions
@ -549,7 +581,9 @@ class Controller extends BaseController
|| ($result = $this->layoutObj->onBeforePageStart())) ? $result: null;
CmsException::unmask();
if ($response) return $response;
if ($response) {
return $response;
}
}
/*
@ -561,7 +595,9 @@ class Controller extends BaseController
|| ($result = $this->pageObj->onEnd())) ? $result : null;
CmsException::unmask();
if ($response) return $response;
if ($response) {
return $response;
}
/*
* Run remaining layout functions
@ -575,11 +611,13 @@ class Controller extends BaseController
/*
* Extensibility
*/
if ($event = $this->fireEvent('page.end', [], true))
if ($event = $this->fireEvent('page.end', [], true)) {
return $event;
}
if ($event = Event::fire('cms.page.end', [$this], true))
if ($event = Event::fire('cms.page.end', [$this], true)) {
return $event;
}
return $response;
}
@ -595,11 +633,13 @@ class Controller extends BaseController
/*
* Extensibility
*/
if ($event = $this->fireEvent('page.render', [$contents], true))
if ($event = $this->fireEvent('page.render', [$contents], true)) {
return $event;
}
if ($event = Event::fire('cms.page.render', [$this, $contents], true))
if ($event = Event::fire('cms.page.render', [$this, $contents], true)) {
return $event;
}
return $contents;
}
@ -617,8 +657,9 @@ class Controller extends BaseController
/*
* Alias @ symbol for ::
*/
if (substr($name, 0, 1) == '@')
if (substr($name, 0, 1) == '@') {
$name = '::' . substr($name, 1);
}
/*
* Process Component partial
@ -633,23 +674,23 @@ class Controller extends BaseController
if (!strlen($componentAlias)) {
if ($this->componentContext !== null) {
$componentObj = $this->componentContext;
}
elseif (($componentObj = $this->findComponentByPartial($partialName)) === null) {
if ($throwException)
} elseif (($componentObj = $this->findComponentByPartial($partialName)) === null) {
if ($throwException) {
throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name'=>$name]));
else
} else {
return false;
}
}
}
/*
* Component alias is supplied
*/
else {
} else {
if (($componentObj = $this->findComponentByName($componentAlias)) === null) {
if ($throwException)
if ($throwException) {
throw new CmsException(Lang::get('cms::lang.component.not_found', ['name'=>$componentAlias]));
else
} else {
return false;
}
}
}
@ -667,31 +708,33 @@ class Controller extends BaseController
/*
* Check the component partial
*/
if ($partial === null)
if ($partial === null) {
$partial = ComponentPartial::loadCached($componentObj, $partialName);
}
if ($partial === null) {
if ($throwException)
if ($throwException) {
throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name'=>$name]));
else
} else {
return false;
}
}
/*
* Set context for self access
*/
$this->vars['__SELF__'] = $componentObj;
}
else {
} else {
/*
* Process theme partial
*/
if (($partial = Partial::loadCached($this->theme, $name)) === null) {
if ($throwException)
if ($throwException) {
throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name'=>$name]));
else
} else {
return false;
}
}
}
@ -714,28 +757,29 @@ class Controller extends BaseController
/*
* Extensibility
*/
if ($event = $this->fireEvent('page.beforeRenderContent', [$name], true))
if ($event = $this->fireEvent('page.beforeRenderContent', [$name], true)) {
$content = $event;
elseif ($event = Event::fire('cms.page.beforeRenderContent', [$this, $name], true))
} elseif ($event = Event::fire('cms.page.beforeRenderContent', [$this, $name], true)) {
$content = $event;
/*
* Load content from theme
*/
elseif (($content = Content::loadCached($this->theme, $name)) === null)
} elseif (($content = Content::loadCached($this->theme, $name)) === null) {
throw new CmsException(Lang::get('cms::lang.content.not_found', ['name'=>$name]));
}
$fileContent = $content->parsedMarkup;
/*
* Extensibility
*/
if ($event = $this->fireEvent('page.renderContent', [$name, $fileContent], true))
if ($event = $this->fireEvent('page.renderContent', [$name, $fileContent], true)) {
return $event;
}
if ($event = Event::fire('cms.page.renderContent', [$this, $name, $fileContent], true))
if ($event = Event::fire('cms.page.renderContent', [$this, $name, $fileContent], true)) {
return $event;
}
return $fileContent;
}
@ -749,8 +793,9 @@ class Controller extends BaseController
if ($componentObj = $this->findComponentByName($name)) {
$componentObj->id = uniqid($name);
$componentObj->setProperties(array_merge($componentObj->getProperties(), $parameters));
if ($result = $componentObj->onRender())
if ($result = $componentObj->onRender()) {
return $result;
}
}
return $this->renderPartial($name.'::default', [], false);
@ -823,8 +868,9 @@ class Controller extends BaseController
*/
public function pageUrl($name, $parameters = [], $routePersistence = true, $absolute = true)
{
if (!$name)
if (!$name) {
return null;
}
/*
* Second parameter can act as third
@ -834,14 +880,17 @@ class Controller extends BaseController
$parameters = [];
}
if ($routePersistence)
if ($routePersistence) {
$parameters = array_merge($this->router->getParameters(), $parameters);
}
if (!$url = $this->router->findByFile($name, $parameters))
if (!$url = $this->router->findByFile($name, $parameters)) {
return null;
}
if (substr($url, 0, 1) == '/')
if (substr($url, 0, 1) == '/') {
$url = substr($url, 1);
}
return URL::action('Cms\Classes\Controller@run', ['slug' => $url], $absolute);
}
@ -867,10 +916,11 @@ class Controller extends BaseController
if (is_array($url)) {
$_url = Request::getBaseUrl();
$_url .= CombineAssets::combine($url, $themePath);
}
else {
} else {
$_url = Request::getBasePath().$themePath;
if ($url !== null) $_url .= '/'.$url;
if ($url !== null) {
$_url .= '/'.$url;
}
}
return $_url;
@ -894,19 +944,17 @@ class Controller extends BaseController
*/
public function combine($name)
{
try {
if (!strpos($name, '-'))
throw new CmsException(Lang::get('cms::lang.combiner.not_found', ['name'=>$name]));
$parts = explode('-', $name);
$cacheId = $parts[0];
$combiner = new CombineAssets;
return $combiner->getContents($cacheId);
}
catch (Exception $ex) {
return '/* '.$ex->getMessage().' */';
}
try {
if (!strpos($name, '-')) {
throw new CmsException(Lang::get('cms::lang.combiner.not_found', ['name'=>$name]));
}
$parts = explode('-', $name);
$cacheId = $parts[0];
$combiner = new CombineAssets;
return $combiner->getContents($cacheId);
} catch (Exception $ex) {
return '/* '.$ex->getMessage().' */';
}
}
/**
@ -915,11 +963,13 @@ class Controller extends BaseController
*/
protected function findComponentByName($name)
{
if (isset($this->page->components[$name]))
if (isset($this->page->components[$name])) {
return $this->page->components[$name];
}
if (isset($this->layout->components[$name]))
if (isset($this->layout->components[$name])) {
return $this->layout->components[$name];
}
return null;
}
@ -931,13 +981,15 @@ class Controller extends BaseController
protected function findComponentByHandler($handler)
{
foreach ($this->page->components as $component) {
if (method_exists($component, $handler))
if (method_exists($component, $handler)) {
return $component;
}
}
foreach ($this->layout->components as $component) {
if (method_exists($component, $handler))
if (method_exists($component, $handler)) {
return $component;
}
}
return null;
@ -951,20 +1003,24 @@ class Controller extends BaseController
{
foreach ($this->page->components as $component) {
$fileName = ComponentPartial::getFilePath($component, $partial);
if (!strlen(File::extension($fileName)))
if (!strlen(File::extension($fileName))) {
$fileName .= '.htm';
}
if (File::isFile($fileName))
if (File::isFile($fileName)) {
return $component;
}
}
foreach ($this->layout->components as $component) {
$fileName = ComponentPartial::getFilePath($component, $partial);
if (!strlen(File::extension($fileName)))
if (!strlen(File::extension($fileName))) {
$fileName .= '.htm';
}
if (File::isFile($fileName))
if (File::isFile($fileName)) {
return $component;
}
}
return null;
@ -985,7 +1041,10 @@ class Controller extends BaseController
// if (($page = Page::loadCached($theme, $page)) && isset($page->settings['components'])) {
// foreach ($page->settings['components'] as $component => $properties) {
// list($name, $alias) = strpos($component, ' ') ? explode(' ', $component) : array($component, $component);
// list($name, $alias) = strpos($component, ' ') ?
// explode(' ', $component) :
// array($component, $component)
// ;
// if ($manager->resolve($name) == $class) {
// $componentObj->setProperties($properties);
// $componentObj->alias = $alias;
@ -999,7 +1058,10 @@ class Controller extends BaseController
// $layout = $page->settings['layout'];
// if (($layout = Layout::loadCached($theme, $layout)) && isset($layout->settings['components'])) {
// foreach ($layout->settings['components'] as $component => $properties) {
// list($name, $alias) = strpos($component, ' ') ? explode(' ', $component) : array($component, $component);
// list($name, $alias) = strpos($component, ' ') ?
// explode(' ', $component) :
// array($component, $component)
// ;
// if ($manager->resolve($name) == $class) {
// $componentObj->setProperties($properties);
// $componentObj->alias = $alias;
@ -1011,5 +1073,4 @@ class Controller extends BaseController
// return null;
// }
}

View File

@ -30,8 +30,9 @@ class FileHelper
public static function validateExtension($fileName, $allowedExtensions, $allowEmpty = true)
{
$extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
if (!strlen($extension))
if (!strlen($extension)) {
return $allowEmpty;
}
return in_array($extension, $allowedExtensions);
}
@ -46,19 +47,24 @@ class FileHelper
*/
public static function validatePath($filePath, $maxNesting = 2)
{
if (strpos($filePath, '..') !== false)
if (strpos($filePath, '..') !== false) {
return false;
}
if (strpos($filePath, './') !== false || strpos($filePath, '//') !== false)
if (strpos($filePath, './') !== false || strpos($filePath, '//') !== false) {
return false;
}
$segments = explode('/', $filePath);
if ($maxNesting !== null && count($segments) > $maxNesting)
if ($maxNesting !== null && count($segments) > $maxNesting) {
return false;
}
foreach ($segments as $segment)
if (!self::validateName($segment))
foreach ($segments as $segment) {
if (!self::validateName($segment)) {
return false;
}
}
return true;
}
@ -74,20 +80,23 @@ class FileHelper
$content = null;
$sections = [];
foreach ($data as $key=>$value) {
foreach ($data as $key => $value) {
if (is_array($value)) {
if ($level == 1)
if ($level == 1) {
$sections[$key] = self::formatIniString($value, $level+1);
else {
foreach ($value as $val)
} else {
foreach ($value as $val) {
$content .= $key.'[] = "'.self::escapeIniString($val).'"'.PHP_EOL;
}
}
} elseif (strlen($value))
} elseif (strlen($value)) {
$content .= $key.' = "'.self::escapeIniString($value).'"'.PHP_EOL;
}
}
foreach ($sections as $key=>$section)
foreach ($sections as $key => $section) {
$content .= PHP_EOL.'['.$key.']'.PHP_EOL.$section.PHP_EOL;
}
return trim($content);
}

View File

@ -10,7 +10,9 @@ class Layout extends CmsCompoundObject
{
const FALLBACK_FILE_NAME = 'fallback';
protected function parseSettings() {}
protected function parseSettings()
{
}
/**
* Returns the directory name corresponding to the object type.

View File

@ -12,5 +12,7 @@ class LayoutCode extends CodeBase
* This event is triggered after the layout components are executed,
* but before the page's onStart event.
*/
public function onBeforePageStart() {}
public function onBeforePageStart()
{
}
}

View File

@ -31,7 +31,9 @@ class Page extends CmsCompoundObject
];
}
protected function parseSettings() {}
protected function parseSettings()
{
}
/**
* Returns the directory name corresponding to the object type.
@ -59,8 +61,9 @@ class Page extends CmsCompoundObject
*/
public function getLayoutOptions()
{
if (!($theme = Theme::getEditTheme()))
if (!($theme = Theme::getEditTheme())) {
throw new ApplicationException(Lang::get('cms::lang.theme.edit.not_found'));
}
$layouts = Layout::listInTheme($theme, true);
$result = [];
@ -101,8 +104,9 @@ class Page extends CmsCompoundObject
* request processing.
*/
$controller = Controller::getController();
if (!$controller)
if (!$controller) {
$controller = new Controller;
}
return $controller->pageUrl($page, $params, true, $absolute);
}
@ -119,8 +123,8 @@ class Page extends CmsCompoundObject
* false if omitted.
* - dynamicItems - Boolean value indicating whether the item type could generate new menu items.
* Optional, false if omitted.
* - cmsPages - a list of CMS pages (objects of the Cms\Classes\Page class), if the item type requires a CMS page reference to
* resolve the item URL.
* - cmsPages - a list of CMS pages (objects of the Cms\Classes\Page class), if the item type requires
* a CMS page reference to resolve the item URL.
* @param string $type Specifies the menu item type
* @return array Returns an array
*/
@ -132,8 +136,9 @@ class Page extends CmsCompoundObject
$theme = Theme::getActiveTheme();
$pages = self::listInTheme($theme, true);
foreach ($pages as $page)
foreach ($pages as $page) {
$references[$page->getBaseFileName()] = $page->title;
}
$result = [
'references' => $references,
@ -167,8 +172,9 @@ class Page extends CmsCompoundObject
$result = null;
if ($item->type == 'cms-page') {
if (!$item->reference)
if (!$item->reference) {
return;
}
$pageUrl = self::url($item->reference);

View File

@ -18,7 +18,8 @@ use October\Rain\Router\Helper as RouterHelper;
* add the question mark after its name:
* <pre>/blog/post/:post_id?</pre>
* By default parameters in the middle of the URL are required, for example:
* <pre>/blog/:post_id?/comments - although the :post_id parameter is marked as optional, it will be processed as required.</pre>
* <pre>/blog/:post_id?/comments - although the :post_id parameter is marked as optional,
* it will be processed as required.</pre>
* Optional parameters can have default values which are used as fallback values in case if the real
* parameter value is not presented in the URL. Default values cannot contain the pipe symbols and question marks.
* Specify the default value after the question mark:
@ -74,16 +75,21 @@ class Router
$url = RouterHelper::normalizeUrl($url);
$apiResult = Event::fire('cms.router.beforeRoute', [$url], true);
if ($apiResult !== null)
if ($apiResult !== null) {
return $apiResult;
}
for ($pass = 1; $pass <= 2; $pass++) {
$fileName = null;
$urlList = [];
$cacheable = Config::get('cms.enableRoutesCache') && in_array(Config::get('cache.driver'), ['apc', 'memcached', 'redis', 'array']);
if ($cacheable)
$cacheable = Config::get('cms.enableRoutesCache') && in_array(
Config::get('cache.driver'),
['apc', 'memcached', 'redis', 'array']
);
if ($cacheable) {
$fileName = $this->getCachedUrlFileName($url, $urlList);
}
/*
* Find the page by URL and cache the route
@ -96,8 +102,9 @@ class Router
$fileName = $router->matchedRoute();
if ($cacheable) {
if (!$urlList || !is_array($urlList))
if (!$urlList || !is_array($urlList)) {
$urlList = [];
}
$urlList[$url] = $fileName;
@ -140,8 +147,9 @@ class Router
*/
public function findByFile($fileName, $parameters = [])
{
if (!strlen(File::extension($fileName)))
if (!strlen(File::extension($fileName))) {
$fileName .= '.htm';
}
$router = $this->getRouterObject();
return $router->url($fileName, $parameters);
@ -153,8 +161,9 @@ class Router
*/
protected function getRouterObject()
{
if (self::$routerObj !== null)
if (self::$routerObj !== null) {
return self::$routerObj;
}
/*
* Load up each route rule
@ -178,8 +187,9 @@ class Router
*/
protected function getUrlMap()
{
if (!count(self::$urlMap))
if (!count(self::$urlMap)) {
$this->loadUrlMap();
}
return self::$urlMap;
}
@ -196,10 +206,11 @@ class Router
$key = $this->getCacheKey('page-url-map');
$cacheable = Config::get('cms.enableRoutesCache');
if ($cacheable)
if ($cacheable) {
$cached = Cache::get($key, false);
else
} else {
$cached = false;
}
if (!$cached || ($unserialized = @unserialize($cached)) === false) {
/*
@ -208,15 +219,17 @@ class Router
$pages = $this->theme->listPages();
$map = [];
foreach ($pages as $page) {
if (!$page->url)
if (!$page->url) {
continue;
}
$map[] = ['file' => $page->getFileName(), 'pattern' => $page->url];
}
self::$urlMap = $map;
if ($cacheable)
if ($cacheable) {
Cache::put($key, serialize($map), Config::get('cms.urlCacheTtl', 1));
}
return false;
}
@ -259,8 +272,9 @@ class Router
*/
public function getParameter($name, $default = null)
{
if (isset($this->parameters[$name]) && !empty($this->parameters[$name]))
if (isset($this->parameters[$name]) && !empty($this->parameters[$name])) {
return $this->parameters[$name];
}
return $default;
}
@ -296,8 +310,9 @@ class Router
$urlList = Cache::get($key, false);
if ($urlList && ($urlList = @unserialize($urlList)) && is_array($urlList)) {
if (array_key_exists($url, $urlList))
if (array_key_exists($url, $urlList)) {
return $urlList[$url];
}
}
return null;

View File

@ -33,8 +33,9 @@ class SectionParser
{
$sections = preg_split('/^={2,}\s*/m', $content, -1);
$count = count($sections);
foreach ($sections as &$section)
foreach ($sections as &$section) {
$section = trim($section);
}
$result = [
'settings' => [],
@ -51,13 +52,12 @@ class SectionParser
$result['code'] = preg_replace('/\?\>\s*$/', '', $result['code']);
$result['markup'] = $sections[2];
}
elseif ($count == 2) {
} elseif ($count == 2) {
$result['settings'] = parse_ini_string($sections[0], true);
$result['markup'] = $sections[1];
}
elseif ($count == 1)
} elseif ($count == 1) {
$result['markup'] = $sections[0];
}
return $result;
}
@ -83,12 +83,10 @@ class SectionParser
$result['settings'] = self::adjustLinePosition($content);
$result['code'] = self::calculateLinePosition($content);
$result['markup'] = self::calculateLinePosition($content, 2);
}
elseif ($count == 2) {
} elseif ($count == 2) {
$result['settings'] = self::adjustLinePosition($content);
$result['markup'] = self::calculateLinePosition($content);
}
elseif ($count == 1) {
} elseif ($count == 1) {
$result['markup'] = 1;
}
@ -106,11 +104,13 @@ class SectionParser
$count = 0;
$lines = explode(PHP_EOL, $content);
foreach ($lines as $number => $line) {
if (trim($line) == self::SECTION_SEPARATOR)
if (trim($line) == self::SECTION_SEPARATOR) {
$count++;
}
if ($count == $instance)
if ($count == $instance) {
return static::adjustLinePosition($content, $number);
}
}
return null;
@ -150,10 +150,11 @@ class SectionParser
}
/*
* PHP namespaced line (use x;)
* PHP namespaced line (use x;) {
* Don't increase the line count, it will be rewritten by Cms\Classes\CodeParser
*/
if (preg_match_all('/(use\s+[a-z0-9_\\\\]+;\n?)/mi', $line) == 1) {
if (preg_match_all('/(use\s+[a-z0-9_\\\\]+;
}\n?)/mi', $line) == 1) {
continue;
}

View File

@ -60,8 +60,9 @@ class Theme
*/
public function getPath($dirName = null)
{
if (!$dirName)
if (!$dirName) {
$dirName = $this->getDirName();
}
return base_path().Config::get('cms.themesDir').'/'.$dirName;
}
@ -108,8 +109,9 @@ class Theme
*/
public static function getActiveTheme()
{
if (self::$activeThemeCache !== false)
if (self::$activeThemeCache !== false) {
return self::$activeThemeCache;
}
$activeTheme = Config::get('cms.activeTheme');
@ -119,21 +121,25 @@ class Theme
->pluck('value')
;
if ($dbResult !== null)
if ($dbResult !== null) {
$activeTheme = $dbResult;
}
}
$apiResult = Event::fire('cms.activeTheme', [], true);
if ($apiResult !== null)
if ($apiResult !== null) {
$activeTheme = $apiResult;
}
if (!strlen($activeTheme))
if (!strlen($activeTheme)) {
throw new SystemException(Lang::get('cms::lang.theme.active.not_set'));
}
$theme = new static;
$theme->load($activeTheme);
if (!File::isDirectory($theme->getPath()))
if (!File::isDirectory($theme->getPath())) {
return self::$activeThemeCache = null;
}
return self::$activeThemeCache = $theme;
}
@ -160,24 +166,29 @@ class Theme
*/
public static function getEditTheme()
{
if (self::$editThemeCache !== false)
if (self::$editThemeCache !== false) {
return self::$editThemeCache;
}
$editTheme = Config::get('cms.editTheme');
if (!$editTheme)
if (!$editTheme) {
$editTheme = static::getActiveTheme()->getDirName();
}
$apiResult = Event::fire('cms.editTheme', [], true);
if ($apiResult !== null)
if ($apiResult !== null) {
$editTheme = $apiResult;
}
if (!strlen($editTheme))
if (!strlen($editTheme)) {
throw new SystemException(Lang::get('cms::lang.theme.edit.not_set'));
}
$theme = new static;
$theme->load($editTheme);
if (!File::isDirectory($theme->getPath()))
if (!File::isDirectory($theme->getPath())) {
return self::$editThemeCache = null;
}
return self::$editThemeCache = $theme;
}
@ -195,8 +206,9 @@ class Theme
$result = [];
foreach ($it as $fileinfo) {
if (!$fileinfo->isDir() || $fileinfo->isDot())
if (!$fileinfo->isDir() || $fileinfo->isDot()) {
continue;
}
$theme = new static;
$theme->load($fileinfo->getFilename());
@ -212,12 +224,14 @@ class Theme
*/
public function getConfig()
{
if ($this->configCache !== null)
if ($this->configCache !== null) {
return $this->configCache;
}
$path = $this->getPath().'/theme.yaml';
if (!File::exists($path))
if (!File::exists($path)) {
return $this->configCache = [];
}
return $this->configCache = Yaml::parseFile($path);
}
@ -225,14 +239,16 @@ class Theme
/**
* Returns a value from the theme configuration file by its name.
* @param string $name Specifies the configuration parameter name.
* @param mixed $default Specifies the default value to return in case if the parameter doesn't exist in the configuration file.
* @param mixed $default Specifies the default value to return in case if the parameter
* doesn't exist in the configuration file.
* @return mixed Returns the parameter value or a default value
*/
public function getConfigValue($name, $default = null)
{
$config = $this->getConfig();
if (isset($config[$name]))
if (isset($config[$name])) {
return $config[$name];
}
return $default;
}
@ -246,8 +262,9 @@ class Theme
{
$previewPath = '/assets/images/theme-preview.png';
$path = $this->getPath().$previewPath;
if (!File::exists($path))
if (!File::exists($path)) {
return URL::asset('modules/cms/assets/images/default-theme-preview.png');
}
return URL::asset('themes/'.$this->getDirName().$previewPath);
}

View File

@ -28,8 +28,9 @@ class ViewBag extends ComponentBase
public function __call($method, $parameters)
{
if (array_key_exists($method, $this->properties) && !method_exists($this, $method))
if (array_key_exists($method, $this->properties) && !method_exists($this, $method)) {
return $this->properties[$method];
}
return parent::__call($method, $parameters);
}
@ -38,7 +39,7 @@ class ViewBag extends ComponentBase
{
$result = [];
foreach ($this->properties as $name=>$value) {
foreach ($this->properties as $name => $value) {
$result[$name] = [
'title' => $name,
'type' => 'string'