/
class.textformattermanager.php
executable file
·164 lines (138 loc) · 4.91 KB
/
class.textformattermanager.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
161
162
163
164
<?php
/**
* @package toolkit
*/
/**
* The TextformatterManager class is responsible for managing all Text
* Formatter objects in Symphony. Text Formatter's are stored on the file
* system either in the `TEXTFORMATTERS` folder or provided by
* an extension in an /text-formatters/ folder. All formatters provide one
* simple method, run, which applies the formatting to an unformatted
* string and returns it.
*/
require_once(TOOLKIT . '/class.textformatter.php');
Class TextformatterManager extends Manager{
/**
* An array of all the objects that the Manager is responsible for.
* Defaults to an empty array.
* @var array
*/
protected static $_pool = array();
/**
* Given the filename of a Text Formatter return it's handle. This will remove
* the Symphony convention of `formatter.*.php`
*
* @param string $filename
* The filename of the Text Formatter
* @return string
*/
public function __getHandleFromFilename($filename){
return preg_replace(array('/^formatter./i', '/.php$/i'), '', $filename);
}
/**
* Given a name, returns the full class name of a Text Formatter.
* Text Formatters use a 'formatter' prefix.
*
* @param string $handle
* The formatter handle
* @return string
*/
public function __getClassName($handle){
return 'formatter' . $handle;
}
/**
* Finds a Text Formatter by name by searching the `TEXTFORMATTERS` folder
* and in all installed extension folders and returns the path to it's folder.
*
* @param string $handle
* The handle of the Text Formatter free from any Symphony conventions
* such as `formatter.*.php`
* @return mixed
* If the Text Formatter is found, the function returns the path it's folder,
* otherwise false.
*/
public function __getClassPath($handle){
if(is_file(TEXTFORMATTERS . "/formatter.$handle.php")) return TEXTFORMATTERS;
else{
$extensions = Symphony::ExtensionManager()->listInstalledHandles();
if(is_array($extensions) && !empty($extensions)){
foreach($extensions as $e){
if(is_file(EXTENSIONS . "/$e/text-formatters/formatter.$handle.php")) return EXTENSIONS . "/$e/text-formatters";
}
}
}
return false;
}
/**
* Given a name, return the path to the driver of the Text Formatter.
*
* @see __getClassPath()
* @param string $handle
* The handle of the Text Formatter free from any Symphony conventions
* such as `formatter.*.php`
* @return string
*/
public function __getDriverPath($handle){
return $this->__getClassPath($handle) . "/formatter.$handle.php";
}
/**
* Finds all available Text Formatter's by searching the `TEXTFORMATTERS` folder
* and in all installed extension folders. Returns an associative array of formatters.
*
* @see toolkit.Manager#about()
* @return array
* Associative array of formatters with the key being the handle of the formatter
* and the value being the text formatter's description.
*/
public function listAll(){
$result = array();
$structure = General::listStructure(TEXTFORMATTERS, '/formatter.[\\w-]+.php/', false, 'ASC', TEXTFORMATTERS);
if(is_array($structure['filelist']) && !empty($structure['filelist'])){
foreach($structure['filelist'] as $f){
$f = $this->__getHandleFromFilename($f);
$result[$f] = $this->about($f);
}
}
$extensions = Symphony::ExtensionManager()->listInstalledHandles();
if(is_array($extensions) && !empty($extensions)){
foreach($extensions as $e){
if(!is_dir(EXTENSIONS . "/$e/text-formatters")) continue;
$tmp = General::listStructure(EXTENSIONS . "/$e/text-formatters", '/formatter.[\\w-]+.php/', false, 'ASC', EXTENSIONS . "/$e/text-formatters");
if(is_array($tmp['filelist']) && !empty($tmp['filelist'])){
foreach($tmp['filelist'] as $f){
$f = $this->__getHandleFromFilename($f);
$result[$f] = $this->about($f);
}
}
}
}
ksort($result);
return $result;
}
/**
* Creates an instance of a given class and returns it. Adds the instance
* to the `$_pool` array with the key being the handle.
*
* @param string $handle
* The handle of the Text Formatter to create
* @return TextFormatter
*/
public function &create($handle){
if(!isset(self::$_pool[$handle])){
$classname = $this->__getClassName($handle);
$path = $this->__getDriverPath($handle);
if(!is_file($path)){
throw new Exception(
__(
'Could not find Text Formatter <code>%s</code>. If the Text Formatter was provided by an Extension, ensure that it is installed, and enabled.',
array($name)
)
);
}
if(!class_exists($classname))
require_once($path);
self::$_pool[$handle] = new $classname($this->_Parent);
}
return self::$_pool[$handle];
}
}