/** * Create package migration * * @access private * @param string $name * @return string $path */ private function create_package_migration($name) { list($packagename, $name) = explode('/', $name); $skeleton_packages = \Skeleton\Core\Skeleton::get_all(); $package = null; foreach ($skeleton_packages as $skeleton_package) { if ($skeleton_package->name == $packagename) { $package = $skeleton_package; } } if ($package === null) { throw new \Exception('Package ' . $packagename . ' not found'); } if (!file_exists($package->migration_path)) { mkdir($package->migration_path); } $name = preg_replace(array('/\\s/', '/\\.[\\.]+/', '/[^\\w_\\.\\-]/'), array('_', '.', ''), $name); $datetime = date('Ymd_His'); $filename = $datetime . '_' . strtolower($name) . '.php'; $classname = 'Migration_' . $datetime . '_' . ucfirst($name); $namespace_parts = explode('-', $package->name); foreach ($namespace_parts as $key => $namespace_part) { $namespace_parts[$key] = ucfirst($namespace_part); } $namespace = implode('\\', $namespace_parts); $template = file_get_contents(__DIR__ . '/../template/migration.php'); $template = str_replace('%%namespace%%', 'namespace ' . $namespace . ';' . "\n", $template); $template = str_replace('%%classname%%', $classname, $template); file_put_contents($package->migration_path . '/' . $filename, $template); return $package->migration_path . '/' . $filename; }
/** * Add commands * * @access public */ public function add_commands() { /** * Search for other Skeleton packages installed */ $skeletons = \Skeleton\Core\Skeleton::get_all(); /** * Check in every Skeleton package for commands */ foreach ($skeletons as $skeleton) { if (!file_exists($skeleton->path . '/console')) { continue; } $files = scandir($skeleton->path . '/console'); foreach ($files as $file) { if ($file[0] == '.') { continue; } require_once $skeleton->path . '/console/' . $file; $file = str_replace('.php', '', $file); $classname = '\\Skeleton\\Console\\Command\\' . ucfirst(str_replace('skeleton-', '', $skeleton->name)) . '_' . ucfirst($file); $this->add(new $classname()); } } }
/** * Get all migrations * * @access public * @return array $migrations */ public static function get_by_package($package_name = 'project') { if (!file_exists(Config::$migration_directory)) { throw new \Exception('Config::$migration_directory is not set to a valid directory'); } /** * Array with results */ $migrations = []; if ($package_name == 'project') { $files = scandir(Config::$migration_directory, SCANDIR_SORT_ASCENDING); foreach ($files as $key => $file) { if ($file[0] == '.') { unset($files[$key]); continue; } if ($file == 'db_version') { unset($files[$key]); continue; } if (!preg_match("/^\\d{8}_\\d{6}_.*\$/", $file)) { unset($files[$key]); continue; } $parts = explode('_', $file); foreach ($parts as $key => $part) { $parts[$key] = ucfirst($part); } $classname = 'Migration_' . str_replace('.php', '', implode('_', $parts)); include_once Config::$migration_directory . '/' . $file; $migrations[] = new $classname(); } } else { /** * Make a list of all migrations to be execute */ $packages = \Skeleton\Core\Skeleton::get_all(); foreach ($packages as $package) { if ($package->name != $package_name) { continue; } if (!file_exists($package->migration_path)) { continue; } $files = scandir($package->migration_path, SCANDIR_SORT_ASCENDING); foreach ($files as $key => $file) { if ($file[0] == '.') { unset($files[$key]); continue; } if ($file == 'db_version') { unset($files[$key]); continue; } if (!preg_match("/^\\d{8}_\\d{6}_.*\$/", $file)) { unset($files[$key]); continue; } $parts = explode('_', $file); foreach ($parts as $key => $part) { $parts[$key] = ucfirst($part); } $namespace_parts = explode('-', $package->name); foreach ($namespace_parts as $key => $namespace_part) { $namespace_parts[$key] = ucfirst($namespace_part); } $namespace = implode('\\', $namespace_parts); $classname = $namespace . '\\Migration_' . str_replace('.php', '', implode('_', $parts)); include_once $package->migration_path . '/' . $file; $migrations[] = new $classname(); } } } return $migrations; }