]> git.andy128k.dev Git - ipf.git/commitdiff
autodiscover app models
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 16 Jun 2013 22:34:10 +0000 (01:34 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sun, 16 Jun 2013 22:34:10 +0000 (01:34 +0300)
ipf/application.php

index f32af43542d7807e9dd0811917f24f7d3b94a36e..c0a44ca491f345918117cb92f81d7c960a2e80f7 100644 (file)
@@ -1,79 +1,94 @@
 <?php
 
-abstract class IPF_Application{
-
+abstract class IPF_Application
+{
     protected $models = array();
     protected $name = null;
 
-    public function __construct($data){
-        $this->setName();
-        if (array_key_exists('models',$data)){
-           foreach($data['models'] as &$modelname){
-               if (!IPF_Utils::isValidName($modelname))
-                       throw new IPF_Exception_Panic("Model name \"$name\" is incorrect");
-               $this->models[] = $modelname;
-            }
-        }
-    }
-
-    protected function setName(){
+    public function __construct($data=array())
+    {
         $this->name = str_replace('_App', '', get_class($this));
+
         if (strpos($this->name,'IPF_')===0)
             $this->path = IPF::get('ipf_path').DIRECTORY_SEPARATOR.'ipf'.DIRECTORY_SEPARATOR.strtolower(str_replace('_',DIRECTORY_SEPARATOR,str_replace('IPF_','',$this->name)));
         else
             $this->path = IPF::get('project_path').DIRECTORY_SEPARATOR.strtolower(str_replace('_',DIRECTORY_SEPARATOR,$this->name));
         $this->path .= DIRECTORY_SEPARATOR;
+
+        if (array_key_exists('models',$data)) {
+            foreach ($data['models'] as &$modelname) {
+                if (!IPF_Utils::isValidName($modelname))
+                    throw new IPF_Exception_Panic("Model name \"$modelname\" is incorrect");
+                $this->models[] = $modelname;
+            }
+        } else {
+            try {
+                $it = new DirectoryIterator($this->path.DIRECTORY_SEPARATOR.'models');
+                foreach ($it as $file) {
+                    $e = explode('.', $file->getFileName(), 2);
+                    if ($e[1] === 'php') {
+                        $this->models[] = $e[0];
+                    }
+                }
+            } catch(RuntimeException $e) {
+                // nothing to do
+            }
+        }
     }
 
-    public function generateSql(){
-        if (count($this->models)==0)
-            return;
+    public function generateSql()
+    {
         return IPF_ORM::generateSqlFromModels($this->path.'models');
     }
 
-    public function modelList(){
+    public function modelList()
+    {
         return $this->models;
     }
 
-    public function getName(){
+    public function getName()
+    {
         return $this->name;
     }
 
-    public function getLabel(){
+    public function getLabel()
+    {
         return str_replace('ipf_','',strtolower($this->name));
     }
 
-    public function getAdditions(){
+    public function getAdditions()
+    {
         return array();
     }
 
-    public function getTitle(){
+    public function getTitle()
+    {
         return $this->name;
     }
-    
+
     public function getPath()
     {
         return $this->path;
     }
 
-    public function getSlug(){
+    public function getSlug()
+    {
         $e = explode('_',$this->name);
         return strtolower($e[count($e)-1]);
     }
 
-    public function createTablesFromModels(){
-        if (count($this->models)==0)
-            return;
+    public function createTablesFromModels()
+    {
         return IPF_ORM::createTablesFromModels($this->path.'models');
     }
-  
-    public function generateModels(){
+
+    public function generateModels()
+    {
         IPF_ORM::generateModelsFromYaml($this->path.'models.yml', $this->path.'models');
     }
 
-    public function loadModels(){
-        if (count($this->models)==0)
-            return;
+    public function loadModels()
+    {
         IPF_ORM::loadModels($this->path.'models');
     }
 }