From: Andrey Kutejko Date: Wed, 20 Aug 2014 22:51:25 +0000 (+0300) Subject: move models lists to legacy orm app X-Git-Tag: 0.6~179 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=e24aad4ccd2f990f74801c1998e2dfea5122f7fc;p=ipf.git move models lists to legacy orm app --- diff --git a/ipf/admin/app.php b/ipf/admin/app.php index a51ab0e..5221a33 100644 --- a/ipf/admin/app.php +++ b/ipf/admin/app.php @@ -2,13 +2,6 @@ class IPF_Admin_App extends IPF_Application { - public function __construct() - { - parent::__construct(array( - 'models'=>array('AdminLog') - )); - } - public static function urls() { return array( @@ -45,6 +38,13 @@ class IPF_Admin_App extends IPF_Application return new IPF_HTTP_Response($html); } + public static function loadAllModels() + { + foreach (IPF_Project::getInstance()->appList() as $appname => $app) + foreach (IPF_Legacy_ORM_App::appModelList($app) as $modelName) + new $modelName; + } + static function checkAdminAuth($request) { $ok = true; @@ -56,14 +56,14 @@ class IPF_Admin_App extends IPF_Application if (!$ok) return new IPF_HTTP_Response_Redirect(IPF_HTTP_URL::urlForView('IPF_Admin_Views_Login')); - IPF_Project::getInstance()->loadAllModels(); + self::loadAllModels(); return true; } static function appByModel($model) { foreach (IPF_Project::getInstance()->appList() as $app) - foreach ($app->modelList() as $m) + foreach (IPF_Legacy_ORM_App::appModelList($app) as $m) if ($model == $m) return $app; return null; @@ -73,7 +73,7 @@ class IPF_Admin_App extends IPF_Application { foreach (IPF_Project::getInstance()->appList() as $app) { if ($app->getSlug() == $lapp) { - foreach ($app->modelList() as $m) { + foreach (IPF_Legacy_ORM_App::appModelList($app) as $m) { if (strtolower($m) == $lmodel) return array('app' => $app, 'modelname' => $m); } diff --git a/ipf/admin/commands/syncperms.php b/ipf/admin/commands/syncperms.php index 1811cfd..c56430b 100644 --- a/ipf/admin/commands/syncperms.php +++ b/ipf/admin/commands/syncperms.php @@ -14,7 +14,7 @@ class IPF_Admin_Command_SyncPerms print "COLLECTED PERMS:\n----\n"; $permissions = array(); foreach (IPF_Project::getInstance()->appList() as $appname => $app) { - foreach ($app->modelList() as $modelName) { + foreach (IPF_Legacy_ORM_App::appModelList($app) as $modelName) { $adminModel = IPF_Admin_Model::getModelAdmin($modelName); if ($adminModel) { foreach ($adminModel->getPerms(null) as $permName) { diff --git a/ipf/admin/views.php b/ipf/admin/views.php index 3955ab7..43d2be0 100644 --- a/ipf/admin/views.php +++ b/ipf/admin/views.php @@ -7,36 +7,38 @@ function IPF_Admin_Views_Index($request, $match) $apps = array(); $app_list = new IPF_Template_ContextVars(); - foreach (IPF_Project::getInstance()->appList() as $app){ - if (count($app->modelList())>0){ - $models = new IPF_Template_ContextVars(); - $models_found = false; - foreach($app->modelList() as $m){ - $ma = IPF_Admin_Model::getModelAdmin($m); - if ($ma!==null && !$ma->hidden){ - $perms = $ma->getPerms($request); - if (array_search('view', $perms)!==false){ - $user_perms = IPF_Auth_App::checkPermissions($request, $app, $m, array('view')); - if ($user_perms['view']) - { - $models[] = new IPF_Template_ContextVars(array( - 'name'=>$ma->verbose_name(), - 'path'=>strtolower($m), - 'perms'=>$perms, - )); - $models_found = true; - } + foreach (IPF_Project::getInstance()->appList() as $app) { + $appModels = IPF_Legacy_ORM_App::appModelList($app); + if (!$appModels) + continue; + + $models = new IPF_Template_ContextVars(); + $models_found = false; + foreach ($appModels as $m) { + $ma = IPF_Admin_Model::getModelAdmin($m); + if ($ma!==null && !$ma->hidden){ + $perms = $ma->getPerms($request); + if (array_search('view', $perms)!==false){ + $user_perms = IPF_Auth_App::checkPermissions($request, $app, $m, array('view')); + if ($user_perms['view']) + { + $models[] = new IPF_Template_ContextVars(array( + 'name'=>$ma->verbose_name(), + 'path'=>strtolower($m), + 'perms'=>$perms, + )); + $models_found = true; } } } - if ($models_found){ - $app_list[$app->getName()] = new IPF_Template_ContextVars(array( - 'name' => $app->getTitle(), - 'path' => $app->getSlug(), - 'additions' => $app->getAdditions(), - 'models' => $models, - )); - } + } + if ($models_found){ + $app_list[$app->getName()] = new IPF_Template_ContextVars(array( + 'name' => $app->getTitle(), + 'path' => $app->getSlug(), + 'additions' => $app->getAdditions(), + 'models' => $models, + )); } } diff --git a/ipf/application.php b/ipf/application.php index d6b049d..458a370 100644 --- a/ipf/application.php +++ b/ipf/application.php @@ -2,7 +2,6 @@ abstract class IPF_Application { - protected $models = array(); protected $name = null; public function __construct($data=array()) @@ -11,35 +10,6 @@ abstract class IPF_Application $rc = new ReflectionClass($this); $this->path = dirname($rc->getFileName()).DIRECTORY_SEPARATOR; - - $models = array(); - try { - $it = new DirectoryIterator($this->path.DIRECTORY_SEPARATOR.'models'); - foreach ($it as $file) { - $e = explode('.', $file->getFileName(), 2); - if (count($e) == 2 && $e[1] === 'php') { - $models[] = $e[0]; - } - } - } catch (RuntimeException $e) { - // nothing to do - } - - // reorder models according to a given parameter - if (array_key_exists('models', $data)) { - foreach ($data['models'] as $modelname) { - if (!in_array($modelname, $models)) - throw new IPF_Exception_Panic("Model \"$modelname\" does not exist."); - } - $models = array_merge($data['models'], array_diff($models, $data['models'])); - } - - $this->models = $models; - } - - public function modelList() - { - return $this->models; } public function getName() diff --git a/ipf/auth/app.php b/ipf/auth/app.php index e2330b7..81affdd 100644 --- a/ipf/auth/app.php +++ b/ipf/auth/app.php @@ -2,13 +2,9 @@ class IPF_Auth_App extends IPF_Application { - public function __construct() + public function admin_models_order() { - parent::__construct(array( - 'models' => array( - 'User', - 'Role', - ))); + return array('User', 'Role'); } static function login($request, $user) diff --git a/ipf/legacy_orm/app.php b/ipf/legacy_orm/app.php index 0376699..8aa0019 100644 --- a/ipf/legacy_orm/app.php +++ b/ipf/legacy_orm/app.php @@ -2,9 +2,37 @@ class IPF_Legacy_ORM_App extends IPF_Application { - public function __construct() + private static $appModels = array(); + + public static function appModelList($app) { - parent::__construct(); + if (!array_key_exists($app->getName(), self::$appModels)) { + $models = array(); + try { + $it = new DirectoryIterator($app->getPath().DIRECTORY_SEPARATOR.'models'); + foreach ($it as $file) { + $e = explode('.', $file->getFileName(), 2); + if (count($e) == 2 && $e[1] === 'php') { + $models[] = $e[0]; + } + } + } catch (RuntimeException $e) { + // nothing to do + } + + // reorder models according to a given option + if (method_exists($app, 'admin_models_order')) { + $ordered = $app->admin_models_order(); + foreach ($ordered as $modelname) { + if (!in_array($modelname, $models)) + throw new IPF_Exception_Panic("Model \"$modelname\" does not exist."); + } + $models = array_merge($ordered, array_diff($models, $ordered)); + } + + self::$appModels[$app->getName()] = $models; + } + return self::$appModels[$app->getName()]; } public function commands() diff --git a/ipf/legacy_orm/commands/buildmodels.php b/ipf/legacy_orm/commands/buildmodels.php index 3aa84b5..0e2e9b8 100644 --- a/ipf/legacy_orm/commands/buildmodels.php +++ b/ipf/legacy_orm/commands/buildmodels.php @@ -23,7 +23,7 @@ class IPF_Legacy_ORM_Command_BuildModels { $models = array(); foreach ($project->frameworkApps() as $app) - $models = array_merge($models, $app->modelList()); + $models = array_merge($models, IPF_Legacy_ORM_App::appModelList($app)); return $models; } } diff --git a/ipf/legacy_orm/commands/syncdb.php b/ipf/legacy_orm/commands/syncdb.php index cf1afcc..02b550f 100644 --- a/ipf/legacy_orm/commands/syncdb.php +++ b/ipf/legacy_orm/commands/syncdb.php @@ -13,7 +13,7 @@ class IPF_Legacy_ORM_Command_SyncDB $models = array(); foreach ($project->appList() as $app) - $models = array_merge($models, $app->modelList()); + $models = array_merge($models, IPF_Legacy_ORM_App::appModelList($app)); IPF_ORM::createTablesFromModels($models); } diff --git a/ipf/legacy_orm/orm.php b/ipf/legacy_orm/orm.php index fb0396f..660c0a9 100644 --- a/ipf/legacy_orm/orm.php +++ b/ipf/legacy_orm/orm.php @@ -207,7 +207,7 @@ final class IPF_ORM public static function generateSqlFromModels($app) { - $sql = IPF_ORM_Manager::connection()->export->exportSortedClassesSql($app->modelList(), false); + $sql = IPF_ORM_Manager::connection()->export->exportSortedClassesSql(IPF_Legacy_ORM_App::appModelList($app), false); $build = ''; foreach ($sql as $query) {