Created
September 28, 2015 23:35
-
-
Save nimmneun/571d29d4d340f5d2429a to your computer and use it in GitHub Desktop.
quick and dirty console snippet to create basic models & tables
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 | |
/** | |
* @author neun | |
* @since 28.09.2015 20:16 | |
*/ | |
if (PHP_SAPI == 'cli') { | |
if (3 > count($argv)) { | |
print_r(" bake:[type] [name] [property1,property2,...]\n repo\t : Creates new repository". | |
"\n model\t : Creates new model\n [bake:model User id,name,email,created,updated,deleted]"); | |
} else { | |
$pdo = new PDO("mysql:host=127.0.0.1;dbname=keydings;charset=utf8", 'root', ''); | |
new Baker($argv, $pdo); | |
} | |
} | |
class Baker | |
{ | |
private $pdo; | |
private $properties = [ | |
'model' => ['datetime#created', 'datetime#updated', 'bool#deleted'] | |
]; | |
public function __construct($argv, PDO $pdo = null) | |
{ | |
$this->pdo = $pdo; | |
$props = isset($argv[3]) ? trim(array_pop($argv)) : false; | |
$name = array_pop($argv); | |
$type = @array_pop(explode(':', array_pop($argv))); | |
if ($props) { | |
$this->properties['model'] = array_merge(explode(',', $props), $this->properties['model']); | |
} | |
array_unshift($this->properties['model'], 'int#id'); | |
$this->properties['model'] = array_unique($this->properties['model']); | |
$type = strtolower(trim($type)); | |
if (!is_callable([$this, $type])) { | |
print_r('['.$type.'] is not a valid command!'); | |
} else { | |
$this->$type(trim($name)); | |
} | |
} | |
protected function model($name) | |
{ | |
$props = $funcs = $fields = []; | |
$str = "<?php\n/**\n * @since ".date('Y-m-d H:i:s')."\n */\nclass {$name}Model\n{\n"; | |
foreach ($this->properties['model'] as $type_prop) { | |
if (2 == count($pt = explode('#', $type_prop))) { | |
$dbType = $this->getDbType($pt[0]); | |
$p = $pt[1]; | |
} else { | |
$dbType = $this->getDbType(''); | |
$p = $pt[0]; | |
} | |
$fields[$p] = $dbType; | |
$props[] = " private $".camel($p).";\n"; | |
$funcs[] = " public function get".studly($p)."()\n {\n return \$this->".camel($p).";\n }\n"; | |
$funcs[] = " public function set".studly($p)."(\$".camel($p).")\n {\n \$this->".camel($p)." = \$".camel($p).";\n return \$this;\n }\n"; | |
} | |
file_put_contents($name.'Model.php', $str.implode($props)."\n".implode("\n", $funcs)."}\n"); | |
$this->getCreateTable($fields, $name); | |
} | |
private function getDbType($type) | |
{ | |
switch($type) { | |
case 'datetime': | |
return "DATETIME NOT NULL"; | |
case 'decimal': | |
return "DECIMAL(19,5)"; | |
case 'int': | |
return "INT(10)"; | |
case 'bool': | |
return "TINYINT(1) NOT NULL DEFAULT '0'"; | |
default: | |
return "VARCHAR(255) COLLATE 'utf8_unicode_ci'"; | |
} | |
} | |
private function getCreateTable($fields, $table) | |
{ | |
$str = 'CREATE TABLE'.' `'.snake($table)."`(\n"; | |
$columns = []; | |
foreach ($fields as $field => $type) { | |
if (0 == count($columns)) { | |
$type .= ' UNSIGNED NOT NULL AUTO_INCREMENT'; | |
$pk = " PRIMARY KEY (`".$field."`)"; | |
} | |
$columns[] = ' `'.$field.'` '.$type; | |
} | |
$columns[] = $pk; | |
$str .= implode(",\n", $columns)."\n)"; | |
$str .= " COLLATE='utf8_unicode_ci' ENGINE=InnoDB;"; | |
if ($this->pdo instanceof PDO) { | |
$this->pdo->query($str); | |
} else { | |
print_r($str); | |
} | |
} | |
} | |
function camel($string) { | |
return lcfirst(studly($string)); | |
} | |
function snake($string) { | |
return ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', $string)), '_'); | |
} | |
function studly($string) { | |
return str_replace(' ', '', ucwords(str_replace('_', ' ', $string))); | |
} |
Author
nimmneun
commented
Sep 28, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment