Commit 1d92c67e by Scott

More minor improvements to metadata system

Allowed glob() to sort results
parent 89b717fa
...@@ -22,34 +22,22 @@ ...@@ -22,34 +22,22 @@
class Q2A_Util_Metadata class Q2A_Util_Metadata
{ {
const METADATA_FILE_JSON = 'metadata.json'; const METADATA_FILE_JSON = 'metadata.json';
/** /**
* Return an array from a JSON string
* @param mixed $json The JSON string to turn into an array
* @return array Always return an array containing the decoded JSON or an empty array in case the
* $json parameter is not a valid JSON string
*/
private function getArrayFromJson($json)
{
$result = json_decode($json, true);
return is_array($result) ? $result : array();
}
/**
* Fetch metadata information from an addon path * Fetch metadata information from an addon path
* @param string $path Path to the addon * @param string $path Directory the addon is in (without trailing slash)
* @return array The metadata fetched from the metadata.json file in the addon path * @return array The metadata fetched from the JSON file, or an empty array otherwise
*/ */
public function fetchFromAddonPath($path) public function fetchFromAddonPath($path)
{ {
$metadataFile = $path . '/' . self::METADATA_FILE_JSON; $metadataFile = $path . '/' . self::METADATA_FILE_JSON;
if (is_file($metadataFile)) { if (!is_file($metadataFile)) {
$content = file_get_contents($metadataFile); return array();
return $this->getArrayFromJson($content);
} }
return array();
$content = file_get_contents($metadataFile);
return $this->getArrayFromJson($content);
} }
/** /**
...@@ -63,4 +51,15 @@ class Q2A_Util_Metadata ...@@ -63,4 +51,15 @@ class Q2A_Util_Metadata
return $this->getArrayFromJson($content); return $this->getArrayFromJson($content);
} }
} /**
\ No newline at end of file * Return an array from a JSON string
* @param mixed $json The JSON string to turn into an array
* @return array Always return an array containing the decoded JSON or an empty array in case the
* $json parameter is not a valid JSON string
*/
private function getArrayFromJson($json)
{
$result = json_decode($json, true);
return is_array($result) ? $result : array();
}
}
...@@ -55,14 +55,14 @@ ...@@ -55,14 +55,14 @@
} }
/**
* Return a sorted array of available languages, [short code] => [long name]
*/
function qa_admin_language_options() function qa_admin_language_options()
/*
Return a sorted array of available languages, [short code] => [long name]
*/
{ {
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); } if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
/* /**
* @deprecated The hardcoded language ids will be removed in favor of language metadata files. * @deprecated The hardcoded language ids will be removed in favor of language metadata files.
* See qa-lang/en-GB directory for a clear example of how to use them. * See qa-lang/en-GB directory for a clear example of how to use them.
*/ */
...@@ -121,7 +121,7 @@ ...@@ -121,7 +121,7 @@
// find all language folders // find all language folders
$metadataUtil = new Q2A_Util_Metadata(); $metadataUtil = new Q2A_Util_Metadata();
foreach (glob(QA_LANG_DIR . '*', GLOB_NOSORT | GLOB_ONLYDIR) as $directory) { foreach (glob(QA_LANG_DIR . '*', GLOB_ONLYDIR) as $directory) {
$code = basename($directory); $code = basename($directory);
$metadata = $metadataUtil->fetchFromAddonPath($directory); $metadata = $metadataUtil->fetchFromAddonPath($directory);
if (isset($metadata['display_name'])) if (isset($metadata['display_name']))
...@@ -143,12 +143,12 @@ ...@@ -143,12 +143,12 @@
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); } if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
$metadataUtil = new Q2A_Util_Metadata(); $metadataUtil = new Q2A_Util_Metadata();
foreach (glob(QA_THEME_DIR . '*', GLOB_NOSORT | GLOB_ONLYDIR) as $directory) { foreach (glob(QA_THEME_DIR . '*', GLOB_ONLYDIR) as $directory) {
$theme = basename($directory); $theme = basename($directory);
$metadata = $metadataUtil->fetchFromAddonPath($directory); $metadata = $metadataUtil->fetchFromAddonPath($directory);
if (empty($metadata)) { if (empty($metadata)) {
// limit theme parsing to first 8kB // limit theme parsing to first 8kB
$contents = file_get_contents($directory . '/qa-styles.css', false, NULL, -1, 8192); $contents = file_get_contents($directory . '/qa-styles.css', false, null, -1, 8192);
$metadata = qa_addon_metadata($contents, 'Theme'); $metadata = qa_addon_metadata($contents, 'Theme');
} }
$options[$theme] = isset($metadata['name']) ? $metadata['name'] : $theme; $options[$theme] = isset($metadata['name']) ? $metadata['name'] : $theme;
......
...@@ -1011,7 +1011,7 @@ ...@@ -1011,7 +1011,7 @@
$metadata = $metadataUtil->fetchFromAddonPath($themedirectory); $metadata = $metadataUtil->fetchFromAddonPath($themedirectory);
if (empty($metadata)) { if (empty($metadata)) {
// limit theme parsing to first 8kB // limit theme parsing to first 8kB
$contents = file_get_contents($themedirectory . '/qa-styles.css', false, NULL, -1, 8192); $contents = file_get_contents($themedirectory . '/qa-styles.css', false, null, -1, 8192);
$metadata = qa_addon_metadata($contents, 'Theme'); $metadata = qa_addon_metadata($contents, 'Theme');
} }
......
...@@ -102,11 +102,12 @@ ...@@ -102,11 +102,12 @@
if (!empty($pluginfiles)) { if (!empty($pluginfiles)) {
$metadataUtil = new Q2A_Util_Metadata(); $metadataUtil = new Q2A_Util_Metadata();
$sortedPluginFiles = array(); $sortedPluginFiles = array();
foreach ($pluginfiles as $pluginFile) { foreach ($pluginfiles as $pluginFile) {
$metadata = $metadataUtil->fetchFromAddonPath(dirname($pluginFile)); $metadata = $metadataUtil->fetchFromAddonPath(dirname($pluginFile));
if (empty($metadata)) { if (empty($metadata)) {
// limit plugin parsing to first 8kB // limit plugin parsing to first 8kB
$contents = file_get_contents($pluginFile, false, NULL, -1, 8192); $contents = file_get_contents($pluginFile, false, null, -1, 8192);
$metadata = qa_addon_metadata($contents, 'Plugin'); $metadata = qa_addon_metadata($contents, 'Plugin');
} }
$metadata['name'] = isset($metadata['name']) && !empty($metadata['name']) $metadata['name'] = isset($metadata['name']) && !empty($metadata['name'])
...@@ -115,7 +116,9 @@ ...@@ -115,7 +116,9 @@
; ;
$sortedPluginFiles[$pluginFile] = $metadata; $sortedPluginFiles[$pluginFile] = $metadata;
} }
qa_sort_by($sortedPluginFiles, 'name'); qa_sort_by($sortedPluginFiles, 'name');
$pluginIndex = -1; $pluginIndex = -1;
foreach ($sortedPluginFiles as $pluginFile => $metadata) { foreach ($sortedPluginFiles as $pluginFile => $metadata) {
$pluginIndex++; $pluginIndex++;
......
...@@ -343,7 +343,7 @@ ...@@ -343,7 +343,7 @@
$metadata = $metadataUtil->fetchFromAddonPath($pluginDirectory); $metadata = $metadataUtil->fetchFromAddonPath($pluginDirectory);
if (empty($metadata)) { if (empty($metadata)) {
// limit plugin parsing to first 8kB // limit plugin parsing to first 8kB
$contents = file_get_contents($pluginfile, false, NULL, -1, 8192); $contents = file_get_contents($pluginfile, false, null, -1, 8192);
$metadata = qa_addon_metadata($contents, 'Plugin', true); $metadata = qa_addon_metadata($contents, 'Plugin', true);
} }
......
{ {
"name": "SnowFlat", "name": "SnowFlat",
"uri": "", "uri": "",
"description": "ShowFlat responsive theme with RTL support", "description": "SnowFlat responsive theme with RTL support",
"version": "1.4", "version": "1.4",
"date": "2014-03-11", "date": "2014-03-11",
"author": "Q2A Market", "author": "Q2A Market",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment