Last active
November 13, 2017 09:23
-
-
Save poloey/f24f117cb171e2677c52efe9f93bb51a to your computer and use it in GitHub Desktop.
to create a basic boilerplate code using command line. anyone can create model, view, controller using this tool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* to create mvc structure | |
* @return [void] [it will create whole mvc structure] | |
*/ | |
function create_mvc_structure () { | |
if (! file_exists('core')) { | |
mkdir('core'); | |
} | |
if (! file_exists('models')) { | |
mkdir('models'); | |
} | |
if (! file_exists('views')) { | |
mkdir('views'); | |
} | |
if (! file_exists('controllers')) { | |
mkdir('controllers'); | |
} | |
if (!file_exists('core/db.php')) { | |
$file = fopen('core/db.php', 'w+'); | |
$db = <<<'DB' | |
<?php | |
class db { | |
public function connect($config) | |
{ | |
return new PDO($config['dsn'], $config['username'], $config['password'], $config['options']); | |
} | |
} | |
DB; | |
fwrite($file, $db); | |
fclose($file); | |
} | |
if (!file_exists('config.php')) { | |
$file = fopen('config.php', 'w+'); | |
$config = <<<'CONFIG' | |
<?php | |
$config = [ | |
'dsn' => 'mysql:host=localhost;dbname=auth', | |
'username' => 'root', | |
'password' => '', | |
'options' => [] | |
]; | |
CONFIG; | |
fwrite($file, $config); | |
fclose($file); | |
} | |
} | |
/** | |
* to create a route | |
* @param [string] $r [name of the route] | |
* @return [string] [success message] | |
*/ | |
function create_route ($p) { | |
$file_name = "{$p}.php"; | |
$file = fopen($file_name, 'w+'); | |
$content = <<<TEXT | |
<?php | |
require 'controllers/index.controller.php'; | |
TEXT; | |
fwrite($file, $content); | |
fclose($file); | |
return "$file_name created successfully \n"; | |
} | |
/** | |
* to create a model | |
* @param [string] $p [name of the model] | |
* @return [string] [success message] | |
*/ | |
function create_model ($p) { | |
$file_name = "models/{$p}.php"; | |
$file = fopen($file_name, 'w+'); | |
fwrite($file, '<?php '); | |
fclose($file); | |
return "$file_name created successfully \n"; | |
} | |
/** | |
* create a controller | |
* @param string $p [controller name] | |
* @return [string] [success message] | |
*/ | |
function create_controller ($p) { | |
$file_name = "controllers/{$p}.controller.php"; | |
$file = fopen($file_name, 'w+'); | |
$content = <<<TEXT | |
<?php | |
require 'views/{$p}.view.php'; | |
TEXT; | |
fwrite($file, $content); | |
fclose($file); | |
return "$file_name created successfully \n"; | |
} | |
/** | |
* create a view | |
* @param [string] $p [view name] | |
* @return [string] [success message] | |
*/ | |
function create_view ($p) { | |
$file_name = "views/{$p}.view.php"; | |
$file = fopen($file_name, 'w+'); | |
$content = <<<TEXT | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>${p}</title> | |
</head> | |
<body> | |
{$p} file | |
</body> | |
</html> | |
TEXT; | |
fwrite($file, $content); | |
fclose($file); | |
return "$file_name created successfully \n"; | |
} | |
if (isset($argv[1])) { | |
if (!file_exists('core')) { | |
create_mvc_structure(); | |
} | |
$args = $argv[1]; | |
if (strpos($args, ':') === false) { | |
die ('Please use proper syntax. see documentation '); | |
} | |
list($key, $value) = explode(':', $args); | |
$return_text = ''; | |
if ($key === 'route') { | |
$return_text = create_route($value); | |
} | |
if ($key === 'model') { | |
$return_text = create_model($value); | |
} | |
if ($key === 'controller') { | |
$return_text = create_controller($value); | |
} | |
if ($key === 'view') { | |
$return_text = create_view($value); | |
} | |
if ($key === 'rvc') { | |
$return_text = create_route($value); | |
$return_text .= create_controller($value); | |
$return_text .= create_view($value); | |
} | |
die ($return_text); | |
} | |
create_mvc_structure(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment