Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

...
  • 16 commits
  • 15 files changed
  • 0 commit comments
  • 8 contributors
Commits on Apr 23, 2011
Nick punkstar Discover tasks that are available to the user and display them in the…
… help text.
642fddb
Commits on Jun 02, 2011
Rostislav zbrox adding add_{fieldname}_to_{tablename} and rename_field_{table}_{field…
…name}_to_{fieldname} magic migrations;
9495230
Commits on Jun 03, 2011
Rostislav zbrox rename_field_<table><field>_to_<field> is now rename_field_<field>_to…
…_<field>_in_<table>
1ff4890
Commits on Jun 04, 2011
Rostislav zbrox added drop table magic migration's down action; 6bb2a48
Commits on Jun 09, 2011
Phil Sturgeon philsturgeon Merge pull request #12 from zbrox/feature/new_magic_migrations
Feature/new magic migrations
f7005dc
Commits on Jun 10, 2011
Phil Sturgeon philsturgeon Merge pull request #6 from punkstar/master
Auto-discover refine tasks available to the user.
a8e1387
Commits on Jun 22, 2011
Phil Lavin phil-lavin Don't try to encode object passed through to set_global as this cause…
…s an exception
aa695ed
Harro Verton Merge pull request #14 from phil-lavin/patch-1
Don't try to encode object passed through to set_global
bd7e02f
Commits on Jun 24, 2011
Phil Sturgeon philsturgeon Updated the crap out of plural/singular/foo Scaffolding so that you c…
…an do stuff like $ oil g scaffold admin/foo without it bitching out.
d1c6276
Commits on Jul 01, 2011
Phil Lavin phil-lavin Syntax error fixed 572800d
Harro Verton Merge pull request #15 from phil-lavin/patch-2
Syntax error fixed
541d834
Commits on Jul 14, 2011
Adam Barrett Making more use of the Form::label() in the scaffold builds 0f8e5e1
Commits on Jul 15, 2011
Phil Sturgeon philsturgeon Merge pull request #18 from utahcon/master
A simple change to _form.php to use more native code
4be84aa
Commits on Jul 20, 2011
grekker grekker Tweak to avoid throwing an error when a task is run more than once in…
… a single PHP session
f975e55
Phil Sturgeon philsturgeon Merge pull request #19 from grekker/master
Tweak to avoid throwing an error when a task is run more than once in a single PHP session
8c15846
Commits on Jul 30, 2011
Dan Horrigan dhrrgn Merge branch 'release/v1.0' 5ea2197
25 classes/generate.php
View
@@ -54,7 +54,7 @@ public static function controller($args, $build = true)
// Create views folder and each view file
static::views($args, false);
- $actions or $actions = array('index');
+ $actions or $actions = array('index');
$action_str = '';
foreach ($actions as $action)
@@ -67,11 +67,13 @@ public function action_'.$action.'()
}'.PHP_EOL;
}
+ $extends = \Cli::option('extends', 'Controller_Template');
+
// Build Controller
$controller = <<<CONTROLLER
<?php
-class Controller_{$class_name} extends Controller_Template {
+class Controller_{$class_name} extends {$extends} {
{$action_str}
}
@@ -80,13 +82,14 @@ class Controller_{$class_name} extends Controller_Template {
// Write controller
static::create($filepath, $controller, 'controller');
+
$build and static::build();
}
public static function model($args, $build = true)
{
- $singular = strtolower(array_shift($args));
+ $singular = \Str::lower(array_shift($args));
if (empty($args))
{
@@ -178,7 +181,7 @@ public static function views($args, $build = true)
public static function migration($args, $build = true)
{
// Get the migration name
- $migration_name = strtolower(str_replace('-', '_', array_shift($args)));
+ $migration_name = \Str::lower(str_replace(array('-', '/'), '_', array_shift($args)));
// Check if a migration with this name already exists
if (count($duplicates = glob(APPPATH."migrations/*_{$migration_name}*")) > 0)
@@ -244,8 +247,18 @@ public static function migration($args, $build = true)
{
$subjects = array($matches[0], $matches[2]);
}
+
+ // rename_field_{field}_to_{field}_in_{table} (with underscores in field names)
+ else if (count($matches) >= 5 && in_array('to', $matches) && in_array('in', $matches))
+ {
+ $subjects = array(
+ implode('_', array_slice($matches, array_search('in', $matches)+1)),
+ implode('_', array_slice($matches, 0, array_search('to', $matches))),
+ implode('_', array_slice($matches, array_search('to', $matches)+1, array_search('in', $matches)-2))
+ );
+ }
- // create_{table} (with underscores in table name)
+ // create_{table} or drop_{table} (with underscores in table name)
else if (count($matches) !== 0)
{
$subjects = array(false, implode('_', $matches));
@@ -525,4 +538,4 @@ private static function _clear_args($actions = array())
}
}
-/* End of file oil/classes/generate.php */
+/* End of file oil/classes/generate.php */
151 classes/generate/migration/actions.php
View
@@ -86,13 +86,93 @@ public static function create($subjects, $fields)
// add_{thing}_to_{tablename}
public static function add($subjects, $fields)
{
- return array("\t\t\t// Not yet implemented this migration action", "\t\t\t// Not yet implemented this migration action");
+ $field_up_str = '';
+
+ foreach($fields as $field)
+ {
+ $name = array_shift($field);
+
+ $field_opts = array();
+ foreach($field as $option => $val)
+ {
+ if($val === true)
+ {
+ $field_opts[] = "'$option' => true";
+ }
+ else
+ {
+ if(is_int($val))
+ {
+ $field_opts[] = "'$option' => $val";
+ }
+ else
+ {
+ $field_opts[] = "'$option' => '$val'";
+ }
+ }
+ }
+ $field_opts = implode(', ', $field_opts);
+
+ $field_up_str .= "\t\t\t'$name' => array({$field_opts}),".PHP_EOL;
+ $field_down[] = "'$name'";
+ }
+ $field_down_str = implode(',', $field_down);
+ $up = <<<UP
+ \DBUtil::add_fields('{$subjects[1]}', array(
+\t\t\t$field_up_str
+ ));
+UP;
+ $down = <<<DOWN
+ \DBUtil::drop_fields('{$subjects[1]}', array(
+\t\t\t$field_down_str
+ ));
+DOWN;
+ return array($up, $down);
}
- // rename_field_{fieldname}_to_{newfieldname}
+ // rename_field_{table}_{fieldname}_to_{newfieldname}
public static function rename_field($subjects, $fields)
{
- return array("\t\t\t// Not yet implemented this migration action", "\t\t\t// Not yet implemented this migration action");
+ $column_list = \DB::list_columns($subjects[0], $subjects[1]);
+ $column = $column_list[$subjects[1]];
+
+ switch ($column['type'])
+ {
+ case 'float':
+ $constraint = '\''.$column['numeric_precision'].', '.$column['numeric_scale'].'\'';
+ break;
+ case 'int':
+ $constraint = $column['display'];
+ break;
+ case 'string':
+ switch ($column['data_type'])
+ {
+ case 'binary':
+ case 'varbinary':
+ case 'char':
+ case 'varchar':
+ $constraint = $column['character_maximum_length'];
+ break;
+
+ case 'enum':
+ case 'set':
+ $constraint = '"\''.implode('\',\'',$column['options']).'\'"';
+ break;
+ }
+ break;
+ }
+ $constraint_str = isset($constraint) ? ", 'constraint' => $constraint" : '';
+ $up = <<<UP
+ \DBUtil::modify_fields('{$subjects[0]}', array(
+\t\t\t'{$subjects[1]}' => array('name' => '{$subjects[2]}', 'type' => '{$column['data_type']}'$constraint_str)
+ ));
+UP;
+ $down = <<<DOWN
+ \DBUtil::modify_fields('{$subjects[0]}', array(
+\t\t\t'{$subjects[2]}' => array('name' => '{$subjects[1]}', 'type' => '{$column['data_type']}'$constraint_str)
+ ));
+DOWN;
+ return array($up, $down);
}
// rename_table_{tablename}_to_{newtablename}
@@ -115,10 +195,67 @@ public static function drop($subjects, $fields)
$up = <<<UP
\DBUtil::drop_table('{$subjects[1]}');
UP;
-
- // TODO Create down by looking at the table and building a create
-
- return array($up, '');
+ $field_str = '';
+ $column_list = \DB::list_columns($subjects[1]);
+ foreach ($column_list as $column)
+ {
+ switch ($column['type'])
+ {
+ case 'float':
+ $constraint = '\''.$column['numeric_precision'].', '.$column['numeric_scale'].'\'';
+ break;
+ case 'int':
+ $constraint = $column['display'];
+ break;
+ case 'string':
+ switch ($column['data_type'])
+ {
+ case 'binary':
+ case 'varbinary':
+ case 'char':
+ case 'varchar':
+ $constraint = $column['character_maximum_length'];
+ break;
+
+ case 'enum':
+ case 'set':
+ $constraint = '"\''.implode('\',\'',$column['options']).'\'"';
+ break;
+ }
+ break;
+ }
+ $constraint_str = isset($constraint) ? ", 'constraint' => $constraint" : '';
+ $auto_increment = $column['extra'] == 'auto_increment' ? ", 'auto_increment' => true" : '';
+ $default_str = $column['default'] != null ? ", 'default' => '{$column['default']}'" : ", 'null' => true";
+ if ($column['key'] == 'PRI')
+ {
+ $primary_keys[] = "'{$column['name']}'";
+ }
+ else if ($column['key'] == 'MUL')
+ {
+ $indexes[] = $column['name'];
+ }
+ $field_str .= "\t\t\t'{$column['name']}' => array('type' => '{$column['data_type']}'{$default_str}{$constraint_str}{$auto_increment}),".PHP_EOL;
+ unset($constraint);
+ }
+ $primary_keys = implode(',', $primary_keys);
+ $down = <<<DOWN
+ \DBUtil::create_table('{$subjects[1]}', array(
+$field_str
+ ), array($primary_keys));
+DOWN;
+ $down .= PHP_EOL;
+
+ $active_db = \Config::get('db.active');
+ $table_prefix = \Config::get('db.'.$active_db.'.table_prefix');
+ if (isset($indexes))
+ {
+ foreach ($indexes as $field)
+ {
+ $down .= "\t\t\\DB::query(\"CREATE INDEX {$field}_idx ON {$table_prefix}{$subjects[1]} (`{$field}`)\")->execute();".PHP_EOL;
+ }
+ }
+ return array($up, $down);
}
}
71 classes/refine.php
View
@@ -62,7 +62,7 @@ public static function run($task, $args)
return;
}
- require $file;
+ require_once $file;
$task = '\\Fuel\\Tasks\\'.$task;
@@ -82,24 +82,79 @@ public static function run($task, $args)
public static function help()
{
+ // Build a list of possible tasks for the help output
+ $tasks = self::_discover_tasks();
+ if (count($tasks) > 0)
+ {
+ $output_available_tasks = "";
+
+ foreach ($tasks as $task => $options)
+ {
+ foreach ($options as $option)
+ {
+ $option = ($option == "run") ? "" : ":$option";
+ $output_available_tasks .= " php oil refine $task$option\n";
+ }
+ }
+ } else {
+ $output_available_tasks = " (none found)";
+ }
+
$output = <<<HELP
Usage:
- php oil [r|refine] <taskname>
+ php oil [r|refine] <taskname>
Description:
Tasks are classes that can be run through the the command line or set up as a cron job.
-Examples:
- php oil refine robots [<message>]
- php oil refine robots:protect
-
+Available tasks:
+$output_available_tasks
Documentation:
- http://fuelphp.com/docs/packages/oil/refine.html
+ http://fuelphp.com/docs/packages/oil/refine.html
HELP;
\Cli::write($output);
}
+
+ /**
+ * Find all of the task classes in the system and use reflection to discover the
+ * commands we can call.
+ *
+ * @return array $taskname => array($taskmethods)
+ **/
+ protected static function _discover_tasks() {
+ $result = array();
+ $files = \Fuel::list_files('tasks');
+
+ if (count($files) > 0)
+ {
+ foreach ($files as $file)
+ {
+ $task_name = str_replace('.php', '', basename($file));
+ $class_name = '\\Fuel\\Tasks\\'.$task_name;
+
+ require $file;
+
+ $reflect = new \ReflectionClass($class_name);
+
+ // Ensure we only pull out the public methods
+ $methods = $reflect->getMethods(\ReflectionMethod::IS_PUBLIC);
+
+ $result[$task_name] = array();
+
+ if (count($methods) > 0)
+ {
+ foreach ($methods as $method)
+ {
+ $result[$task_name][] = $method->name;
+ }
+ }
+ }
+ }
+
+ return $result;
+ }
}
-/* End of file oil/classes/refine.php */
+/* End of file oil/classes/refine.php */
20 classes/scaffold.php
View
@@ -46,18 +46,26 @@ public static function generate($args, $subfolder = 'default')
preg_match('/([a-z0-9_]+):([a-z0-9_]+)(\[([0-9]+)\])?/i', $arg, $matches);
$fields[] = array(
- 'name' => strtolower($matches[1]),
+ 'name' => \Str::lower($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_'.\Inflector::classify($singular);
+ $full_thing = array_shift($args);
+ $full_underscores = str_replace(DS, '_', $full_thing);
+
+ // Either something[s] or folder/something[s]
+ $data['controller_uri'] = $controller_uri = \Inflector::pluralize(\Str::lower($full_thing));
+ $data['controller'] = 'Controller_'.\Inflector::classify(\Inflector::pluralize($full_underscores));
+
+ // If a folder is used, the entity is the last part
+ $data['singular'] = $singular = \Inflector::singularize(end(explode(DS, $full_thing)));
+ $data['model'] = $model_name = 'Model_'.\Inflector::classify($full_underscores);
$data['plural'] = $plural = \Inflector::pluralize($singular);
$data['fields'] = $fields;
- $filepath = APPPATH.'classes/controller/'.trim(str_replace(array('_', '-'), DS, $plural), DS).'.php';
+ $filepath = APPPATH.'classes/controller/'.trim(str_replace(array('_', '-'), DS, $controller_uri), DS).'.php';
$controller = \View::factory($subfolder.'/scaffold/controller', $data);
$controller->actions = array(
@@ -94,7 +102,7 @@ public static function generate($args, $subfolder = 'default')
// 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');
+ Generate::create(APPPATH.'views/'.$controller_uri.'/'.$view.'.php', \View::factory($subfolder.'/scaffold/views/'.$view, $data), 'view');
}
// Add the default template if it doesnt exist
@@ -108,4 +116,4 @@ public static function generate($args, $subfolder = 'default')
}
-/* End of file oil/classes/scaffold.php */
+/* End of file oil/classes/scaffold.php */
6 views/default/scaffold/actions/create.php
View
@@ -10,7 +10,7 @@
{
Session::set_flash('notice', 'Added <?php echo $singular; ?> #' . $<?php echo $singular; ?>->id . '.');
- Response::redirect('<?php echo $plural; ?>');
+ Response::redirect('<?php echo $controller_uri; ?>');
}
else
@@ -19,5 +19,5 @@
}
}
- $this->template->title = "<?php echo ucfirst($plural); ?>";
- $this->template->content = View::factory('<?php echo Str::lower($plural);?>/create');
+ $this->template->title = "<?php echo \Str::ucwords($plural); ?>";
+ $this->template->content = View::factory('<?php echo $controller_uri ?>/create');
2  views/default/scaffold/actions/delete.php
View
@@ -10,4 +10,4 @@
Session::set_flash('notice', 'Could not delete <?php echo $singular; ?> #' . $id);
}
- Response::redirect('<?php echo $plural; ?>');
+ Response::redirect('<?php echo $controller_uri; ?>');
6 views/default/scaffold/actions/edit.php
View
@@ -10,7 +10,7 @@
{
Session::set_flash('notice', 'Updated <?php echo $singular; ?> #' . $id);
- Response::redirect('<?php echo $plural; ?>');
+ Response::redirect('<?php echo $controller_uri; ?>');
}
else
@@ -21,8 +21,8 @@
else
{
- $this->template->set_global('<?php echo $singular; ?>', $<?php echo $singular; ?>);
+ $this->template->set_global('<?php echo $singular; ?>', $<?php echo $singular; ?>, false);
}
$this->template->title = "<?php echo ucfirst($plural); ?>";
- $this->template->content = View::factory('<?php echo strtolower($plural);?>/edit');
+ $this->template->content = View::factory('<?php echo $controller_uri; ?>/edit');
4 views/default/scaffold/actions/index.php
View
@@ -1,3 +1,3 @@
- $data['<?php echo strtolower($plural);?>'] = <?php echo $model; ?>::find('all');
+ $data['<?php echo $plural ?>'] = <?php echo $model; ?>::find('all');
$this->template->title = "<?php echo ucfirst($plural); ?>";
- $this->template->content = View::factory('<?php echo strtolower($plural);?>/index', $data);
+ $this->template->content = View::factory('<?php echo $controller_uri ?>/index', $data);
6 views/default/scaffold/actions/view.php
View
@@ -1,4 +1,4 @@
- $data['<?php echo strtolower($singular);?>'] = <?php echo $model; ?>::find($id);
+ $data['<?php echo $singular ?>'] = <?php echo $model ?>::find($id);
- $this->template->title = "<?php echo ucfirst($singular); ?>";
- $this->template->content = View::factory('<?php echo strtolower($plural);?>/view', $data);
+ $this->template->title = "<?php echo ucfirst($singular) ?>";
+ $this->template->content = View::factory('<?php echo $controller_uri ?>/view', $data);
4 views/default/scaffold/controller.php
View
@@ -1,6 +1,6 @@
<?php echo '<?php' ?>
-class Controller_<?php echo ucfirst($plural); ?> extends Controller_Template {
+class <?php echo $controller; ?> extends <?php echo \Cli::option('extends', 'Controller_Template') ?> {
<?php foreach ($actions as $action): ?>
public function action_<?php echo $action['name']; ?>(<?php echo $action['params']; ?>)
@@ -12,4 +12,4 @@ public function action_<?php echo $action['name']; ?>(<?php echo $action['params
}
-/* End of file <?php echo strtolower($plural); ?>.php */
+/* End of file <?php echo $controller_uri; ?>.php */
5 views/default/scaffold/views/_form.php
View
@@ -2,8 +2,9 @@
<?php foreach ($fields as $field): ?>
<p>
- <label for="<?php echo $field['name']; ?>"><?php echo \Inflector::humanize($field['name']); ?>:</label>
<?php
+ echo "<?php echo Form::label('". \Inflector::humanize($field['name'] ."', '{$field['name']}'); ?>\n";
+
switch($field['type']):
case 'text':
@@ -23,4 +24,4 @@
<?php echo '<?php echo Form::submit(); ?>'; ?>
</div>
-<?php echo '<?php echo Form::close(); ?>' ?>
+<?php echo '<?php echo Form::close(); ?>' ?>
6 views/default/scaffold/views/create.php
View
@@ -1,5 +1,5 @@
-<h2 class="first">New <?php echo ucfirst($singular); ?></h2>
+<h2 class="first">New <?php echo \Str::ucfirst($singular); ?></h2>
-<?php echo '<?php'; ?> echo render('<?php echo $plural; ?>/_form'); ?>
+<?php echo '<?php'; ?> echo render('<?php echo $controller_uri ?>/_form'); ?>
<br />
-<p><?php echo '<?php'; ?> echo Html::anchor('<?php echo $plural; ?>', 'Back'); <?php echo '?>'; ?></p>
+<p><?php echo '<?php'; ?> echo Html::anchor('<?php echo $controller_uri ?>', 'Back'); <?php echo '?>'; ?></p>
10 views/default/scaffold/views/edit.php
View
@@ -1,8 +1,8 @@
-<h2 class="first">Editing <?php echo ucfirst($singular); ?></h2>
+<h2 class="first">Editing <?php echo \Str::ucfirst($singular); ?></h2>
-<?php echo '<?php'; ?> echo render('<?php echo $plural; ?>/_form'); ?>
+<?php echo '<?php'; ?> echo render('<?php echo $controller_uri; ?>/_form'); ?>
<br />
<p>
-<?php echo '<?php'; ?> echo Html::anchor('<?php echo $plural; ?>/view/'.$<?php echo $singular; ?>->id, 'View'); <?php echo '?>'; ?> |
-<?php echo '<?php'; ?> echo Html::anchor('<?php echo $plural; ?>', 'Back'); <?php echo '?>'; ?>
-</p>
+<?php echo '<?php'; ?> echo Html::anchor('<?php echo $controller_uri; ?>/view/'.$<?php echo $singular; ?>->id, 'View'); <?php echo '?>'; ?> |
+<?php echo '<?php'; ?> echo Html::anchor('<?php echo $controller_uri; ?>', 'Back'); <?php echo '?>'; ?>
+</p>
10 views/default/scaffold/views/index.php
View
@@ -1,4 +1,4 @@
-<h2 class="first">Listing <?php echo ucfirst($plural); ?></h2>
+<h2 class="first">Listing <?php echo \Str::ucfirst($plural); ?></h2>
<table cellspacing="0">
<tr>
@@ -15,9 +15,9 @@
<td><?php echo '<?php'; ?> echo $<?php echo $singular.'->'.$field['name']; ?>; <?php echo '?>'; ?></td>
<?php endforeach; ?>
<td>
- <?php echo '<?php'; ?> echo Html::anchor('<?php echo $plural; ?>/view/'.$<?php echo $singular; ?>->id, 'View'); <?php echo '?>'; ?> |
- <?php echo '<?php'; ?> echo Html::anchor('<?php echo $plural; ?>/edit/'.$<?php echo $singular; ?>->id, 'Edit'); <?php echo '?>'; ?> |
- <?php echo '<?php'; ?> echo Html::anchor('<?php echo $plural; ?>/delete/'.$<?php echo $singular; ?>->id, 'Delete', array('onclick' => "return confirm('Are you sure?')")); <?php echo '?>'; ?>
+ <?php echo '<?php'; ?> echo Html::anchor('<?php echo $controller_uri; ?>/view/'.$<?php echo $singular; ?>->id, 'View'); <?php echo '?>'; ?> |
+ <?php echo '<?php'; ?> echo Html::anchor('<?php echo $controller_uri; ?>/edit/'.$<?php echo $singular; ?>->id, 'Edit'); <?php echo '?>'; ?> |
+ <?php echo '<?php'; ?> echo Html::anchor('<?php echo $controller_uri; ?>/delete/'.$<?php echo $singular; ?>->id, 'Delete', array('onclick' => "return confirm('Are you sure?')")); <?php echo '?>'; ?>
</td>
</tr>
<?php echo '<?php endforeach; ?>'; ?>
@@ -25,4 +25,4 @@
<br />
-<?php echo '<?php'; ?> echo Html::anchor('<?php echo $plural; ?>/create', 'Add new <?php echo \Inflector::humanize($singular); ?>'); <?php echo '?>'; ?>
+<?php echo '<?php'; ?> echo Html::anchor('<?php echo $controller_uri; ?>/create', 'Add new <?php echo \Inflector::humanize($singular); ?>'); <?php echo '?>'; ?>
4 views/default/scaffold/views/view.php
View
@@ -5,5 +5,5 @@
</p>
<?php endforeach; ?>
-<?php echo '<?php'; ?> echo Html::anchor('<?php echo $plural; ?>/edit/'.$<?php echo $singular; ?>->id, 'Edit'); <?php echo '?>'; ?> |
-<?php echo '<?php'; ?> echo Html::anchor('<?php echo $plural; ?>', 'Back'); <?php echo '?>'; ?>
+<?php echo '<?php'; ?> echo Html::anchor('<?php echo $controller_uri ?>/edit/'.$<?php echo $singular; ?>->id, 'Edit'); <?php echo '?>'; ?> |
+<?php echo '<?php'; ?> echo Html::anchor('<?php echo $controller_uri ?>', 'Back'); <?php echo '?>'; ?>

No commit comments for this range

Something went wrong with that request. Please try again.