-
Notifications
You must be signed in to change notification settings - Fork 11
/
ModelController.php
85 lines (69 loc) · 2.09 KB
/
ModelController.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
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace MABI;
include_once __DIR__ . '/Utilities.php';
include_once __DIR__ . '/Controller.php';
/**
* todo: docs
*/
class ModelController extends Controller {
protected $modelClass = NULL;
/**
* @var \MABI\Model
*/
protected $model = NULL;
/**
* @endpoint ignore
* @return string
*/
public function getModelClass() {
return $this->modelClass;
}
/**
* @endpoint ignore
* @return \MABI\Model
*/
public function getModel() {
return $this->model;
}
/**
* @param $extension Extension
*/
public function __construct(Extension $extension) {
if (empty($this->modelClass)) {
$this->modelClass = ReflectionHelper::getPrefixFromControllerClass(get_called_class());
}
if (empty($this->base)) {
$this->base = Inflector::pluralize(strtolower(ReflectionHelper::stripClassName($this->modelClass)));
}
parent::__construct($extension);
if (class_exists($this->modelClass)) {
$this->model = call_user_func($this->modelClass . '::init', $this->getApp());
}
}
public static function generate($modelClass, Extension $extension) {
/**
* @var $newController \MABI\ModelController
*/
$calledClass = get_called_class();
$newController = new $calledClass($extension);
$newController->modelClass = $modelClass;
$newController->base = Inflector::pluralize(strtolower(ReflectionHelper::stripClassName($modelClass)));
$newController->model = call_user_func($newController->modelClass . '::init', $newController->getApp());
return $newController;
}
public function getDocJSON(Parser $parser) {
$doc = parent::getDocJSON($parser);
$rClass = new \ReflectionClass(get_called_class());
if (in_array('show-model', ReflectionHelper::getDocDirective($rClass->getDocComment(), 'docs'))) {
/**
* @var $model \MABI\Model
*/
$model = call_user_func($this->modelClass . '::init', $this->getApp());
if (empty($doc['models'])) {
$doc['models'] = array();
}
array_unshift($doc['models'], $model->getDocOutput($parser));
}
return $doc;
}
}