]> git.andy128k.dev Git - ipf.git/commitdiff
move models lists to legacy orm app
authorAndrey Kutejko <andy128k@gmail.com>
Wed, 20 Aug 2014 22:51:25 +0000 (01:51 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Wed, 20 Aug 2014 22:51:25 +0000 (01:51 +0300)
ipf/admin/app.php
ipf/admin/commands/syncperms.php
ipf/admin/views.php
ipf/application.php
ipf/auth/app.php
ipf/legacy_orm/app.php
ipf/legacy_orm/commands/buildmodels.php
ipf/legacy_orm/commands/syncdb.php
ipf/legacy_orm/orm.php

index a51ab0e6bfa4c973d581711d8fa91ca3e36a87c4..5221a33eb5d6c1786d69ba4b557d2d1a7524437d 100644 (file)
@@ -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);
                 }
index 1811cfd87e4343a9ee63c7d7ccbd521918daa42d..c56430bfae5273f60cbf2fca5028046ae8a1da70 100644 (file)
@@ -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) {
index 3955ab7a3f94376fc957bd2c7fc155d101241a46..43d2be0bf4bbc3f7d47b73c8693d86845de38908 100644 (file)
@@ -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,
+            ));
         }
     }
 
index d6b049db87c2a56e286f537c6961433246b5a570..458a3701b513a08911e0eda1b6d3a76357548dce 100644 (file)
@@ -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()
index e2330b77bd5a7e964594dec74b38ff5a1f4f9025..81affddbfd7a4912483ad3f2c83e5a7d60c56acf 100644 (file)
@@ -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)
index 037669996e5d88164051b0db25e11353ab1e10cb..8aa0019e82b7e04507cedf3e7ff86f5676f3059a 100644 (file)
@@ -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()
index 3aa84b54923be59152c8518711f7c5d43c70cc88..0e2e9b895fd68558241d0c6b1404d95b448b9898 100644 (file)
@@ -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;
     }
 }
index cf1afcc13a806bc1fd9fe81f1d33a8245a77a3d4..02b550faff3f1298db3af1dc753697031dd5f048 100644 (file)
@@ -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);
     }
index fb0396f4abb90c7945252c836c817cd0124f4610..660c0a97f26ca69b451248b8b325a0e73004d6c6 100644 (file)
@@ -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) {