title, $template);
$text = str_replace("_ACTION_", $view->action, $text);
// fields
/*
*/
$fields = [];
foreach ($view->fields as $name => $field) {
$html = "\n" .
"\n";
if(!isset($field->type)) {
$field->type = "text";
}
switch($field->type) {
case "switch":
// todo
break;
default:
$html .= "\n";
break;
}
$html .= "
\n";
$fields[] = $html;
}
$text = str_replace("_FIELDS_", implode("\n", $fields), $text);
$this->file_force_contents(resource_path("views/{$view->output}.blade.php"), $text);
echo "Generated " . resource_path("views/{$view->output}.blade.php") . "\n";
$routes[] = "/* SCAFVR */Route::get('{$view->output}', function () { return view('{$view->output}'); });";
}
// write routes
$text = [];
$file = fopen(base_path("routes/web.php"), "r");
while (!feof($file)) {
$line = rtrim(fgets($file));
if(strpos($line, "/* SCAFVR */") === FALSE) {
$text[] = $line;
}
}
$text = implode("\n", $text);
fclose($file);
$text = str_replace("/* __SCAFFOLD_VIEW_ROUTES__ */", implode("\n", $routes), $text);
file_put_contents(base_path("routes/web.php"), $text);
echo "Updated " . base_path("routes/web.php") . "\n";
}
private function file_force_contents($dir, $contents){
$parts = explode('/', $dir);
$file = array_pop($parts);
$dir = '';
foreach($parts as $part)
if(!is_dir($dir .= "/$part")) mkdir($dir);
file_put_contents("$dir/$file", $contents);
}
}