forked from Streetwise-Media/wpmvc2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWPMVC.php
160 lines (140 loc) · 4.41 KB
/
WPMVC.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
152
153
154
155
156
157
158
159
160
<?php
/*
Plugin Name: WPMVC
Description: MVC Framework for working with WordPress
Author: Brian Zeligson
Version: 0.1-alpha
Author URI: http://github.com/beezee
*/
define('DS', DIRECTORY_SEPARATOR);
require_once(dirname(__FILE__).DS.'Autoloader.php');
require_once(dirname(__FILE__).DS.'WPMVC'.DS.'vendor'.DS.'autoload.php');
require_once(dirname(__FILE__).DS.'WPMVC'.DS.'vendor'.DS.'HtmlPurifier'.DS.'HTMLPurifier.standalone.php');
require_once(dirname(__FILE__).DS.'WPMVC'.DS.'vendor'.DS.'_.php');
if (defined('ENVIRONMENT') and ENVIRONMENT === 'development')
{
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once(dirname(__FILE__).DS.'WPMVC'.DS.'vendor'.DS.'php_error.php');
\php_error\reportErrors(array('wordpress' => true));
}
$framework_loader = new SplClassLoader('WPMVC', dirname(__FILE__));
$framework_loader->register();
use \WPMVC\Framework\Router;
use \Illuminate\Database\Capsule\Manager as Capsule;
use \Illuminate\Events\Dispatcher;
use \Illuminate\Container\Container;
class WPMVC extends \WPMVC\Framework\Component
{
private static $_instance;
private static $_is_being_accessed_through_factory = false;
private $_router;
private $_autoloaders = array();
private $_purifier;
public function __construct()
{
if (!WPMVC::is_being_accessed_through_factory())
throw new Exception('WPMVC::__construct cannot be called directly. Use WPMVC::instance');
$this->initialize();
return parent::__construct();
}
private function initialize()
{
$this->_router = new Router();
$this->load_database();
$this->register_vendor_autoloader('Axelarge');
$this->register_vendor_autoloader('Stringy');
$this->set_up_validator();
add_action('plugins_loaded', array($this, 'trigger_loaded'));
add_action('template_include', array($this, 'dispatch_request'));
}
public function trigger_loaded()
{
do_action('wpmvc_loaded');
}
public static function is_being_accessed_through_factory()
{
return self::$_is_being_accessed_through_factory;
}
public static function instance()
{
self::$_is_being_accessed_through_factory = true;
$instance = self::$_instance
?: self::$_instance = new WPMVC();
self::$_is_being_accessed_through_factory = false;
return $instance;
}
private function register_vendor_autoloader($namespace)
{
$this->register_autoloader($namespace, dirname(__FILE__).DS.'WPMVC'.DS.'vendor');
}
public function register_autoloader($namespace, $path)
{
$this->_autoloaders[$namespace] = new SplClassLoader($namespace, $path);
$this->_autoloaders[$namespace]->register();
}
private function set_up_validator()
{
$this->register_vendor_autoloader('Valitron');
\Valitron\Validator::langDir(dirname(__FILE__).DS.'WPMVC'.DS.'vendor'.DS.'Valitron'.DS.'lang');
\Valitron\Validator::addRule('sanitize',
array(new \WPMVC\Framework\Validators\PurifyValidator(), 'run'), '');
\Valitron\Validator::addRule('strip_tags',
array(new \WPMVC\Framework\Validators\StripTagsValidator(), 'run'), '');
}
public function load_database()
{
$capsule = new Capsule();
$capsule->addConnection(array(
'driver' => 'mysql',
'host' => DB_HOST,
'database' => DB_NAME,
'username' => DB_USER,
'password' => DB_PASSWORD,
'charset' => DB_CHARSET,
'collation' => 'utf8_unicode_ci',
'prefix' => ''));
$capsule->setEventDispatcher(new Dispatcher(new Container()));
$capsule->setAsGlobal();
$capsule->bootEloquent();
}
public function get_autoloader($namespace)
{
return $this->_autoloaders[$namespace];
}
public function get_router()
{
return $this->_router;
}
public function get_purifier()
{
if ($this->_purifier)
return $this->_purifier;
$config = HTMLPurifier_Config::createDefault();
return $this->_purifier = new HTMLPurifier($config);
}
private function parse_controller_action_pair_from_route($route)
{
$r = explode('#', $route->getTarget());
$controller_class = $r[0].'Controller';
$controller = new $controller_class();
$r[0] = $controller;
return $r;
}
public function dispatch_request($default_response)
{
if (!$route = $this->get_router()->matchCurrentRequest())
return $default_response;
list($controller, $action) = $this->parse_controller_action_pair_from_route($route);
$controller->before();
call_user_func_array(
array($controller, $action), $route->getParameters());
$controller->after();
return;
}
public function get_base_dir()
{
return dirname(__FILE__).DS.'WPMVC';
}
}
WPMVC::instance();