/
Application.class.php
134 lines (111 loc) · 2.95 KB
/
Application.class.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
Library::import('recess.framework.controllers.Controller');
Library::import('recess.database.orm.Model');
Library::import('recess.lang.PathFinder');
abstract class Application {
public $name = 'Unnamed Application';
/**
* OVERRIDE THIS with appname.controllers.
*
* @var string
*/
public $controllersPrefix = '';
/**
* OVERRIDE THIS with appname.models.
*
* @var string
*/
public $modelsPrefix = '';
/**
* OVERRIDE THIS with appname/views/
*
* @var PathFinder
*/
public $viewsDir = null;
/**
* OVERRIDE THIS with the path to your app's public files relative to url.base, apps/appname/public/ by default
*
* @var string
*/
public $assetUrl = '';
/**
* OVERRIDE THIS with the routing prefix to your application
*
* @var string
*/
public $routingPrefix = '/';
public $plugins = array();
protected $viewPathFinder = null;
public function addViewPath($path) {
if($this->viewPathFinder == null) {
$this->viewPathFinder = new PathFinder();
}
$this->viewPathFinder->addPath($path);
}
public function viewPathFinder() {
return $this->viewPathFinder;
}
public function findView($view) {
return $this->viewPathFinder->find($view);
}
static protected $runningApplication = null;
static function active() {
return self::$runningApplication;
}
static function activate(Application $application) {
$application->init();
self::$runningApplication = $application;
}
function init() {
$this->addViewPath($_ENV['dir.recess'] . 'recess/framework/ui/parts/');
foreach($this->plugins as $plugin) {
$plugin->init($this);
}
$this->addViewPath($this->viewsDir);
}
function addRoutesToRouter(RtNode $router) {
$classes = $this->listControllers();
foreach($classes as $class) {
if(Library::classExists($this->controllersPrefix . $class)) {
$instance = new $class($this);
} else {
continue;
}
if($instance instanceof Controller) {
$routes = Controller::getRoutes($instance);
if(!is_array($routes)) continue;
foreach($routes as $route) {
$router->addRoute($this, $route, $this->routingPrefix);
}
}
}
return $router;
}
const CONTROLLERS_CACHE_KEY = 'Recess::Framework::App::LstCntrlrs::';
function listControllers() {
$controllers = Cache::get(self::CONTROLLERS_CACHE_KEY . get_class($this));
if($controllers === false) {
$controllers = Library::findClassesIn($this->controllersPrefix);
}
return $controllers;
}
/**
* Deprecated. Use findView instead.
* @return unknown_type
*/
function getViewsDir() {
return $this->viewsDir;
}
function getAssetUrl() {
return $this->assetUrl;
}
function urlTo($methodName) {
$args = func_get_args();
list($controllerName, $methodName) = explode('::', $methodName, 2);
$args[0] = $methodName;
Library::import($this->controllersPrefix . $controllerName);
$controller = new $controllerName($this);
return call_user_func_array(array($controller,'urlTo'),$args);
}
}
?>