From: Andrey Kutejko Date: Sun, 14 Jul 2013 09:37:21 +0000 (+0300) Subject: cleanup X-Git-Tag: 0.5~171 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=f2eaa79c4380bb19565eef641e2be3ef4de15f15;p=ipf.git cleanup --- diff --git a/ipf/application.php b/ipf/application.php index 73f3709..47006cb 100644 --- a/ipf/application.php +++ b/ipf/application.php @@ -33,11 +33,6 @@ abstract class IPF_Application } } - public function generateSql() - { - return IPF_ORM::generateSqlFromModels($this->path.'models'); - } - public function modelList() { return $this->models; @@ -74,16 +69,6 @@ abstract class IPF_Application return strtolower($e[count($e)-1]); } - public function createTablesFromModels() - { - return IPF_ORM::createTablesFromModels($this->path.'models'); - } - - public function loadModels() - { - IPF_ORM::loadModels($this->path.'models'); - } - /** * Returns additional context for templates * diff --git a/ipf/auth/app.php b/ipf/auth/app.php index 5568add..a5295a9 100644 --- a/ipf/auth/app.php +++ b/ipf/auth/app.php @@ -33,32 +33,17 @@ class IPF_Auth_App extends IPF_Application $request->session->setData('logout_time', gmdate('Y-m-d H:i:s')); } - static function createPermissionsFromModels(array $pathesToModels) + static function createPermissionsFromModels(array $apps) { - $baseAdmin = new IPF_Admin_Model(); - $basePerms = $baseAdmin->getPerms(); $permsTable = IPF_ORM::getTable('Permission'); - $appList = IPF_Project::getInstance()->appList(); $permissions = array(); - - foreach ($pathesToModels as $path) - { - foreach (IPF_ORM::filterInvalidModels(IPF_ORM::loadModels($path)) as $modelName) - { + foreach ($apps as $appname => $app) { + foreach ($app->modelList() as $modelName) { $adminModel = IPF_Admin_Model::getModelAdmin($modelName); - - if ($adminModel) - { - $perms = method_exists($adminModel, 'getPerms') ? $adminModel->getPerms(null) : $basePerms; - - foreach ($appList as $app) - { - if (in_array($modelName, $app->modelList()) && (!method_exists($app, 'NoPermsFor') || !in_array($modelName, $app->NoPermsFor()))) - { - foreach ($perms as $permName) - $permissions[] = get_class($app).'|'.$modelName.'|'.$permName; - } + if ($adminModel) { + foreach ($adminModel->getPerms(null) as $permName) { + $permissions[] = $appname.'|'.$modelName.'|'.$permName; } } } @@ -66,8 +51,7 @@ class IPF_Auth_App extends IPF_Application print "COLLECTED PERMS:\n----\n".implode("\n", $permissions)."\n----\n"; - if (count($permissions)) - { + if (count($permissions)) { $existingPerms = array(); foreach ($permsTable->findAll() as $model) @@ -75,14 +59,12 @@ class IPF_Auth_App extends IPF_Application print "EXISTING PERMS:\n----\n".implode("\n",$existingPerms)."\n----\n"; - if (count($existingPerms)) - { + if (count($existingPerms)) { $toDel = array_diff($existingPerms, $permissions); print "2DEL:\n----\n".implode("\n",$toDel)."\n----\n"; - if (count($toDel)) - { + if (count($toDel)) { $permsTable->createQuery() ->delete() ->where("name in ('".implode("','", $toDel)."')") @@ -90,9 +72,8 @@ class IPF_Auth_App extends IPF_Application } $toAdd = array_diff($permissions, $existingPerms); - } - else // first time - { + } else { + // first time // *** FIX: previously, the following models haven't "onDelete: CASCADE" constrain *** print "DROP RolePermission, UserRole, UserPermission\n"; $export = IPF_ORM_Manager::connection()->export; @@ -100,23 +81,19 @@ class IPF_Auth_App extends IPF_Application $export->dropTable(IPF_ORM::getTable('UserRole')->getTableName()); $export->dropTable(IPF_ORM::getTable('UserPermission')->getTableName()); $auth_app = new IPF_Auth_App(); - $auth_app->createTablesFromModels(); - // *** FIX *** + IPF_ORM::createTablesFromModels($auth_app); $toAdd = $permissions; } print "2ADD:\n----\n".implode("\n",$toAdd)."\n----\n"; - foreach ($toAdd as $name) - { + foreach ($toAdd as $name) { $model = new Permission(); $model->name = $name; $model->save(); } - } - else - { + } else { print "REMOVE ALL\n"; $permsTable->createQuery()->delete()->execute(); // no women, no cry... diff --git a/ipf/command/createsuperuser.php b/ipf/command/createsuperuser.php index e230e2c..780f6dd 100644 --- a/ipf/command/createsuperuser.php +++ b/ipf/command/createsuperuser.php @@ -15,8 +15,6 @@ class IPF_Command_CreateSuperUser $project = IPF_Project::getInstance(); - $project->loadModels(); - $su = new User; $su->username = $username; $su->email = $email; diff --git a/ipf/command/fixtures.php b/ipf/command/fixtures.php index 7bb6154..0ed9fff 100644 --- a/ipf/command/fixtures.php +++ b/ipf/command/fixtures.php @@ -27,8 +27,6 @@ class IPF_Command_Fixtures return; } - $project->loadModels(); - foreach ($fixtures as $fixture) { $modelClass = $fixture['model']; $key = $fixture['key']; diff --git a/ipf/command/sql.php b/ipf/command/sql.php index ac22e70..4124cbc 100644 --- a/ipf/command/sql.php +++ b/ipf/command/sql.php @@ -14,12 +14,10 @@ class IPF_Command_Sql $sql = ''; foreach ($project->frameworkApps() as $app) - $sql .= $app->generateSql()."\n"; - - $sql .= IPF_ORM::generateSqlFromModels(IPF::get('project_path').DIRECTORY_SEPARATOR.'models')."\n"; + $sql .= IPF_ORM::generateSqlFromModels($app)."\n"; foreach ($project->customApps() as $app) - $sql .= $app->generateSql()."\n"; + $sql .= IPF_ORM::generateSqlFromModels($app)."\n"; print $sql; } diff --git a/ipf/command/syncdb.php b/ipf/command/syncdb.php index 9c52373..9841e29 100644 --- a/ipf/command/syncdb.php +++ b/ipf/command/syncdb.php @@ -11,11 +11,8 @@ class IPF_Command_SyncDB $project = IPF_Project::getInstance(); - foreach ($project->frameworkApps() as $app) - $app->createTablesFromModels(); - IPF_ORM::createTablesFromModels(IPF::get('project_path').DIRECTORY_SEPARATOR.'models'); - foreach ($project->customApps() as $app) - $app->createTablesFromModels(); + foreach ($project->appList() as $app) + IPF_ORM::createTablesFromModels($app); } } diff --git a/ipf/command/syncperms.php b/ipf/command/syncperms.php index e2a5e5c..9aadb85 100644 --- a/ipf/command/syncperms.php +++ b/ipf/command/syncperms.php @@ -10,17 +10,7 @@ class IPF_Command_SyncPerms print "Create/Update permissions from model classes\n"; $project = IPF_Project::getInstance(); - - $pathes = array(); - - foreach ($project->apps as $appname => &$app) { - $app = $project->getApp($appName); - $pathes[] = $app->getPath().'models'; - } - - $pathes[] = IPF::get('project_path').DIRECTORY_SEPARATOR.'models'; - - return IPF_Auth_App::createPermissionsFromModels($pathes); + return IPF_Auth_App::createPermissionsFromModels($project->appList()); } } 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) diff --git a/ipf/project.php b/ipf/project.php index aebcf8b..37eb764 100644 --- a/ipf/project.php +++ b/ipf/project.php @@ -82,17 +82,6 @@ final class IPF_Project return $result; } - public function loadModels() - { - foreach ($this->frameworkApps() as $app) - $app->loadModels(); - - IPF_ORM::loadModels(IPF::get('project_path').DIRECTORY_SEPARATOR.'models'); - - foreach ($this->customApps() as $app) - $app->loadModels(); - } - public function run() { if (IPF::get('debug')) { @@ -108,7 +97,6 @@ final class IPF_Project $cli = new IPF_Cli; $cli->run(); } else { - $this->loadModels(); $this->router = new IPF_Router(); $this->router->dispatch(IPF_HTTP_URL::getAction()); }