-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwigModule.module
More file actions
197 lines (162 loc) · 6.61 KB
/
TwigModule.module
File metadata and controls
197 lines (162 loc) · 6.61 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* ProcessWire 'Hello world' demonstration module
*
* Demonstrates the Module interface and how to add hooks.
*
* ProcessWire 2.x
* Copyright (C) 2010 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class TwigModule extends WireData implements Module {
private $twig;
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo() {
return array(
// The module'ss title, typically a little more descriptive than the class name
'title' => 'Twig',
// version: major, minor, revision, i.e. 100 = 1.0.0
'version' => 001,
// summary is brief description of what this module is
'summary' => 'Adds page controllers and twig tempplates to processwire',
// Optional URL to more information about the module
'href' => 'http://www.processwire.com',
// singular=true: indicates that only one instance of the module is allowed.
// This is usually what you want for modules that attach hooks.
'singular' => true,
// autoload=true: indicates the module should be started with ProcessWire.
// This is necessary for any modules that attach runtime hooks, otherwise those
// hooks won't get attached unless some other code calls the module on it's own.
// Note that autoload modules are almost always also 'singular' (seen above).
'autoload' => true,
);
}
/**
* Initialize the module
*
* ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
* when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
*
*/
public function init()
{
require_once $this->config->paths->root.'vendor/autoload.php';
require_once $this->config->paths->siteModules.'TwigModule/DefaultController.php';
$this->addHookAfter('ProcessPageEdit::buildFormSettings',$this,'addConfig');
$this->addHookBefore('Page::viewable',$this,'beforePageViewable');
$this->addHookBefore('PageRender::renderPage',$this,'beforeRenderPage');
if ( !is_dir($this->config->paths->templates.'twig') ) {
mkdir($this->config->paths->templates.'twig',0777,true);
}
if ( !is_dir($this->config->paths->cache.'twig_cache') ) {
mkdir($this->config->paths->cache.'twig_cache',0777,true);
}
$loader = new Twig_Loader_Filesystem($this->config->paths->templates.'twig');
$this->twig = new Twig_Environment($loader, array(
'cache' => $this->config->paths->cache.'twig_cache',
'auto_reload' => true
));
$function = new Twig_SimpleFunction('wire', function () {
if ( func_get_args() == 0 ) {
$a = 'pages';
} else {
$a = func_get_arg(0);
}
return wire($a);
});
$this->twig->addFunction($function);
}
public function beforeRenderPage($event)
{
// $page = $event->arguments(0)->object;
//
// $templateName = $page->template->name;
//
// $controllerName = 'ExampleController';
//
// if (is_file($this->config->paths->templates.'controllers/'.$controllerName.'.php')) {
// require_once $this->config->paths->templates.'controllers/'.$controllerName.'.php';
// }
//
// $controller = new $controllerName();
// $controller->setTwig($this->twig);
// $controller->setPage($page);
// $controller->setTemplateName($templateName);
//
// echo $controller->render();
// exit;
}
public function beforePageViewable($event)
{
$page = $event->object;
// if template doesn't exist, but controller is selected. Set template file to dummy file to fool the check
if(!$page->template || !$page->template->filenameExists()) {
$controller = 'ExampleController';
$controllerPath = $this->fuel('templates')->path.'controllers';
if ( is_file($controllerPath.'/'.$controller.'.php') ) {
$dummyFile = $this->config->paths->siteModules.'TwigModule/dummy.template.php';
$event->object->template->set('filename',$dummyFile);
}
}
}
public function addConfig(HookEvent $event)
{
$wrapper = $event->return;
// Controller
$field = $this->modules->get('InputfieldSelect');
$field->label = $this->_('Controller'); // Settings: Template field label
$field->attr('id+name', 'controller');
$field->attr('value', $event->page->controller);
$field->required = true;
$field->addOption('','--default');
$files = $this->getDirectoryContent($this->config->paths->templates.'controllers');
foreach($files as $file) {
$pos = strrpos($file,'Controller.php');
if ($pos === false) continue;
$displayName = str_replace('Controller.php','',$file);
$field->addOption($file,$displayName);
}
$wrapper->insertAfter($field,$wrapper->get('template'));
// $field = $this->modules->get('InputfieldSelect');
// $field->label = $this->_('Twig template'); // Settings: Template field label
// $field->attr('id+name', 'twigtemplate');
// $field->attr('value', 1);
// $field->required = true;
//
// $field->addOption('','--default');
//
// $files = $this->getDirectoryContent($this->config->paths->templates.'controllers');
// foreach($files as $file) {
// $pos = strrpos($file,'Controller.php');
// if ($pos === false) continue;
// $displayName = str_replace('Controller.php','',$file);
// $field->addOption($file,$displayName);
// }
//
// $wrapper->insertAfter($field,$wrapper->get('controller'));
$event->return = $wrapper;
}
private function getDirectoryContent($path,$files=array())
{
$relativePath = str_replace($this->config->paths->templates.'controllers','',$path);
$dir = new DirectoryIterator($path);
foreach($dir as $f) {
if ($f->isDot()) continue;
if ($f->isDir()) {
$files = array_merge($files,$this->getDirectoryContent($path.'/'.$f->getFilename()));
} else {
$files[] = trim($relativePath.'/'.$f->getBasename(),'/');
}
}
return $files;
}
}