symphonycms /
symphony-2
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @package toolkit |
||
| 4 | */ |
||
| 5 | /** |
||
| 6 | * The DatasourceManager class is responsible for managing all Datasource objects |
||
| 7 | * in Symphony. Datasources's are stored on the file system either in the |
||
| 8 | * `WORKSPACE . /data-sources` folder or provided by an extension in `EXTENSIONS . /./data-sources` folder. |
||
| 9 | * Datasources are run from the Frontend to commonly output Entries and parameters, |
||
| 10 | * however there any different types of Datasource that query external sources for data. |
||
| 11 | * Typically, a Datasource returns XML. |
||
| 12 | */ |
||
| 13 | |||
| 14 | class DatasourceManager implements FileResource |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Given the filename of a Datasource, return its handle. This will remove |
||
| 18 | * the Symphony convention of `data.*.php` |
||
| 19 | * |
||
| 20 | * @param string $filename |
||
| 21 | * The filename of the Datasource |
||
| 22 | * @return string |
||
| 23 | */ |
||
| 24 | public static function __getHandleFromFilename($filename) |
||
| 25 | { |
||
| 26 | return preg_replace(array('/^data./i', '/.php$/i'), '', $filename); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Given a name, returns the full class name of an Datasources. Datasources |
||
| 31 | * use a 'datasource' prefix. |
||
| 32 | * |
||
| 33 | * @param string $handle |
||
| 34 | * The Datasource handle |
||
| 35 | * @return string |
||
| 36 | */ |
||
| 37 | public static function __getClassName($handle) |
||
| 38 | { |
||
| 39 | return 'datasource' . $handle; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Finds a Datasource by name by searching the data-sources folder in the |
||
| 44 | * workspace and in all installed extension folders and returns the path |
||
| 45 | * to it's folder. |
||
| 46 | * |
||
| 47 | * @param string $handle |
||
| 48 | * The handle of the Datasource free from any Symphony conventions |
||
| 49 | * such as `data.*.php` |
||
| 50 | * @return string|boolean |
||
| 51 | * If the datasource is found, the function returns the path it's folder, otherwise false. |
||
| 52 | */ |
||
| 53 | public static function __getClassPath($handle) |
||
| 54 | { |
||
| 55 | if (is_file(DATASOURCES . "/data.$handle.php")) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
As per coding-style, please use concatenation or
sprintf for the variable $handle instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
|
|||
| 56 | return DATASOURCES; |
||
| 57 | } else { |
||
| 58 | $extensions = Symphony::ExtensionManager()->listInstalledHandles(); |
||
| 59 | |||
| 60 | if (is_array($extensions) && !empty($extensions)) { |
||
| 61 | foreach ($extensions as $e) { |
||
| 62 | if (is_file(EXTENSIONS . "/$e/data-sources/data.$handle.php")) { |
||
|
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $e instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
As per coding-style, please use concatenation or
sprintf for the variable $handle instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
|
|||
| 63 | return EXTENSIONS . "/$e/data-sources"; |
||
|
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $e instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
|
|||
| 64 | } |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | return false; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Given a name, return the path to the Datasource class |
||
| 74 | * |
||
| 75 | * @see DatasourceManager::__getClassPath() |
||
| 76 | * @param string $handle |
||
| 77 | * The handle of the Datasource free from any Symphony conventions |
||
| 78 | * such as `data.*.php` |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | public static function __getDriverPath($handle) |
||
| 82 | { |
||
| 83 | return self::__getClassPath($handle) . "/data.$handle.php"; |
||
|
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $handle instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
|
|||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Finds all available Datasources by searching the data-sources folder in |
||
| 88 | * the workspace and in all installed extension folders. Returns an |
||
| 89 | * associative array of data sources. |
||
| 90 | * |
||
| 91 | * @see toolkit.Manager#about() |
||
| 92 | * @return array |
||
| 93 | * Associative array of Datasources with the key being the handle of the |
||
| 94 | * Datasource and the value being the Datasource's `about()` information. |
||
| 95 | */ |
||
| 96 | public static function listAll() |
||
| 97 | { |
||
| 98 | $result = array(); |
||
| 99 | $structure = General::listStructure(DATASOURCES, '/data.[\\w-]+.php/', false, 'ASC', DATASOURCES); |
||
|
0 ignored issues
–
show
Are you sure the assignment to
$structure is correct as General::listStructure(D...se, 'ASC', DATASOURCES) targeting General::listStructure() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 100 | |||
| 101 | if (is_array($structure['filelist']) && !empty($structure['filelist'])) { |
||
| 102 | foreach ($structure['filelist'] as $f) { |
||
| 103 | $f = self::__getHandleFromFilename($f); |
||
| 104 | |||
| 105 | if ($about = self::about($f)) { |
||
| 106 | $classname = self::__getClassName($f); |
||
| 107 | $env = array(); |
||
| 108 | $class = new $classname($env, false); |
||
| 109 | |||
| 110 | $about['can_parse'] = method_exists($class, 'allowEditorToParse') |
||
| 111 | ? $class->allowEditorToParse() |
||
|
0 ignored issues
–
show
|
|||
| 112 | : false; |
||
| 113 | |||
| 114 | $about['source'] = method_exists($class, 'getSource') |
||
| 115 | ? $class->getSource() |
||
|
0 ignored issues
–
show
|
|||
| 116 | : null; |
||
| 117 | |||
| 118 | $result[$f] = $about; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | $extensions = Symphony::ExtensionManager()->listInstalledHandles(); |
||
| 124 | |||
| 125 | if (is_array($extensions) && !empty($extensions)) { |
||
| 126 | foreach ($extensions as $e) { |
||
| 127 | if (!is_dir(EXTENSIONS . "/$e/data-sources")) { |
||
|
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $e instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
|
|||
| 128 | continue; |
||
| 129 | } |
||
| 130 | |||
| 131 | $tmp = General::listStructure(EXTENSIONS . "/$e/data-sources", '/data.[\\w-]+.php/', false, 'ASC', EXTENSIONS . "/$e/data-sources"); |
||
|
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $e instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Are you sure the assignment to
$tmp is correct as General::listStructure(E...'/'.$e.'/data-sources') targeting General::listStructure() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 132 | |||
| 133 | if (is_array($tmp['filelist']) && !empty($tmp['filelist'])) { |
||
| 134 | foreach ($tmp['filelist'] as $f) { |
||
| 135 | $f = self::__getHandleFromFilename($f); |
||
| 136 | |||
| 137 | if ($about = self::about($f)) { |
||
| 138 | $about['can_parse'] = false; |
||
| 139 | $about['source'] = null; |
||
| 140 | $result[$f] = $about; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | ksort($result); |
||
| 148 | return $result; |
||
| 149 | } |
||
| 150 | |||
| 151 | public static function about($name) |
||
| 152 | { |
||
| 153 | $classname = self::__getClassName($name); |
||
| 154 | $path = self::__getDriverPath($name); |
||
| 155 | |||
| 156 | if (!@file_exists($path)) { |
||
| 157 | return false; |
||
| 158 | } |
||
| 159 | |||
| 160 | require_once($path); |
||
| 161 | |||
| 162 | $handle = self::__getHandleFromFilename(basename($path)); |
||
| 163 | $env = array(); |
||
| 164 | $class = new $classname($env, false); |
||
| 165 | |||
| 166 | try { |
||
| 167 | $method = new ReflectionMethod($classname, 'about'); |
||
| 168 | $about = $method->invoke($class); |
||
| 169 | } catch (ReflectionException $e) { |
||
| 170 | $about = array(); |
||
| 171 | } |
||
| 172 | |||
| 173 | return array_merge($about, array('handle' => $handle)); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Creates an instance of a given class and returns it. |
||
| 178 | * |
||
| 179 | * @param string $handle |
||
| 180 | * The handle of the Datasource to create |
||
| 181 | * @param array $env |
||
| 182 | * The environment variables from the Frontend class which includes |
||
| 183 | * any params set by Symphony or Events or by other Datasources |
||
| 184 | * @param boolean $process_params |
||
| 185 | * @throws Exception |
||
| 186 | * @return Datasource |
||
| 187 | */ |
||
| 188 | public static function create($handle, array $env = null, $process_params = true) |
||
|
0 ignored issues
–
show
|
|||
| 189 | { |
||
| 190 | $classname = self::__getClassName($handle); |
||
| 191 | $path = self::__getDriverPath($handle); |
||
| 192 | |||
| 193 | if (!is_file($path)) { |
||
| 194 | throw new Exception( |
||
| 195 | __('Could not find Data Source %s.', array('<code>' . $handle . '</code>')) |
||
| 196 | . ' ' . __('If it was provided by an Extension, ensure that it is installed, and enabled.') |
||
| 197 | ); |
||
| 198 | } |
||
| 199 | |||
| 200 | if (!class_exists($classname)) { |
||
| 201 | require_once $path; |
||
| 202 | } |
||
| 203 | |||
| 204 | return new $classname($env, $process_params); |
||
| 205 | } |
||
| 206 | } |
||
| 207 |