From: Andrey Kutejko Date: Sun, 14 Jul 2013 09:37:21 +0000 (+0300) Subject: cleanup X-Git-Tag: 0.6~93 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=7be81261713e861a225a098be0e7ba708b431d7d;p=ipf-legacy-orm.git cleanup --- diff --git a/ipf/orm.php b/ipf/orm.php index bb5854d..78cc0ce 100644 --- a/ipf/orm.php +++ b/ipf/orm.php @@ -177,8 +177,6 @@ final class IPF_ORM const BASE_CLASSES_DIRECTORY = '_generated'; - private static $_loadedModelFiles = array(); - private function __construct() {} public static function getTable($componentName) @@ -207,80 +205,20 @@ final class IPF_ORM return $models; } - public static function createTablesFromModels($directory) + public static function createTablesFromModels($app) { - return IPF_ORM_Manager::connection()->export->exportSchema($directory); + IPF_ORM_Manager::connection()->export->exportClasses($app->modelList()); } - - public static function generateSqlFromModels($directory = null) + + public static function generateSqlFromModels($app) { - $sql = IPF_ORM_Manager::connection()->export->exportSql($directory); + $sql = IPF_ORM_Manager::connection()->export->exportSortedClassesSql($app->modelList(), false); + $build = ''; foreach ($sql as $query) { $build .= $query.";\n\n"; } return $build; - } - - public static function loadModel($className, $path = null) - { - self::$_loadedModelFiles[$className] = $path; - } - - public static function filterInvalidModels($classes) - { - $validModels = array(); - foreach ((array) $classes as $name) { - if (self::isValidModelClass($name) && ! in_array($name, $validModels)) { - $validModels[] = $name; - } - } - - return $validModels; - } - - public static function loadModels($directory, $modelLoading = null) - { - $loadedModels = array(); - try{ - $it = new DirectoryIterator($directory.DIRECTORY_SEPARATOR.IPF_ORM::BASE_CLASSES_DIRECTORY); - }catch(RuntimeException $e){ - return $loadedModels; - } - foreach ($it as $file) { - $e = explode('.', $file->getFileName()); - if (end($e) === 'php') { - $className = $e[0]; - require_once($file->getPathName()); - } - } - $it = new DirectoryIterator($directory); - foreach ($it as $file) { - $e = explode('.', $file->getFileName()); - if (end($e) === 'php') { - $className = $e[0]; - require_once($file->getPathName()); - $loadedModels[$className] = $className; - } - } - return $loadedModels; - } - - - public static function isValidModelClass($class) - { - if ($class instanceof IPF_ORM_Record) { - $class = get_class($class); - } - if (is_string($class) && class_exists($class)) { - $class = new ReflectionClass($class); - } - if ($class instanceof ReflectionClass) { - if ( ! $class->isAbstract() && $class->isSubClassOf('IPF_ORM_Record')) { - return true; - } - } - return false; } public static function dump($var, $output = true, $indent = "") @@ -311,3 +249,4 @@ final class IPF_ORM return implode("\n", $ret); } } + diff --git a/ipf/orm/export.php b/ipf/orm/export.php index 10db5f3..43b76ed 100644 --- a/ipf/orm/export.php +++ b/ipf/orm/export.php @@ -128,15 +128,6 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module return $sql; } - public function createTable($name, array $fields, array $options = array()) - { - $sql = (array) $this->createTableSql($name, $fields, $options); - - foreach ($sql as $query) { - $this->conn->execute($query); - } - } - public function createSequence($seqName, $start = 1, array $options = array()) { return $this->conn->execute($this->createSequenceSql($seqName, $start = 1, $options)); @@ -443,16 +434,6 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module return ''; } - public function exportSchema($directory = null) - { - if ($directory !== null) { - $models = IPF_ORM::filterInvalidModels(IPF_ORM::loadModels($directory)); - } else { - $models = IPF_ORM::getLoadedModels(); - } - $this->exportClasses($models); - } - public function exportSortedClassesSql($classes, $groupByConnection = true) { $connections = array(); @@ -548,10 +529,8 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module } } - public function exportClassesSql(array $classes) + public function exportClassesSql(array $models) { - $models = IPF_ORM::filterInvalidModels($classes); - $sql = array(); foreach ($models as $name) { @@ -630,29 +609,5 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module return $sql; } +} - public function exportSql($directory = null) - { - if ($directory !== null) { - $models = IPF_ORM::filterInvalidModels(IPF_ORM::loadModels($directory)); - } else { - $models = IPF_ORM::getLoadedModels(); - } - - return $this->exportSortedClassesSql($models, false); - } - - public function exportTable(IPF_ORM_Table $table) - { - try { - $data = $table->getExportableFormat(); - - $this->conn->export->createTable($data['tableName'], $data['columns'], $data['options']); - } catch(IPF_ORM_Exception $e) { - // we only want to silence table already exists errors - if ($e->getPortableCode() !== IPF_ORM::ERR_ALREADY_EXISTS) { - throw $e; - } - } - } -} \ No newline at end of file diff --git a/ipf/orm/import/builder.php b/ipf/orm/import/builder.php index 09330d2..dd9dfc3 100644 --- a/ipf/orm/import/builder.php +++ b/ipf/orm/import/builder.php @@ -452,8 +452,6 @@ class IPF_ORM_Import_Builder IPF_Utils::makeDirectories($targetPath); if (file_put_contents($writePath, implode(PHP_EOL, $code)) === false) throw new IPF_ORM_Exception("Couldn't write file " . $writePath); - - IPF_ORM::loadModel($className, $writePath); } } diff --git a/ipf/orm/table.php b/ipf/orm/table.php index d4e566e..af399f6 100644 --- a/ipf/orm/table.php +++ b/ipf/orm/table.php @@ -326,11 +326,7 @@ class IPF_ORM_Table extends IPF_ORM_Configurable implements Countable public function getTemplates() { return $this->_templates; - } - public function export() - { - $this->_conn->export->exportTable($this); } public function getExportableFormat($parseForeignKeys = true)