-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlarapress.php
151 lines (120 loc) · 4.31 KB
/
larapress.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/*
Plugin Name: Larapress
Plugin URI: https://github.com/mrosati84/Larapress
Version: 0.0.1
Description: Use Wordpress with the power of the Laravel Framework
Author: Matteo Rosati <[email protected]>
Author URI: http://mrosati.it
License: GPL
*/
require_once 'vendor/autoload.php';
require_once 'vendor/illuminate/support/Illuminate/Support/helpers.php';
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Database\Capsule\Manager as Capsule;
class Larapress
{
protected $capsule = null;
protected $app = null;
public function __construct()
{
add_action('init', array($this, 'init'));
}
public function init()
{
$this->setupCapsule();
$this->setupPaths();
$this->setupApplication();
$this->setupRouting();
// add_action('pre_get_posts', array($this->context,'preGetPosts'));
}
protected function setupCapsule()
{
$this->capsule = new Capsule;
$this->capsule->addConnection(array(
'driver' => 'mysql',
'host' => DB_HOST,
'database' => DB_NAME,
'username' => DB_USER,
'password' => DB_PASSWORD,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => $table_prefix
));
$this->capsule->bootEloquent();
}
protected function setupPaths()
{
$basePath = str_finish(get_template_directory(), '/');
$controllersDirectory = $basePath . 'controllers';
$modelsDirectory = $basePath . 'models';
Illuminate\Support\ClassLoader::register();
Illuminate\Support\ClassLoader::addDirectories(array(
$controllersDirectory,
$modelsDirectory,
));
}
protected function setupApplication()
{
$this->app = new Illuminate\Container\Container;
Illuminate\Support\Facades\Facade::setFacadeApplication($this->app);
$this->app['app'] = $this->app;
$this->app['env'] = (defined(LARAPRESS_ENV)) ? LARAPRESS_ENV : 'development';
with(new Illuminate\Events\EventServiceProvider($this->app))->register();
with(new Illuminate\Routing\RoutingServiceProvider($this->app))->register();
}
protected function setupRouting()
{
if (file_exists($basePath . 'routes.php')) {
try {
require $basePath . 'routes.php';
$request = Illuminate\Http\Request::createFromGlobals();
$response = $this->app['router']->dispatch($request);
$response->send();
exit(); // exit to skip other wordpress output
} catch (NotFoundHttpException $e) {
// just ignore 404 errors here
}
}
}
public function getApp()
{
return $this->app;
}
/**
* removes from an array of parameters, the parameters that are not needed to call a specific controller action
*
* @param Controller $controller
* @param string $action
* @param array $params
* @return array
*/
protected function reflectParameters($controller, $action, $params)
{
$reflection = new ReflectionClass($controller);
$method = new \ReflectionMethod($controller, $action);
$passed_params = array();
foreach ($method->getParameters() as $method_param) {
if (isset($params[$method_param->getName()])) {
$passed_params[$method_param->getName()] = $params[$method_param->getName()];
}
}
return $passed_params;
}
public function render($controller, $params = array())
{
$controller_parts = explode('@', $controller);
$controller_name = $controller_parts[0];
$action = $controller_parts[1];
if (!class_exists($controller_name)) {
throw new Larapress\Exceptions\ControllerNotFoundException();
}
if (!is_callable($controller_name."::".$action)) {
throw new Larapress\Exceptions\ControllerMethodNotFoundException();
}
$controller = $this->getApp()->make($controller_name);
$passed_params = $this->reflectParameters($controller, $action, $params);
return $controller->callAction($action, $passed_params);
}
}
$Larapress = new Larapress();