]> git.andy128k.dev Git - ipf-legacy-orm.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/legacy_orm/app.php
ipf/legacy_orm/commands/buildmodels.php
ipf/legacy_orm/commands/syncdb.php
ipf/legacy_orm/orm.php

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) {