public static function generate($args, $subfolder = 'default') { $subfolder = trim($subfolder, '/'); if (!is_dir(PKGPATH . 'oil/views/' . $subfolder)) { throw new Exception('The subfolder for scaffolding templates doesn\'t exist or is spelled wrong: ' . $subfolder . ' '); } // Do this first as there is the largest chance of error here Generate::model($args, false); // Go through all arguments after the first and make them into field arrays $fields = array(); foreach (array_slice($args, 1) as $arg) { // Parse the argument for each field in a pattern of name:type[constraint] preg_match('/([a-z0-9_]+):([a-z0-9_]+)(\\[([0-9]+)\\])?/i', $arg, $matches); $fields[] = array('name' => strtolower($matches[1]), 'type' => isset($matches[2]) ? $matches[2] : 'string', 'constraint' => isset($matches[4]) ? $matches[4] : null); } $data['singular'] = $singular = strtolower(array_shift($args)); $data['model'] = $model_name = 'Model_' . Generate::class_name($singular); $data['plural'] = $plural = \Inflector::pluralize($singular); $data['fields'] = $fields; $filepath = APPPATH . 'classes/controller/' . trim(str_replace(array('_', '-'), DS, $plural), DS) . '.php'; $controller = \View::factory($subfolder . '/scaffold/controller', $data); $controller->actions = array(array('name' => 'index', 'params' => '', 'code' => \View::factory($subfolder . '/scaffold/actions/index', $data)), array('name' => 'view', 'params' => '$id = null', 'code' => \View::factory($subfolder . '/scaffold/actions/view', $data)), array('name' => 'create', 'params' => '$id = null', 'code' => \View::factory($subfolder . '/scaffold/actions/create', $data)), array('name' => 'edit', 'params' => '$id = null', 'code' => \View::factory($subfolder . '/scaffold/actions/edit', $data)), array('name' => 'delete', 'params' => '$id = null', 'code' => \View::factory($subfolder . '/scaffold/actions/delete', $data))); // Write controller Generate::create($filepath, $controller, 'controller'); // Create each of the views foreach (array('index', 'view', 'create', 'edit', '_form') as $view) { Generate::create(APPPATH . 'views/' . $plural . '/' . $view . '.php', \View::factory($subfolder . '/scaffold/views/' . $view, $data), 'view'); } // Add the default template if it doesnt exist if (!file_exists($app_template = APPPATH . 'views/template.php')) { Generate::create($app_template, file_get_contents(PKGPATH . 'oil/views/default/template.php'), 'view'); } Generate::build(); }
public function generate($args) { $g = new Generate(); $g->model($args); $singular = strtolower(array_shift($args)); $model_name = ucfirst($singular); $plural = \Inflector::pluralize($singular); $filepath = APPPATH . 'classes/controller/' . $plural . '.php'; $controller = new \View('scaffold/controller'); $controller->name = $plural; $controller->model = $model_name; $controller->actions = array(array('name' => 'index', 'params' => '', 'code' => ' $this->template->title = "' . ucfirst($plural) . '"; $this->template->' . strtolower($plural) . ' = ' . $model_name . '::find(\'all\');'), array('name' => 'view', 'params' => '$id = 0', 'code' => ' $this->template->title = "' . ucfirst($plural) . '"; $this->template->' . strtolower($singular) . ' = ' . $model_name . '::find($id);')); // Write controller if (self::write($filepath, $controller)) { \Cli::write('Created controller'); } }
public function generate($args, $subfolder = 'default') { $subfolder = trim($subfolder, '/'); if (!is_dir(PKGPATH . 'oil/views/' . $subfolder)) { throw new Exception('The subfolder for scaffolding templates doesn\'t exist or is spelled wrong: ' . $subfolder . ' '); } // Do this first as there is the largest chance of error here Generate::model($args); // Go through all arguments after the first and make them into field arrays $fields = array(); foreach (array_slice($args, 1) as $arg) { // Parse the argument for each field in a pattern of name:type[constraint] preg_match('/([a-z0-9_]+):([a-z0-9_]+)(\\[([0-9]+)\\])?/i', $arg, $matches); $fields[] = array('name' => strtolower($matches[1]), 'type' => $matches[2], 'constraint' => isset($matches[4]) ? $matches[4] : null); } $data['singular'] = $singular = strtolower(array_shift($args)); $data['model'] = $model_name = 'Model_' . ucfirst($singular); $data['plural'] = $plural = \Inflector::pluralize($singular); $data['fields'] = $fields; $filepath = APPPATH . 'classes/controller/' . $plural . '.php'; $controller = \View::factory($subfolder . '/scaffold/controller', $data); $controller->actions = array(array('name' => 'index', 'params' => '', 'code' => \View::factory($subfolder . '/scaffold/actions/index', $data)), array('name' => 'view', 'params' => '$id = null', 'code' => \View::factory($subfolder . '/scaffold/actions/view', $data)), array('name' => 'create', 'params' => '$id = null', 'code' => \View::factory($subfolder . '/scaffold/actions/create', $data)), array('name' => 'edit', 'params' => '$id = null', 'code' => \View::factory($subfolder . '/scaffold/actions/edit', $data)), array('name' => 'delete', 'params' => '$id = null', 'code' => \View::factory($subfolder . '/scaffold/actions/delete', $data))); // Write controller if (self::write($filepath, $controller)) { \Cli::write('Created controller: ' . \Fuel::clean_path($filepath)); } // Add the default template if it doesnt exist if (!file_exists($app_template = APPPATH . 'views/template.php')) { copy(PKGPATH . 'oil/views/' . $subfolder . '/template.php', $app_template); chmod($app_template, 0666); } // Create view folder if not already there if (!is_dir($view_folder = APPPATH . 'views/' . $plural . '/')) { mkdir(APPPATH . 'views/' . $plural, 0755); } // Create each of the views foreach (array('index', 'view', 'create', 'edit', '_form') as $view) { static::write($view_file = $view_folder . $view . '.php', \View::factory($subfolder . '/scaffold/views/' . $view, $data)); \Cli::write('Created view: ' . \Fuel::clean_path($view_file)); } }
private function generate_model() { $action = $this->input->post('action'); if ($action == 'model') { require CORE_DIR . 'generate.php'; $generate = new Generate(); $name = $this->input->post('name'); $allow = $this->input->post('allow'); $creatable = $this->input->post('creatable'); $updateable = $this->input->post('updateable'); $deletable = $this->input->post('deletable'); $description = $this->input->post('description'); // Add string quotes for generated file. for ($i = 0; $i < count($allow); $i++) { $allow[$i] = "'" . $allow[$i] . "'"; } $fields = ''; $options = array('name' => ucfirst($name), 'allow' => 'array(' . rtrim(implode(', ', $allow)) . ')', 'creatable' => $creatable ? 'true' : 'false', 'updateable' => $updateable ? 'true' : 'false', 'deletable' => $deletable ? 'true' : 'false', 'description' => $description); // Build query and field strings. for ($i = 0; $i < intval($this->input->post('fields')); $i++) { $id = $i + 1; $field = $this->input->post('field' . $id); $type = $this->input->post('type' . $id); $pk = $this->input->post('pk' . $id); $notnull = $this->input->post('notnull' . $id); $default = $this->input->post('default' . $id); // Build field parameters. Just is just a start point. $options['fields'] .= "'" . $field . "' => array(" . ($pk == 'on' ? "'type' => 'pk'" : "'type' => 'text'") . '), ' . "\n\t\t\t\t"; // Build the query string. $fields .= $field . ' ' . $type . ($pk == 'on' ? ' PRIMARY KEY' : '') . ($notnull == 'on' ? ' NOT NULL' : '') . ($default ? " DEFAULT {$default}, " : ', '); } // Clean up white space characters at string end. $options['fields'] = rtrim(rtrim($options['fields']), ', '); if ($generate->model($name, $options)) { $model = new Model(); $query = 'CREATE TABLE IF NOT EXISTS ' . strtolower($name) . '(' . rtrim($fields, ', ') . ')'; if ($model->query($query)) { $this->session->set('result', 'The model ' . strtolower($name) . '.php was created successfully.'); redirect('admin'); } else { redirect('admin/model'); } } else { redirect('admin/model'); } } else { if ($action == 'display') { $name = $this->input->post('name'); $fields = $this->input->post('fields'); $data = array('name' => $name, 'fields' => intval($fields), 'title' => 'Generate Model'); $template = $this->load->view('admin/model'); $template->render($data); } } }