From e4d64f1cf5adaec19cc470b00768d3bf01a20fa5 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Mon, 17 Jun 2013 01:34:10 +0300 Subject: [PATCH] autodiscover app models --- ipf/application.php | 79 +++++++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 32 deletions(-) diff --git a/ipf/application.php b/ipf/application.php index f32af43..c0a44ca 100644 --- a/ipf/application.php +++ b/ipf/application.php @@ -1,79 +1,94 @@ 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'); } } -- 2.49.0