/** * Parse the args list retrieved from the command line * * @param array $args Argument list * @return array Parsed argument list * @access private * @author Aziz Light */ private static function parse($args) { $parsed_args = array(); // remove the script name from the commands list. array_shift($args); if (!empty($args) && in_array($args[0], self::$valid_tasks)) { $parsed_args['command'] = $args[0]; array_shift($args); } else { if (!empty($args)) { $args[0] = self::check_and_get_command_alias($args[0]); if (empty($args[0])) { throw new InvalidArgumentException("Invalid task", INVALID_TASK_EXCEPTION); } else { # FIXME: Try to remove the duplication here. # NOTE: This is a good case for a goto: :-{) $parsed_args['command'] = $args[0]; array_shift($args); } } else { throw new InvalidArgumentException("Invalid task", INVALID_TASK_EXCEPTION); } } if (!empty($args) && array_key_exists($parsed_args['command'], self::$valid_subjects) && in_array($args[0], self::$valid_subjects[$parsed_args['command']])) { $parsed_args['subject'] = $args[0]; array_shift($args); } else { if (self::has_subjects($parsed_args['command'])) { if ($parsed_args['command'] !== 'migrate' && !in_array($args[0], self::$valid_subjects[$parsed_args['command']])) { throw new InvalidArgumentException("Invalid subject", INVALID_SUBJECT_EXCEPTION); } } } // The bootstrap command is the only one that is called without any additional args if (self::has_a_name($parsed_args['command'])) { if (empty($args)) { throw new InvalidArgumentException("Missing name", MISSING_NAME_EXCEPTION); } $unparsed_name = array_shift($args); // NOTE: I have to use this $tmp variable in order to avoid getting a "Stict Standards" notice by php $tmp = explode(DIRECTORY_SEPARATOR, $unparsed_name); $parsed_args['name'] = end($tmp); if ($parsed_args['command'] != 'new_project') { $parsed_args['filename'] = ApplicationHelpers::underscorify($unparsed_name) . ".php"; } } if (!empty($args)) { if ($parsed_args['command'] === 'generate') { if (in_array($parsed_args['subject'], array('model', 'migration'))) { $parsed_args['extra'] = self::parse_table_columns($args); } else { $parsed_args['extra'] = $args; } } else { throw new InvalidArgumentException('Too many arguments'); } } return $parsed_args; }
/** * Parse the args list retrieved from the command line * * @param array $args Argument list * @return array Parsed argument list * @access private * @author Aziz Light */ private static function parse($args) { $parsed_args = array(); // remove the script name from the commands list. array_shift($args); if (!empty($args) && in_array($args[0], self::$valid_tasks)) { $parsed_args['command'] = $args[0]; array_shift($args); } else { if (!empty($args)) { $args[0] = self::check_and_get_command_alias($args[0]); if (empty($args[0])) { throw new InvalidArgumentException("Invalid task", INVALID_TASK_EXCEPTION); } else { # FIXME: Try to remove the duplication here. # NOTE: This is a good case for a goto: :-{) $parsed_args['command'] = $args[0]; array_shift($args); } } else { throw new InvalidArgumentException("Invalid task", INVALID_TASK_EXCEPTION); } } if (!empty($args) && array_key_exists($parsed_args['command'], self::$valid_subjects) && in_array($args[0], self::$valid_subjects[$parsed_args['command']])) { $parsed_args['subject'] = $args[0]; array_shift($args); } else { if (self::has_subjects($parsed_args['command'])) { if ($parsed_args['command'] !== 'migrate' && (empty($args) || !in_array($args[0], self::$valid_subjects[$parsed_args['command']]))) { throw new InvalidArgumentException("Invalid subject", INVALID_SUBJECT_EXCEPTION); } } } // The bootstrap command is the only one that is called without any additional args if (self::has_a_name($parsed_args['command'])) { if (empty($args)) { throw new InvalidArgumentException("Missing name", MISSING_NAME_EXCEPTION); } $unparsed_name = array_shift($args); // NOTE: I have to use this $tmp variable in order to avoid getting a "Stict Standards" notice by php $tmp = explode(DIRECTORY_SEPARATOR, $unparsed_name); $parsed_args['name'] = array_pop($tmp); $parsed_args['subdirectories'] = join(DIRECTORY_SEPARATOR, $tmp); if (Inflector::is_plural($parsed_args['name']) && $parsed_args['command'] === 'generate' && in_array($parsed_args['subject'], array('model', 'scaffold'), TRUE)) { $message = 'Fire thinks that your model name is plural.' . PHP_EOL; $message .= 'If that\'s the case then you\'re probably doing it wrong...' . PHP_EOL; $message .= 'Read the section on generating models or scaffolds in' . PHP_EOL; $message .= 'the README for more info on the subject.' . PHP_EOL . PHP_EOL; fwrite(STDOUT, $message); } if ($parsed_args['command'] != 'new_project') { $parsed_args['filename'] = ApplicationHelpers::underscorify($parsed_args['name']) . ".php"; } } if (!empty($args)) { if ($parsed_args['command'] === 'generate') { if (in_array($parsed_args['subject'], array('model', 'migration', 'scaffold'))) { $parsed_args['extra'] = self::parse_table_columns($args); } else { $parsed_args['extra'] = $args; } } else { throw new InvalidArgumentException('Too many arguments'); } } return $parsed_args; }
/** * Create a migration file * * @access private * @return void * @author Aziz Light **/ private function migration() { $location = $this->args['location'] . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR; if (!is_dir($location)) { mkdir($location); } $backtrace = debug_backtrace(); $calling_function = $backtrace[1]['function']; if ($calling_function === "model") { $args = array('class_name' => 'Migration_Add_' . Inflector::pluralize($this->args['name']), 'table_name' => Inflector::pluralize(strtolower($this->args['name'])), 'filename' => 'add_' . Inflector::pluralize(ApplicationHelpers::underscorify($this->args['name'])) . '.php', 'application_folder' => $this->args['application_folder'], 'parent_class' => $this->args['parent_migration'], 'extra' => $this->extra); $template_name = 'migration'; } else { $args = array('class_name' => 'Migration_' . $this->args['name'], 'table_name' => $this->get_table_name_out_of_migration_name(), 'filename' => $this->args['filename'], 'application_folder' => $this->args['application_folder'], 'parent_class' => $this->args['parent_migration'], 'extra' => $this->extra); $template_name = 'empty_migration'; } $template = new TemplateScanner($template_name, $args); $migration = $template->parse(); $migration_number = MigrationHelpers::get_migration_number($this->args['location']); $filename = $location . $migration_number . '_' . $args['filename']; $potential_duplicate_migration_filename = MigrationHelpers::decrement_migration_number($migration_number) . '_' . $args['filename']; $potential_duplicate_migration = $location . $potential_duplicate_migration_filename; $message = "\t"; if (file_exists($potential_duplicate_migration)) { $message .= 'Migration already exists : '; if (php_uname("s") !== "Windows NT") { $message = ApplicationHelpers::colorize($message, 'light_blue'); } $message .= $this->args['application_folder'] . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR . $potential_duplicate_migration_filename; } else { if (file_put_contents($filename, $migration) && MigrationHelpers::add_migration_number_to_config_file($this->args['location'], $migration_number)) { $message .= 'Created Migration: '; if (php_uname("s") !== "Windows NT") { $message = ApplicationHelpers::colorize($message, 'green'); } $message .= $this->args['application_folder'] . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR . $migration_number . '_' . $args['filename']; } else { $message .= 'Unable to create migration: '; if (php_uname("s") !== "Windows NT") { $message = ApplicationHelpers::colorize($message, 'red'); } $message .= $this->args['application_folder'] . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR . $migration_number . '_' . $this->args['filename']; } } fwrite(STDOUT, $message . PHP_EOL); return; }