forked from jabranr/lassi
/
Lassi.php
119 lines (99 loc) · 2.31 KB
/
Lassi.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
<?php namespace Lassi;
/**
* Lassi
*
* @author Jabran Rafique <hello@jabran.me>
* @version 0.0.4
* @license MIT License
*/
use \Slim\Slim;
use \Lassi\App\Util;
use \Lassi\App\Database;
use \Lassi\App\Exception\NotFoundException;
class Lassi {
/** @var \Slim\Slim */
protected $app;
/** @var \Lassi\App\Database */
protected $eloquent;
/** @var \Lassi\Lassi */
protected static $instance;
/**
* @param \Slim\Slim $app
* @return \Lassi\Lassi
*/
public function __construct(Slim $app = null) {
$this->setApp($app, array('templates.path' => '../view'));
$this->setEloquent();
return $this;
}
/**
* @return \Lassi\Lassi
*/
public static function getInstance() {
if (! static::$instance instanceof \Lassi\Lassi)
static::$instance = new \Lassi\Lassi;
return static::$instance;
}
/**
* Bootstrap the framework
* @return void
*/
public static function bootstrap() {
/* Load configuration from .dev.env | .dist.env | .env */
Util::setEnvVariables(dirname(__FILE__));
/* Get instances */
$lassi = static::getInstance();
$slim = $lassi->getApp();
/* Add base URL to all views for assets management */
$slim->hook('slim.before', function() use ($slim) {
$slim->view()->appendData(array('baseUrl' => getenv('base_url')));
});
/* Load routes */
static::loadRoutes();
/* Run Slim framework */
$slim->run();
}
/**
* Load framework app routes
* @return void
*/
private static function loadRoutes() {
$routes = dirname(__FILE__) . '/routes.php';
if (!file_exists($routes) || !is_readable($routes))
throw new NotFoundException('Routes not found.');
// Get Slim instance
$app = \Lassi\Lassi::getInstance()->getApp();
require_once $routes;
}
/**
* Set eloquent database
* @return \Lassi\Lassi
*/
public function setEloquent() {
$this->eloquent = Database::getInstance();
return $this;
}
/**
* Get eloquent database instance
* @return \Lassi\App\Database
*/
public function getEloquent() {
return $this->eloquent;
}
/**
* Set app instance
* @param \Slim\Slim $app
* @param array $args
* @return \Lassi\Lassi
*/
public function setApp(Slim $app = null, $args = array()) {
$this->app = $app ? $app : new Slim($args);
}
/**
* Get app instance
* @return \Slim\Slim;
*/
public function getApp() {
return $this->app;
}
}