diff --git a/engine/classes/Action.class.php b/engine/classes/Action.class.php index 610fa430..38c1e06e 100644 --- a/engine/classes/Action.class.php +++ b/engine/classes/Action.class.php @@ -16,73 +16,122 @@ */ /** - * Абстрактный класс экшена + * Абстрактный класс экшена. * + * От этого класса наследуются все экшены в движке. + * Предоставляет базовые метода для работы с параметрами и шаблоном при запросе страницы в браузере. + * + * @package engine + * @since 1.0 */ abstract class Action extends LsObject { - + /** + * Список зарегистрированных евентов + * + * @var array + */ protected $aRegisterEvent=array(); + /** + * Список параметров из URL + *
/action/event/param0/param1/../paramN/
+ * + * @var array + */ protected $aParams=array(); + /** + * Список совпадений по регулярному выражению для евента + * + * @var array + */ protected $aParamsEventMatch=array('event'=>array(),'params'=>array()); + /** + * Объект ядра + * + * @var Engine|null + */ protected $oEngine=null; + /** + * Шаблон экшена + * @see SetTemplate + * @see SetTemplateAction + * + * @var string|null + */ protected $sActionTemplate=null; + /** + * Дефолтный евент + * @see SetDefaultEvent + * + * @var string|null + */ protected $sDefaultEvent=null; + /** + * Текущий евент + * + * @var string|null + */ protected $sCurrentEvent=null; + /** + * Текущий экшен + * + * @var null|string + */ protected $sCurrentAction=null; - + /** * Конструктор * - * @param Engine $oEngine - * @param string $sAction + * @param Engine $oEngine Объект ядра + * @param string $sAction Название экшена */ public function __construct(Engine $oEngine, $sAction) { $this->RegisterEvent(); $this->oEngine=$oEngine; $this->sCurrentAction=$sAction; - $this->aParams=Router::GetParams(); + $this->aParams=Router::GetParams(); } /** * Добавляет евент в экшен * По сути является оберткой для AddEventPreg(), оставлен для простоты и совместимости с прошлыми версиями ядра + * @see AddEventPreg * * @param string $sEventName Название евента * @param string $sEventFunction Какой метод ему соответствует - */ + */ protected function AddEvent($sEventName,$sEventFunction) { $this->AddEventPreg("/^{$sEventName}$/i",$sEventFunction); } - + /** * Добавляет евент в экшен, используя регулярное вырожение для евента и параметров * */ - protected function AddEventPreg() { + protected function AddEventPreg() { $iCountArgs=func_num_args(); if ($iCountArgs<2) { throw new Exception("Incorrect number of arguments when adding events"); } $aEvent=array(); $aEvent['method']=func_get_arg($iCountArgs-1); - if (!method_exists($this,$aEvent['method'])) { + if (!method_exists($this,$aEvent['method'])) { throw new Exception("Method of the event not found: ".$aEvent['method']); } - $aEvent['preg']=func_get_arg(0); + $aEvent['preg']=func_get_arg(0); $aEvent['params_preg']=array(); for ($i=1;$i<$iCountArgs-1;$i++) { $aEvent['params_preg'][]=func_get_arg($i); } - $this->aRegisterEvent[]=$aEvent; + $this->aRegisterEvent[]=$aEvent; } - + /** * Запускает евент на выполнение * Если текущий евент не определен то запускается тот которые определен по умолчанию(default event) * - * @return unknown + * @return mixed */ - public function ExecEvent() { + public function ExecEvent() { $this->sCurrentEvent=Router::GetActionEvent(); if ($this->sCurrentEvent==null) { $this->sCurrentEvent=$this->GetDefaultEvent(); @@ -98,7 +147,7 @@ abstract class Action extends LsObject { } else { continue 2; } - } + } $sCmd='$result=$this->'.$aEvent['method'].'();'; $this->Hook_Run("action_event_".strtolower($this->sCurrentAction)."_before",array('event'=>$this->sCurrentEvent,'params'=>$this->GetParams())); eval($sCmd); @@ -107,17 +156,17 @@ abstract class Action extends LsObject { } } return $this->EventNotFound(); - } - + } + /** * Устанавливает евент по умолчанию * - * @param string $sEvent + * @param string $sEvent Имя евента */ public function SetDefaultEvent($sEvent) { $this->sDefaultEvent=$sEvent; } - + /** * Получает евент по умолчанию * @@ -130,8 +179,8 @@ abstract class Action extends LsObject { /** * Возвращает элементы совпадения по регулярному выражению для евента * - * @param unknown_type $iItem - * @return unknown + * @param int|null $iItem Номер совпадения + * @return string|null */ protected function GetEventMatch($iItem=null) { if ($iItem) { @@ -147,9 +196,9 @@ abstract class Action extends LsObject { /** * Возвращает элементы совпадения по регулярному выражению для параметров евента * - * @param unknown_type $iParamNum - * @param unknown_type $iItem - * @return unknown + * @param int $iParamNum Номер параметра, начинается с нуля + * @param int|null $iItem Номер совпадения, начинается с нуля + * @return string|null */ protected function GetParamEventMatch($iParamNum,$iItem=null) { if (!is_null($iItem)) { @@ -166,40 +215,40 @@ abstract class Action extends LsObject { } } } - + /** * Получает параметр из URL по его номеру, если его нет то null * - * @param unknown_type $iOffset - * @return unknown + * @param int $iOffset Номер параметра, начинается с нуля + * @return mixed */ public function GetParam($iOffset,$default=null) { $iOffset=(int)$iOffset; return isset($this->aParams[$iOffset]) ? $this->aParams[$iOffset] : $default; } - + /** * Получает список параметров из УРЛ * - * @return unknown + * @return array */ - public function GetParams() { + public function GetParams() { return $this->aParams; } - - + + /** - * Установить значение параметра(эмуляция параметра в URL). + * Установить значение параметра(эмуляция параметра в URL). * После установки занова считывает параметры из роутера - для корректной работы * - * @param int $iOffset - по идеи может быть не только числом - * @param unknown_type $value + * @param int $iOffset Номер параметра, но по идеи может быть не только числом + * @param string $value */ - public function SetParam($iOffset,$value) { + public function SetParam($iOffset,$value) { Router::SetParam($iOffset,$value); $this->aParams=Router::GetParams(); } - + /** * Устанавливает какой шаблон выводить * @@ -208,7 +257,7 @@ abstract class Action extends LsObject { protected function SetTemplate($sTemplate) { $this->sActionTemplate=$sTemplate; } - + /** * Устанавливает какой шаблон выводить * @@ -231,72 +280,75 @@ abstract class Action extends LsObject { } } } - $this->sActionTemplate = $sActionTemplatePath; + $this->sActionTemplate = $sActionTemplatePath; } - + /** * Получить шаблон * Если шаблон не определен то возвращаем дефолтный шаблон евента: action/{Action}.{event}.tpl * - * @return unknown + * @return string */ public function GetTemplate() { if (is_null($this->sActionTemplate)) { - $this->SetTemplateAction($this->sCurrentEvent); + $this->SetTemplateAction($this->sCurrentEvent); } return $this->sActionTemplate; } - + /** * Получить каталог с шаблонами экшена(совпадает с именем класса) + * @see Router::GetActionClass * - * @return unknown + * @return string */ public function GetActionClass() { return Router::GetActionClass(); } - + /** * Вызывается в том случаи если не найден евент который запросили через URL - * По дефолту происходит перекидывание на страницу ошибки, это можно переопределить в наследнике, а в ряде случаев и необходимо :) Для примера смотри экшен Profile + * По дефолту происходит перекидывание на страницу ошибки, это можно переопределить в наследнике + * @see Router::Action * - * @return unknown + * @return string */ - protected function EventNotFound() { + protected function EventNotFound() { return Router::Action('error','404'); } - + /** * Выполняется при завершение экшена, после вызова основного евента * */ public function EventShutdown() { - + } - + /** * Ставим хук на вызов неизвестного метода и считаем что хотели вызвать метод какого либо модуля + * @see Engine::_CallModule * - * @param string $sName - * @param array $aArgs - * @return unknown + * @param string $sName Имя метода + * @param array $aArgs Аргументы + * @return mixed */ public function __call($sName,$aArgs) { return $this->oEngine->_CallModule($sName,$aArgs); } - + /** * Абстрактный метод инициализации экшена * */ abstract public function Init(); - + /** * Абстрактный метод регистрации евентов. - * В нём необходимо вызывать метод AddEvent($sEventName,$sEventFunction) + * В нём необходимо вызывать метод AddEvent($sEventName,$sEventFunction) * */ - abstract protected function RegisterEvent(); - + abstract protected function RegisterEvent(); + } ?> \ No newline at end of file diff --git a/engine/classes/Engine.class.php b/engine/classes/Engine.class.php index 9ebc55e8..777562e2 100644 --- a/engine/classes/Engine.class.php +++ b/engine/classes/Engine.class.php @@ -36,114 +36,165 @@ require_once("ManyToManyRelation.class.php"); /** - * Основной класс движка, который позволяет напрямую обращаться к любому модулю + * Основной класс движка. Ядро. * + * Производит инициализацию плагинов, модулей, хуков. + * Через этот класс происходит выполнение методов всех модулей, которые вызываются как
$this->Module_Method();
+ * Также отвечает за автозагрузку остальных классов движка. + * + * В произвольном месте (не в классах движка у которых нет обработки метода __call() на выполнение модулей) метод модуля можно вызвать так: + *
+ * Engine::getInstance()->Module_Method();
+ * 
+ * + * @package engine + * @since 1.0 */ class Engine extends LsObject { - + /** * Имя плагина * @var int */ const CI_PLUGIN = 1; - + /** * Имя экшна * @var int */ const CI_ACTION = 2; - + /** * Имя модуля * @var int */ const CI_MODULE = 4; - + /** * Имя сущности * @var int */ const CI_ENTITY = 8; - + /** * Имя маппера * @var int */ const CI_MAPPER = 16; - + /** * Имя метода * @var int */ const CI_METHOD = 32; - + /** * Имя хука * @var int */ const CI_HOOK = 64; - + /** * Имя класс наследования * @var int */ const CI_INHERIT = 128; - - /** + + /** * Имя блока * @var int */ const CI_BLOCK = 256; - + /** * Префикс плагина * @var int */ const CI_PPREFIX = 8192; - + /** * Разобранный класс наследования * @var int */ const CI_INHERITS = 16384; - + /** * Путь к файлу класса * @var int */ const CI_CLASSPATH = 32768; - + /** * Все свойства класса * @var int */ const CI_ALL = 65535; - + /** * Свойства по-умолчанию * CI_ALL ^ (CI_CLASSPATH | CI_INHERITS | CI_PPREFIX) * @var int */ const CI_DEFAULT = 8191; - + /** * Объекты * CI_ACTION | CI_MAPPER | CI_HOOK | CI_PLUGIN | CI_ACTION | CI_MODULE | CI_ENTITY | CI_BLOCK * @var int */ const CI_OBJECT = 351 ; - - static protected $oInstance=null; - - protected $aModules=array(); - protected $aPlugins=array(); - protected $aConfigModule; - public $iTimeLoadModule=0; - protected $iTimeInit=null; - - + /** - * При создании объекта делаем инициализацию + * Текущий экземпляр движка, используется для синглтона. + * @see getInstance использование синглтона + * + * @var Engine + */ + static protected $oInstance=null; + /** + * Список загруженных модулей + * + * @var array + */ + protected $aModules=array(); + /** + * Список загруженных плагинов + * + * @var array + */ + protected $aPlugins=array(); + /** + * Содержит конфиг модулей. + * Используется для получания списка модулей для авто-загрузки. Остальные модули загружаются при первом обращении. + * В конфиге определен так: + *
+	 * $config['module']['autoLoad'] = array('Hook','Cache','Security','Session','Lang','Message','User');
+	 * 
+ * + * @var array + */ + protected $aConfigModule; + /** + * Время загрузки модулей в микросекундах + * + * @var int + */ + public $iTimeLoadModule=0; + /** + * Текущее время в микросекундах на момент инициализации ядра(движка). + * Определается так: + *
+	 * $this->iTimeInit=microtime(true);
+	 * 
+ * + * @var int|null + */ + protected $iTimeInit=null; + + + /** + * Вызывается при создании объекта ядра. + * Устанавливает время старта инициализации и обрабатывает входные параметры PHP * */ protected function __construct() { @@ -155,9 +206,15 @@ class Engine extends LsObject { func_stripslashes($_COOKIE); } } - + /** - * Ограничиваем объект только одним экземпляром + * Ограничиваем объект только одним экземпляром. + * Функционал синглтона. + * + * Используется так: + *
+	 * Engine::getInstance()->Module_Method();
+	 * 
* * @return Engine */ @@ -169,9 +226,9 @@ class Engine extends LsObject { return self::$oInstance; } } - + /** - * Инициализация + * Инициализация ядра движка * */ public function Init() { @@ -201,7 +258,8 @@ class Engine extends LsObject { $this->Hook_Run('engine_init_complete'); } /** - * Завершение работы модуля + * Завершение работы движка + * Завершает все модули. * */ public function Shutdown() { @@ -215,28 +273,28 @@ class Engine extends LsObject { foreach ($this->aModules as $oModule) { if(!$oModule->isInit()) { /** - * Замеряем время инициализации модуля - */ + * Замеряем время инициализации модуля + */ $oProfiler=ProfilerSimple::getInstance(); $iTimeId=$oProfiler->Start('InitModule',get_class($oModule)); - + $this->InitModule($oModule); $oProfiler->Stop($iTimeId); } } } - + /** * Инициализирует модуль - * - * @param unknown_type $oModule - * @param unknown_type $bHookParent + * + * @param Module $oModule Объект модуля + * @param bool $bHookParent Вызывает хук на родительском модуле, от которого наследуется текущий */ protected function InitModule($oModule, $bHookParent = true){ $sOrigClassName = $sClassName = get_class($oModule); $bRunHooks = false; - + if($this->isInitModule('ModuleHook')){ $bRunHooks = true; if($bHookParent){ @@ -265,12 +323,12 @@ class Engine extends LsObject { $this->Hook_Run($sHookPrefix.'after'); } } - + /** * Проверяет модуль на инициализацию * - * @param unknown_type $sModuleClass - * @return unknown + * @param string $sModuleClass Класс модуля + * @return bool */ public function isInitModule($sModuleClass) { if(!in_array($sModuleClass,array('ModulePlugin','ModuleHook'))) { @@ -281,7 +339,7 @@ class Engine extends LsObject { } return false; } - + /** * Завершаем работу всех модулей * @@ -299,33 +357,33 @@ class Engine extends LsObject { $oProfiler->Stop($iTimeId); } } - + /** * Выполняет загрузку модуля по его названию * - * @param string $sModuleClass - * @param bool $bInit - инициализировать модуль или нет + * @param string $sModuleClass Класс модуля + * @param bool $bInit Инициализировать модуль или нет * @return Module */ public function LoadModule($sModuleClass,$bInit=false) { $tm1=microtime(true); - - /** + + /** * Создаем объект модуля - */ + */ $oModule=new $sModuleClass($this); $this->aModules[$sModuleClass]=$oModule; if ($bInit or $sModuleClass=='ModuleCache') { $this->InitModule($oModule); - } + } $tm2=microtime(true); $this->iTimeLoadModule+=$tm2-$tm1; dump("load $sModuleClass - \t\t".($tm2-$tm1).""); return $oModule; } - + /** - * Загружает все используемые модули и передает им в конструктор ядро + * Загружает модули из авто-загрузки и передает им в конструктор ядро * */ protected function LoadModules() { @@ -333,7 +391,7 @@ class Engine extends LsObject { foreach ($this->aConfigModule['autoLoad'] as $sModuleName) { $sModuleClass='Module'.$sModuleName; if(!in_array($sModuleName,array('Plugin','Hook'))) $sModuleClass=$this->Plugin_GetDelegate('module',$sModuleClass); - + if (!isset($this->aModules[$sModuleClass])) { $this->LoadModule($sModuleClass); } @@ -353,7 +411,7 @@ class Engine extends LsObject { protected function InitHooks() { $sDirHooks=Config::Get('path.root.server').'/classes/hooks/'; $aFiles=glob($sDirHooks.'Hook*.class.php'); - + if($aFiles and count($aFiles)) { foreach ($aFiles as $sFile) { if (preg_match("/Hook([^_]+)\.class\.php$/i",basename($sFile),$aMatch)) { @@ -364,23 +422,21 @@ class Engine extends LsObject { } } } - + /** * Подгружаем хуки активных плагинов */ $this->InitPluginHooks(); } - + /** * Инициализация хуков активированных плагинов * */ protected function InitPluginHooks() { if($aPluginList = func_list_plugins()) { - - $aFiles=array(); $sDirHooks=Config::Get('path.root.server').'/plugins/'; - + foreach ($aPluginList as $sPluginName) { $aFiles=glob($sDirHooks.$sPluginName.'/classes/hooks/Hook*.class.php'); if($aFiles and count($aFiles)) { @@ -412,42 +468,43 @@ class Engine extends LsObject { } } } - + /** - * Инициализация активированных плагинов + * Инициализация активированных(загруженных) плагинов * */ protected function InitPlugins() { - foreach ($this->aPlugins as $oPlugin) { + foreach ($this->aPlugins as $oPlugin) { $oPlugin->Init(); } } - + /** * Возвращает список активных плагинов * - * @return unknown + * @return array */ public function GetPlugins() { return $this->aPlugins; } - + /** * Проверяет файл на существование, если используется кеширование memcache то кеширует результат работы * - * @param string $sFile - * @return mixed + * @param string $sFile Полный путь до файла + * @param int $iTime Время жизни кеша + * @return bool */ public function isFileExists($sFile,$iTime=3600) { // пока так return file_exists($sFile); - + if( - !$this->isInit('cache') - || !Config::Get('sys.cache.use') - || Config::Get('sys.cache.type') != 'memory' + !$this->isInit('cache') + || !Config::Get('sys.cache.use') + || Config::Get('sys.cache.type') != 'memory' ){ - return file_exists($sFile); + return file_exists($sFile); } if (false === ($data = $this->Cache_Get("file_exists_{$sFile}"))) { $data=file_exists($sFile); @@ -458,28 +515,33 @@ class Engine extends LsObject { /** * Вызывает метод нужного модуля * - * @param string $sName - * @param array $aArgs - * @return unknown + * @param string $sName Название метода в полном виде. + * Например
Module_Method
+ * @param array $aArgs Список аргументов + * @return mixed */ public function _CallModule($sName,$aArgs) { list($oModule,$sModuleName,$sMethod)=$this->GetModule($sName); - + if (!method_exists($oModule,$sMethod)) { // comment for ORM testing //throw new Exception("The module has no required method: ".$sModuleName.'->'.$sMethod.'()'); } - + /** + * Замеряем время выполнения метода + */ $oProfiler=ProfilerSimple::getInstance(); $iTimeId=$oProfiler->Start('callModule',$sModuleName.'->'.$sMethod.'()'); - + $sModuleName=strtolower($sModuleName); $aResultHook=array(); if (!in_array($sModuleName,array('plugin','hook'))) { $aResultHook=$this->_CallModule('Hook_Run',array('module_'.$sModuleName.'_'.strtolower($sMethod).'_before',&$aArgs)); - } - - if (array_key_exists('delegate_result',$aResultHook)) { + } + /** + * Хук может делегировать результат выполнения метода модуля, сам метод при этом не выполняется, происходит только подмена результата + */ + if (array_key_exists('delegate_result',$aResultHook)) { $result=$aResultHook['delegate_result']; } else { $aArgsRef=array(); @@ -488,11 +550,11 @@ class Engine extends LsObject { } $result=call_user_func_array(array($oModule,$sMethod),$aArgsRef); } - + if (!in_array($sModuleName,array('plugin','hook'))) { $this->Hook_Run('module_'.$sModuleName.'_'.strtolower($sMethod).'_after',array('result'=>&$result,'params'=>$aArgs)); } - + $oProfiler->Stop($iTimeId); return $result; } @@ -500,7 +562,8 @@ class Engine extends LsObject { /** * Возвращает объект модуля, имя модуля и имя вызванного метода * - * @param string $sName + * @param string $sName Имя метода модуля в полном виде + * Например
Module_Method
* @return array */ public function GetModule($sName) { @@ -510,8 +573,8 @@ class Engine extends LsObject { $aInfo = self::GetClassInfo( $sName, self::CI_MODULE - |self::CI_PPREFIX - |self::CI_METHOD + |self::CI_PPREFIX + |self::CI_METHOD ); if($aInfo[self::CI_MODULE] && $aInfo[self::CI_METHOD]){ $sName = $aInfo[self::CI_MODULE].'_'.$aInfo[self::CI_METHOD]; @@ -519,9 +582,9 @@ class Engine extends LsObject { $sName = $aInfo[self::CI_PPREFIX].$sName; } } - + $aName=explode("_",$sName); - + if(count($aName)==2) { $sModuleName=$aName[0]; $sModuleClass='Module'.$aName[0]; @@ -545,14 +608,24 @@ class Engine extends LsObject { } else { $oModule=$this->LoadModule($sModuleClass,true); } - + return array($oModule,$sModuleName,$sMethod); } - + + /** + * Возвращает статистику выполнения + * + * @return array + */ public function getStats() { return array('sql'=>$this->Database_GetStats(),'cache'=>$this->Cache_GetStats(),'engine'=>array('time_load_module'=>round($this->iTimeLoadModule,3))); } - + + /** + * Возвращает время старта выполнения движка в микросекундах + * + * @return int + */ public function GetTimeInit() { return $this->iTimeInit; } @@ -560,30 +633,35 @@ class Engine extends LsObject { /** * Ставим хук на вызов неизвестного метода и считаем что хотели вызвать метод какого либо модуля * - * @param string $sName - * @param array $aArgs - * @return unknown + * @param string $sName Имя метода + * @param array $aArgs Аргументы + * @return mixed */ public function __call($sName,$aArgs) { return $this->_CallModule($sName,$aArgs); } - + /** * Блокируем копирование/клонирование объекта ядра * */ protected function __clone() { - + } - + /** * Получает объект маппера * - * @param string $sClassName - * @param string $sName + * @param string $sClassName Класс модуля маппера + * @param string|null $sName Имя маппера + * @param DbSimple_Mysql|null $oConnect Объект коннекта к БД + * Можно получить так: + *
+	 * Engine::getInstance()->Database_GetConnect($aConfig);
+	 * 
* @return mixed */ - public static function GetMapper($sClassName,$sName=null,$oConnect=null) { + public static function GetMapper($sClassName,$sName=null,$oConnect=null) { $sModuleName = self::GetClassInfo( $sClassName, self::CI_MODULE, @@ -594,32 +672,33 @@ class Engine extends LsObject { $sName=$sModuleName; } $sClass=$sClassName.'_Mapper'.$sName; - if (!$oConnect) { + if (!$oConnect) { $oConnect=Engine::getInstance()->Database_GetConnect(); } $sClass=self::getInstance()->Plugin_GetDelegate('mapper',$sClass); return new $sClass($oConnect); - } + } return null; } - + /** * Создает объект сущности, контролируя варианты кастомизации * - * @param string $sName - * @param mixed $aParams - * @return mixed + * @param string $sName Имя сущности, возможны сокращенные варианты. + * Например
ModuleUser_EntityUser
эквивалентно
User_User
и эквивалентно
User
т.к. имя сущности совпадает с именем модуля + * @param array $aParams + * @return Entity */ public static function GetEntity($sName,$aParams=array()) { /** - * Сущности, имеющие такое же название как модуль, + * Сущности, имеющие такое же название как модуль, * можно вызывать сокращенно. Например, вместо User_User -> User */ switch (substr_count($sName,'_')) { case 0: $sEntity = $sModule = $sName; break; - + case 1: /** * Поддержка полного синтаксиса при вызове сущности @@ -627,17 +706,17 @@ class Engine extends LsObject { $aInfo = self::GetClassInfo( $sName, self::CI_ENTITY - |self::CI_MODULE - |self::CI_PLUGIN + |self::CI_MODULE + |self::CI_PLUGIN ); if ($aInfo[self::CI_MODULE] - && $aInfo[self::CI_ENTITY]) { + && $aInfo[self::CI_ENTITY]) { $sName=$aInfo[self::CI_MODULE].'_'.$aInfo[self::CI_ENTITY]; } - + list($sModule,$sEntity) = explode('_',$sName,2); /** - * Обслуживание короткой записи сущностей плагинов + * Обслуживание короткой записи сущностей плагинов * PluginTest_Test -> PluginTest_ModuleTest_EntityTest */ if($aInfo[self::CI_PLUGIN]) { @@ -645,7 +724,7 @@ class Engine extends LsObject { $sModule = $sEntity; } break; - + case 2: /** * Поддержка полного синтаксиса при вызове сущности плагина @@ -653,12 +732,12 @@ class Engine extends LsObject { $aInfo = self::GetClassInfo( $sName, self::CI_ENTITY - |self::CI_MODULE - |self::CI_PLUGIN + |self::CI_MODULE + |self::CI_PLUGIN ); if ($aInfo[self::CI_PLUGIN] - && $aInfo[self::CI_MODULE] - && $aInfo[self::CI_ENTITY]) { + && $aInfo[self::CI_MODULE] + && $aInfo[self::CI_ENTITY]) { $sName='Plugin'.$aInfo[self::CI_PLUGIN] .'_'.$aInfo[self::CI_MODULE] .'_'.$aInfo[self::CI_ENTITY] @@ -667,22 +746,22 @@ class Engine extends LsObject { /** * Entity плагина */ - if($aInfo[self::CI_PLUGIN]) { + if($aInfo[self::CI_PLUGIN]) { list(,$sModule,$sEntity)=explode('_',$sName); $sPlugin = $aInfo[self::CI_PLUGIN]; } else { throw new Exception("Unknown entity '{$sName}' given."); } break; - + default: throw new Exception("Unknown entity '{$sName}' given."); } - + $sClass=isset($sPlugin) ? 'Plugin'.$sPlugin.'_Module'.$sModule.'_Entity'.$sEntity : 'Module'.$sModule.'_Entity'.$sEntity; - + /** * If Plugin Entity doesn't exist, search among it's Module delegates */ @@ -699,38 +778,87 @@ class Engine extends LsObject { $sClass='Module'.$sModule.'_Entity'.$sEntity; } } - + /** * Определяем наличие делегата сущности * Делегирование указывается только в полной форме! */ - $sClass=self::getInstance()->Plugin_GetDelegate('entity',$sClass); + $sClass=self::getInstance()->Plugin_GetDelegate('entity',$sClass); $oEntity=new $sClass($aParams); return $oEntity; } - - + + /** + * Возвращает имя плагина моудля если модул принадлежит плагину. + * Например
Openid
+ * + * @static + * @param Module $oModule Объект модуля + * @return string|null + */ public static function GetPluginName($oModule) { return self::GetClassInfo($oModule, self::CI_PLUGIN, true); } + /** + * Возвращает префикс плагина + * Например
PluginOpenid_
+ * + * @static + * @param Module $oModule Объект модуля + * @return string Если плагина нет, возвращает пустую строку + */ public static function GetPluginPrefix($oModule) { return self::GetClassInfo($oModule, self::CI_PPREFIX, true); } + /** + * Возвращает имя модуля + * + * @static + * @param Module $oModule Объект модуля + * @return string|null + */ public static function GetModuleName($oModule) { return self::GetClassInfo($oModule, self::CI_MODULE, true); - } - + } + + /** + * Возвращает имя сущности + * + * @static + * @param Entity $oEntity Объект сущности + * @return string|null + */ public static function GetEntityName($oEntity) { return self::GetClassInfo($oEntity, self::CI_ENTITY, true); } - + + /** + * Возвращает имя экшена + * + * @static + * @param $oAction Объект экшена + * @return string|null + */ public static function GetActionName($oAction) { return self::GetClassInfo($oAction, self::CI_ACTION, true); } - + + /** + * Возвращает информацию об объекта или классе + * + * @static + * @param LsObject|string $oObject Объект или имя класса + * @param int $iFlag Маска по которой нужно вернуть рузультат. Доступные маски определены в константах CI_* + * Например, получить информацию о плагине и модуле: + *
+	 * Engine::GetClassInfo($oObject,Engine::CI_PLUGIN | Engine::CI_MODULE);
+	 * 
+ * @param bool $bSingle Возвращать полный результат или только первый элемент + * @return array|string|null + */ public static function GetClassInfo($oObject,$iFlag=self::CI_DEFAULT,$bSingle=false){ $sClassName = is_string($oObject) ? $oObject : get_class($oObject); $aResult = array(); @@ -770,7 +898,7 @@ class Engine extends LsObject { : null ; } - if($iFlag & self::CI_BLOCK){ + if($iFlag & self::CI_BLOCK){ $aResult[self::CI_BLOCK] = preg_match('/^(?:Plugin[^_]+_|)Block([^_]+)$/',$sClassName,$aMatches) ? $aMatches[1] : null @@ -821,16 +949,23 @@ class Engine extends LsObject { if($iFlag & self::CI_CLASSPATH){ $aResult[self::CI_CLASSPATH] = self::GetClassPath($sClassName); } - + return $bSingle ? array_pop($aResult) : $aResult; } - - + + /** + * Возвращает информацию о пути до файла класса. + * Используется в {@link autoload автозагрузке} + * + * @static + * @param LsObject $oObject Объект - модуль, экшен, плагин, хук, сущность + * @return null|string + */ public static function GetClassPath($oObject){ $aInfo = self::GetClassInfo( $oObject, self::CI_OBJECT - ); + ); $sPath = Config::get('path.root.server').'/'; if($aInfo[self::CI_ENTITY]){ // Сущность @@ -908,7 +1043,7 @@ class Engine extends LsObject { // Хук ядра $sPath .= 'classes/hooks/Hook'.$aInfo[self::CI_HOOK].'.class.php'; } - }elseif($aInfo[self::CI_BLOCK]){ + }elseif($aInfo[self::CI_BLOCK]){ // Блок if($aInfo[self::CI_PLUGIN]){ // Блок плагина @@ -928,16 +1063,17 @@ class Engine extends LsObject { ; }else{ $sClassName = is_string($oObject) ? $oObject : get_class($oObject); - $sPath .= 'engine/classes/'.$sClassName.'.class.php'; + $sPath .= 'engine/classes/'.$sClassName.'.class.php'; } return is_file($sPath) ? $sPath : null; } - - + + /** * Автозагрузка классов * - * @param unknown_type $sClassName + * @param string $sClassName Название класса + * @return bool */ public static function autoload($sClassName) { $aInfo = Engine::GetClassInfo( @@ -962,16 +1098,21 @@ class Engine extends LsObject { } return false; } - + } +/** + * Регистрация автозагрузки классов + */ spl_autoload_register(array('Engine','autoload')); /** - * Short aliases for Engine basic methods - * + * Короткий алиас для вызова основных методов движка + * + * @package engine + * @since 1.0 */ -class LS { +class LS extends LsObject { static protected $oInstance=null; @@ -983,42 +1124,80 @@ class LS { return self::$oInstance; } } - + /** + * Возвращает ядро + * @see Engine::GetInstance + * + * @return Engine + */ public function E() { return Engine::GetInstance(); } - + /** + * Возвращает объект сущности + * @see Engine::GetEntity + * + * @param $sName Название сущности + * @param array $aParams Параметры для передачи в конструктор + * @return Entity + */ public function Ent($sName,$aParams=array()) { return Engine::GetEntity($sName,$aParams); } - + /** + * Возвращает объект маппера + * @see Engine::GetMapper + * + * @param $sClassName Класс модуля маппера + * @param string|null $sName Имя маппера + * @param DbSimple_Mysql|null $oConnect Объект коннекта к БД + * @return mixed + */ public function Mpr($sClassName,$sName=null,$oConnect=null) { return Engine::GetMapper($sClassName,$sName,$oConnect); } - + /** + * Возвращает текущего авторизованного пользователя + * @see ModuleUser::GetUserCurrent + * + * @return ModuleUser_EntityUser + */ public function CurUsr() { return self::E()->User_GetUserCurrent(); } - + /** + * Возвращает true если текущий пользователь администратор + * @see ModuleUser::GetUserCurrent + * @see ModuleUser_EntityUser::isAdministrator + * + * @return bool + */ public function Adm() { return self::CurUsr() && self::CurUsr()->isAdministrator(); } - /** - * In templates we have $LS and can use $LS->Module_Method() instead of $LS->E()->Module_Method(); + * Вызов метода модуля + * Например
$LS->Module_Method()
* - */ + * @param $sName Полное название метода, например
Module_Method
+ * @param array $aArgs Список аргуметов метода + * @return mixed + */ public function __call($sName,$aArgs=array()) { return call_user_func_array(array(self::E(),$sName),$aArgs); - } - + } /** - * With PHP 5.3+ we can use LS::Module_Method() instead of LS::E()->Module_Method(); + * Статический вызов метода модуля для PHP >= 5.3 + * Например
LS::Module_Method()
* + * @static + * @param $sName Полное название метода, например
Module_Method
+ * @param array $aArgs Список аргуметов метода + * @return mixed */ public static function __callStatic($sName,$aArgs=array()) { return call_user_func_array(array(self::E(),$sName),$aArgs); - } + } } ?> \ No newline at end of file