-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathModel_Controller.php
72 lines (59 loc) · 2.21 KB
/
Model_Controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
declare(strict_types=1);
namespace n0nag0n;
use CLI;
class Model_Controller extends Base_Controller {
public function indexAction(\Base $fw): void {
$models = $this->getModels();
$this->renderHtml('models'.DIRECTORY_SEPARATOR.'index.htm', [ 'models' => $models ]);
}
public function addAction(\Base $fw): void {
$this->renderHtml('models'.DIRECTORY_SEPARATOR.'add.htm');
}
public function create(\Base $fw, array $args = []): void {
$post = $fw->clean($fw->POST);
$model_name = $post['model'] ?? $args['model'];
$table_name = $post['table'] ?? $args['table'];
if(empty($model_name)) {
throw new \Exception('No model_name specified');
}
$result = $this->createModelFile($model_name, $table_name);
if($result === true) {
$message = 'Model successfully created';
if($this->fw->CLI) {
CLI::success($message);
} else {
$this->fw->flash->addMessage($message, 'success');
$this->fw->reroute('/models', false);
}
} else {
$message = 'Model failed to be created';
if($this->fw->CLI) {
CLI::error($message);
} else {
$this->fw->flash->addMessage($message, 'danger');
$this->fw->reroute('/models/add', false);
}
}
}
public function createModelFile(string $model_name, string $table_name): bool {
$purified_model_name = preg_replace("/\W/", '_', $model_name);
$full_path = $this->fw->PROJECT_BASE_DIR.$this->fw->project_config->model.$purified_model_name.'.php';
if(file_exists($full_path)) {
throw new \Exception('The model already exists');
}
$contents = $this->fw->read(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR.'Model.php');
$contents = str_replace([ '<?php', '#model_name#', '#table_name#' ], [ '#?php', $purified_model_name, $table_name ], $contents);
$contents = \Template::instance()->resolve($contents);
$write_result = $this->fw->write($full_path, str_replace('#?php', '<?php', $contents));
return is_int($write_result);
}
public function getModels() {
$model_dir = $this->fw->PROJECT_BASE_DIR.$this->fw->project_config->model;
$models = glob($model_dir.'*');
foreach($models as &$model) {
$model = [ 'file' => $model, 'base_name' => basename($model, '.php') ];
}
return $models;
}
}