forked from newsapps/wordpress-mtv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtv.php
135 lines (103 loc) · 3.69 KB
/
mtv.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
namespace mtv;
require 'Twig/Autoloader.php';
use Twig_Autoloader,
Twig_Environment,
Twig_Loader_Filesystem,
Exception,
mtv\http;
define("MTV_VERSION", "1.0.0");
# For a plugin or theme, i need to load:
# urls
# models
# views
# templates
$GLOBALS['registered_apps'] = array();
function register_app( $name, $path ) {
$views_file = $path . '/views.php';
$urls_file = $path . '/urls.php';
$models_file = $path . '/models.php';
$template_dir = $path . '/templates';
$tags_file = $path . '/templatetags/tags.php';
$funcs_file = $path . '/templatetags/functions.php';
$filter_file = $path . '/templatetags/filters.php';
$app_data = array();
if ( file_exists($views_file) ) $app_data['views'] = $views_file;
if ( file_exists($urls_file) ) $app_data['urls'] = $urls_file;
if ( file_exists($models_file) ) $app_data['models'] = $models_file;
if ( file_exists($template_dir) ) $app_data['templates'] = $template_dir;
if ( file_exists($tags_file) ) $app_data['tags'] = $tags_file;
if ( file_exists($funcs_file) ) $app_data['functions'] = $funcs_file;
if ( file_exists($filter_file) ) $app_data['filters'] = $filter_file;
global $registered_apps;
$registered_apps[$name] = $app_data;
}
/**
* load MTV
* Takes:
* $apps - MTV apps to load. Apps must be registered. Loads in order.
**/
function load( $apps ) {
global $registered_apps;
if ( empty($apps) ) throw new Exception( "No apps have been specified" );
# load our models, views and templates
$template_dirs = array();
foreach ( $apps as $name ) {
$app = $registered_apps[$name];
if ( $app['views'] ) include_once $app['views'];
if ( $app['models'] ) include_once $app['models'];
if ( $app['templates'] ) array_push($template_dirs, $app['templates']);
}
# Time to initialize our template engine
Twig_Autoloader::register();
global $twig;
if (empty($twig)) {
$loader = new Twig_Loader_Filesystem($template_dirs);
if ( DEPLOYMENT_TARGET == "development" ) {
$twig = new Twig_Environment($loader, array('debug' => true));
} else {
$cache_dir = '/tmp/mtv_tmpl_cache';
if ( ini_get('safe_mode') ) {
$cache_dir = __DIR__.'/tmp/mtv_tmpl_cache';
}
# TODO: get a temp directory from php to use for caching
$twig = new Twig_Environment($loader, array(
'cache' => $cache_dir,
'auto_reload' => true
));
}
} else
throw new Exception("MTV is already loaded!");
# now that we have a template engine, load some goodies
foreach ( $apps as $name ) {
$app = $registered_apps[$name];
if ( $app['tags'] ) include_once $app['tags'];
if ( $app['functions'] ) include_once $app['functions'];
if ( $app['filters'] ) include_once $app['filters'];
}
}
/**
* run MTV
* Takes:
* $url - url to run on, probably $_REQUEST['path'] or something
* $url_patterns - url regexes and functions to pass them to
* $apps - MTV apps to load. Apps must be registered. Loads in order.
**/
function run( $kwargs ) {
extract( $kwargs );
load( $apps );
# What's the url for this request?
if ( ! $url )
$url = $_REQUEST['path'];
# globalize our $url_patterns
if ( $url_patterns ) $GLOBALS['url_patterns'] = $url_patterns;
# oh, right, we gotta do something with our url
http\urlresolver( array('url'=>$url, 'url_patterns'=>$url_patterns) );
# Smell ya later.
exit;
}
# load the rest of MTV
include('utils.php');
include('http.php');
include('shortcuts.php');
include('models.php');