/** * Get the current builder output * * @return string */ public function output() { $shema = \DB::fetch("DESCRIBE {$this->table};", array(), null, 'assoc'); if (empty($shema)) { throw new CCException('CCShipyard - The given database table is invalid or does not exist.'); } $class = \CCShipyard::create('class', $this->name, "\\DB\\Model"); $class->add('property', '_table', 'protected static', $this->table, 'The database table name'); // timestamps if ($this->timestamps) { $class->add('line', 2); $class->add('property', '_timestamps', 'protected static', true, 'Allow automatic timestamps'); } // shema $class->add('line', 2); // define the internal types $internal_types = array('bool', 'int', 'float', 'double', 'string'); $match_types = array('tinyint' => 'int', 'text' => 'string', 'varchar' => 'string'); foreach ($shema as $item) { $default = $item['Default']; $field = $item['Field']; if (empty($default)) { $type = \CCStr::cut($item['Type'], '('); if (array_key_exists($type, $match_types)) { $type = $match_types[$type]; } elseif (!in_array($type, $internal_types)) { $type = null; } // The primary key should not contain a default value if ($item['Key'] == 'PRI') { $type = null; } elseif ($item['Type'] == 'tinyint(1)') { $type = 'bool'; } if ($type !== null) { settype($default, $type); } } $buffer = "\t'{$field}'"; if (!is_null($type)) { $buffer .= " => array( '" . $type . "'"; if (!is_null($default)) { $buffer .= ", " . var_export($default, true) . " )"; } } $props[] = $buffer; } $class->add('property', '_defaults', 'protected static', "array(\n" . implode(",\n", $props) . "\n)", 'The ' . $class . ' default properties', false); return $class->output(); }
/** * generate an controller * * exmample: * run shipyard::controller <controller> * run shipyard::controller <controller> <parent_class> * run shipyard::controller <namespace>::<controller> * * @param array $params * @return void */ public function action_controller($params) { $options = \CCDataObject::assign($params); $name = $options->get(0, null); $parent = $options->get(1, null); // get name if we dont have one while (!$name) { $name = $this->read('Please enter the controller name: '); } // fix controller suffix if (substr($name, strlen('Controller') * -1) != 'Controller') { $name .= 'Controller'; } // try to resolve the path if (!($path = \CCPath::controllers(str_replace('_', '/', $name), EXT))) { $this->error('Could not resolve the path. Check if the namespace is registered.'); return; } // parent if (is_null($parent)) { $parent = '\\CCController'; } // view controller if ($options->get('view', false)) { $parent = '\\CCViewController'; } // create the class $class = \CCShipyard::create('class', $name, $parent); // get the actions $actions = array('index'); if ($options->get('actions', false)) { $actions = array_merge($actions, explode(',', $options->get('actions'))); } foreach ($actions as $action) { $action = trim($action); $class->add('function', 'action_' . $action, 'protected', 'echo "' . $name . ' ' . $action . ' action";', ucfirst($action) . " action\n@return void|CCResponse"); $class->add('line', 2); } // add static init if (!$options->get('no-events', false)) { $class->add('function', 'wake', 'protected', '//', "Controller wake\n@return void|CCResponse"); $class->add('line', 2); $class->add('function', 'sleep', 'protected', '//', "Controller wake\n@return void"); } // check for overwrite if (file_exists($path)) { if (!$this->confirm("The class already exists. Do you wish to overwrite it?", true)) { return; } } // write file \CCFile::write($path, $class->output()); }
/** * tests model builder */ public function test_builder_model() { // simple property $builder = CCShipyard::create('dbmodel', 'People', 'people'); $this->assertRegExp('/class People extends/i', $builder->output()); $this->assertRegExp('/protected static \\$_table = \'people\'\\;/i', $builder->output()); $this->assertRegExp('/protected static \\$_defaults = array\\(/i', $builder->output()); $builder->timestamps(); $this->assertRegExp('/protected static \\$_timestamps = true\\;/i', $builder->output()); }