Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 200 lines (166 sloc) 5.722 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 198 199
<?php
/**
* @package toolkit
*/
/**
* The EventManager class is responsible for managing all Event objects
* in Symphony. Event's are stored on the file system either in the
* /workspace/events folder or provided by an extension in an /events folder.
* Events run from the Frontend usually to add new entries to the system, but
* they are not limited to that facet.
*/

require_once(TOOLKIT . '/class.event.php');
require_once(FACE . '/interface.fileresource.php');

Class EventManager implements FileResource {

/**
* Given the filename of an Event return it's handle. This will remove
* the Symphony convention of `event.*.php`
*
* @param string $filename
* The filename of the Event
* @return string
*/
public static function __getHandleFromFilename($filename){
return preg_replace(array('/^event./i', '/.php$/i'), '', $filename);
}

/**
* Given a name, returns the full class name of an Event. Events
* use an 'event' prefix.
*
* @param string $handle
* The Event handle
* @return string
*/
public static function __getClassName($handle){
return 'event' . $handle;
}

/**
* Finds an Event by name by searching the events folder in the workspace
* and in all installed extension folders and returns the path to it's folder.
*
* @param string $handle
* The handle of the Event free from any Symphony conventions
* such as `event.*.php`
* @return mixed
* If the Event is found, the function returns the path it's folder, otherwise false.
*/
public static function __getClassPath($handle){
if(is_file(EVENTS . "/event.$handle.php")) return EVENTS;
else{

$extensions = Symphony::ExtensionManager()->listInstalledHandles();

if(is_array($extensions) && !empty($extensions)){
foreach($extensions as $e){
if(is_file(EXTENSIONS . "/$e/events/event.$handle.php")) return EXTENSIONS . "/$e/events";
}
}
}

return false;
}

/**
* Given a name, return the path to the Event class
*
* @see toolkit.EventManager#__getClassPath()
* @param string $handle
* The handle of the Event free from any Symphony conventions
* such as event.*.php
* @return string
*/
public static function __getDriverPath($handle){
return self::__getClassPath($handle) . "/event.$handle.php";
}

/**
* Finds all available Events by searching the events folder in the workspace
* and in all installed extension folders. Returns an associative array of Events.
*
* @see toolkit.Manager#about()
* @return array
* Associative array of Events with the key being the handle of the Event
* and the value being the Event's `about()` information.
*/
public static function listAll(){
$result = array();
$structure = General::listStructure(EVENTS, '/event.[\\w-]+.php/', false, 'ASC', EVENTS);

if(is_array($structure['filelist']) && !empty($structure['filelist'])){
foreach($structure['filelist'] as $f){
$f = self::__getHandleFromFilename($f);

if($about = self::about($f)){
$classname = self::__getClassName($f);
$can_parse = false;
$source = null;
$env = array();
$class = new $classname($env);

try {
$method = new ReflectionMethod($classname, 'allowEditorToParse');
$can_parse = $method->invoke($class);
}
catch (ReflectionException $e) {}

try {
$method = new ReflectionMethod($classname, 'getSource');
$source = $method->invoke($class);
}
catch (ReflectionException $e) {}

$about['can_parse'] = $can_parse;
$about['source'] = $source;
$result[$f] = $about;
}
}
}

$extensions = Symphony::ExtensionManager()->listInstalledHandles();

if(is_array($extensions) && !empty($extensions)){
foreach($extensions as $e){

if(!is_dir(EXTENSIONS . "/$e/events")) continue;

$tmp = General::listStructure(EXTENSIONS . "/$e/events", '/event.[\\w-]+.php/', false, 'ASC', EXTENSIONS . "/$e/events");

if(is_array($tmp['filelist']) && !empty($tmp['filelist'])){
foreach($tmp['filelist'] as $f){
$f = self::__getHandleFromFilename($f);

if($about = self::about($f)){
$about['can_parse'] = false;
$result[$f] = $about;
}
}
}
}
}

ksort($result);
return $result;
}

public static function about($name) {
$classname = self::__getClassName($name);
$path = self::__getDriverPath($name);

if(!@file_exists($path)) return false;

require_once($path);

$handle = self::__getHandleFromFilename(basename($path));
$env = array();
$class = new $classname($env);

try {
$method = new ReflectionMethod($classname, 'about');
$about = $method->invoke($class);
}
catch (ReflectionException $e){
$about = array();
}

return array_merge($about, array('handle' => $handle));
}

/**
* Creates an instance of a given class and returns it.
*
* @param string $handle
* The handle of the Event to create
* @param array $env
* The environment variables from the Frontend class which includes
* any params set by Symphony or Datasources or by other Events
* @return Event
*/
public static function create($handle, array $env = null){
$classname = self::__getClassName($handle);
$path = self::__getDriverPath($handle);

if(!is_file($path)){
throw new Exception(
__('Could not find Event %s.', array('<code>' . $handle . '</code>'))
. ' ' . __('If it was provided by an Extension, ensure that it is installed, and enabled.')
);
}

if(!class_exists($classname)) require_once($path);

return new $classname($env);
}

}
Something went wrong with that request. Please try again.