Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generador de controladores, acciones y sus vistas by error.
<?php
/**
*/
class GenerateController extends AdminController
{
public function index()
{
}
public function action($controller, $action, $with_module='')
{
if ($with_module) $action .= "/$with_module";
(new Generate)->action("$controller/$action");
exit( Redirect::to("$controller/$action") );
}
public function controller($controller, $with_module='')
{
if ($with_module) $controller .= "/$with_module";
(new Generate)->controller($controller);
exit( Redirect::to($controller) );
}
public function model()
{
}
public function view($controller, $view, $with_module='')
{
if ($with_module) $view .= "/$with_module";
(new Generate)->view("$controller/$view");
exit( Redirect::to( dirname("$controller/$view") ) );
}
}

###No sigas perdiendo el tiempo creando controladores, acciones y vistas y empieza a programar pensando solo en programar; sigue los siguientes pasos:

<!--1) crear un enlace como este en CORE/views/errors/no_controller.phtml-->
cambia esto:
Crea la clase <em><?= $Controller;?>Controller</em>
por esto:
<a href="/admin/generate/controller/<?=$controller_path?>">Crea la clase <em><?= $Controller ?>Controller</em></a>

<!--2) crear un enlace como este en CORE/views/errors/no_action.phtml-->
cambia esto:
Crea el método <em><?= $action?></em>
por esto:
<a href="/admin/generate/action/<?="$controller_path/$action"?>">Crea el método <em><?= $action?></em></a>

<!--3) crear un enlace como este en CORE/views/errors/no_view.phtml-->
por esto:
Crea la vista
cambia esto:
<a href="/admin/generate/view/<?=View::getPath()?>">Crea la vista</a>

<!--4) copia el controlador generate_controller.php en APP/controllers/admin/-->

<!--5) copia la libreria generate.php en APP/libs/-->

######Y más adelante ampliaré esto para que pueda generar modelos by error o ¿te atreves tu a hacerlo, siguiendo este patrón?

#Enjoy !!

<?php
/**
* requisitos:
* el template para los controladores llamalo controller.php
* el template para las acciones llamalo action.php
*/
class Generate
{
public $templates = CORE_PATH . 'console/generators/';
public function action($target)
{
$basename = basename($target);
$dirname = dirname($target);
$action = $this->incFile("$this->templates/action.php", ['method'=>$basename]);
$file = APP_PATH . "controllers/$dirname" . '_controller.php';
$contents = file_get_contents($file);
$contents = explode('}', $contents, -1);
$contents = implode('}', $contents);
$contents = "$contents$action" . PHP_EOL . '}';
$contents = $this->eol($contents);
file_put_contents($file, $contents);
}
public function controller($target)
{
$basename = basename($target);
$dirname = dirname($target);
if ( ! file_exists(APP_PATH . "controllers/$dirname") )
mkdir(APP_PATH . "controllers/$dirname", 0777, true);
$controller = $this->incFile("$this->templates/controller.php", ['class'=>$basename]);
$contents = '<?php' . PHP_EOL . $controller;
$contents = $this->eol($contents);
file_put_contents(APP_PATH . "controllers/$target" . '_controller.php', $contents);
}
public function view($target)
{
$dirname = dirname($target);
if ( ! file_exists(APP_PATH . "views/$dirname") )
mkdir(APP_PATH . "views/$dirname", 0777, true);
file_put_contents(APP_PATH . "views/$target", '');
}
public static function eol($s)
{
if ( strstr($s, "\r\n") ) $s = str_replace("\r\n", "\n" , $s);
if ( strstr($s, "\r") ) $s = str_replace("\r", "\n" , $s);
return str_replace("\n", "\r\n" , $s);
}
public static function incFile($name, $vars=NULL)
{
if ( is_file($name) )
{
ob_start();
if ($vars) extract($vars, EXTR_OVERWRITE);
include $name;
return ob_get_clean();
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment