Skip to content
Browse files

Code cleanup. Created inc tagmodules

  • Loading branch information...
1 parent 8d7a8b5 commit f5b863d3a2b31483196ea6aad772da6ff4e1fa91 @shakty committed Apr 22, 2012
Showing with 261 additions and 212 deletions.
  1. +1 −64 includes/api/api.inc
  2. +2 −2 includes/core/common.inc
  3. +255 −0 includes/tagmodules.inc
  4. +3 −146 patterns.module
View
65 includes/api/api.inc
@@ -44,68 +44,6 @@ function patterns_api_is_reserved_word($word = NULL) {
}
/**
- * @see patterns_get_tagmodules_index
- * @see patterns_api_build_moduletags_index
- *
- */
-function patterns_api_build_tagmodules_index($tag = NULL, $data = NULL, $key = NULL) {
- return patterns_get_tagmodules_index(NULL, NULL, $tag, $key); // TODO adjust
-}
-/**
- * Builds up an associative array of modules and exposed tags.
- *
- * @param mixed $module
- * (optional) If set, only information about this specific module is returned.
- * Defaults NULL.
- * @param mixed $key (optional) If set, only the values for that key
- * from all modules are returned. E.g. (PATTERNS_CREATE, PATTERNS_MODIFY, etc.)
- * Defaults NULL.
- *
- * @return array $moduletags The tagmodules index built according to the parameters.
- *
- * @see patterns_api_build_tagmodules_index
- */
-function patterns_api_build_moduletags_index($module = NULL, $data = NULL, $key = NULL) {
- static $moduletags;
-
- // Index not yet built
- if (empty($moduletags)) {
-
- $moduletags = array();
- foreach (module_implements('patterns') as $component) {
- $tags = module_invoke($component, 'patterns', $data);
- $moduletags[$component] = $tags;
- }
- }
-
- //debug($moduletags);
-
- // Part of tag
- if (!is_null($module) && !is_null($key)) {
- return $moduletags[$module][$key];
- }
- // Full tag
- if (!is_null($module)) {
- return $moduletags[$module];
- }
-
- // All the values of key=$key from all tags
- if (!is_null($key)) {
- $out = array();
- foreach ($moduletags as $module => $value) {
- if (isset($value[$key])) {
- $out[$module] = $value[$key];
- }
- }
- return $out;
- }
-
- // All values from all tags
- return $moduletags;
-}
-
-
-/**
* Checks whether a string is a valid component name.
*
* @param mixed $component
@@ -118,11 +56,10 @@ function patterns_api_build_moduletags_index($module = NULL, $data = NULL, $key
*/
function patterns_api_is_valid_component_name($component = NULL) {
if (is_null($component)) return FALSE;
- $idx = patterns_api_build_moduletags_index();
+ $idx = patterns_moduletags_get_index();
return (isset($idx[$component])) ? TRUE : FALSE;
}
-
/**
* Adds / Replaces an Info section to a pattern.
*
View
4 includes/core/common.inc
@@ -28,7 +28,7 @@ function patterns_prepare_action(&$action, &$data, $actions_map) {
}
// Keep a list of which modules handle what tags.
- $tag_modules = patterns_get_tagmodules_index($data);
+ $tag_modules = patterns_tagmodules_get_index($data);
$errors = array();
@@ -105,7 +105,7 @@ function patterns_implement_action($action, $data, &$identifiers, $place = 0, $a
$msg = '';
// Refresh the list of forms based on the data we have.
- $tagmodules = patterns_get_tagmodules_index($data);
+ $tagmodules = patterns_tagmodules_get_index($data);
$tag_info = patterns_tagmodules_filter($tagmodules, $data['tag']);
$form_ids = $tag_info[$action];
View
255 includes/tagmodules.inc
@@ -0,0 +1,255 @@
+<?php
+
+/**
+ * @file
+ * Functions related to build and retrieve information from the
+ * *tagmodules* and *moduletags* indexes.
+ *
+ */
+
+
+/**
+ * Builds an associative array of tags and modules (components).
+ *
+ * Invokes hook_patterns on all modules, and returns an index of
+ * available tags and information associated with them (e.g. from which
+ * module they are handled).
+ *
+ * The index is cached, but reloading can be forced.
+ *
+ * @param array $data (optional) An associative array of data, that the
+ * components can use to build dynamic forms names. Defaults NULL.
+ * The array usually corresponds to an action in a pattern file, e.g.
+ *
+ * array('tag' => 'node',
+ * 'type' => 'article',
+ * 'title' => 'Test Article',
+ * 'body', => 'lorem ipsum ...',
+ * );
+ *
+ * @param bool $reset If TRUE, forces rebuilding the index from all
+ * the components. Defaults FALSE
+ *
+ * @return array $tagmodules Index of tag and modules
+ */
+function patterns_tagmodules_get_index($data = NULL, $reset = FALSE) {
+
+ $tagmodules = &drupal_static(__FUNCTION__);
+
+ // Index not yet built or forced rebuilt
+ if ($reset || empty($tagmodules)) {
+ $tagmodules = array();
+ // Get a list of tags and their modules.
+ foreach (module_implements('patterns') as $module) {
+ _patterns_tagmodules_add_module($module, $tagmodules, $data);
+ }
+ }
+ // We may need to rebuild the index just for the specific tag
+ else if (!empty($data)) {
+ $module = patterns_tagmodules_find_module($data);
+ _patterns_tagmodules_add_module($module, $tagmodules, $data);
+ }
+
+ // All values from all tags
+ return $tagmodules;
+}
+
+/**
+ * Helper function for <patterns_tagmodules_get_index>.
+ *
+ * Call <hook_patterns> on a given module and merge the results
+ * in the tagmodules index.
+ *
+ * @param mixed $module A string representing a module (component) name
+ * @param array $tagmodules An array containing the tagmodules index
+ * @param array $data (optional) An associative array of data, that the
+ * components can use to build dynamic forms names. Defaults NULL.
+ *
+ * @see patterns_tagmodules_get_index
+ * @see _patterns_tagmodules_add_module
+ */
+function _patterns_tagmodules_add_module($module, &$tagmodules, $data = NULL) {
+ if (empty($tagmodules)) $tagmodules = array();
+ if (empty($module)) return;
+
+ $tags = module_invoke($module, 'patterns', $data);
+ _patterns_tagmodules_add_module_to_tags($module, $tags);
+ $tagmodules = array_merge($tagmodules, $tags);
+}
+
+/**
+ * Helper function for <_patterns_tagmodules_add_module>
+ *
+ * Adds the name of the module to each tag returned from <hook_patterns>.
+ *
+ * @param mixed $module A string representing a module (component) name
+ * @param array $tags Array containing the results of the invocation of
+ * <hook_patterns> on a module.
+ */
+function _patterns_tagmodules_add_module_to_tags($module, &$tags) {
+ if (empty($tags)) return;
+ foreach ($tags as $tagname => $value) {
+ $tags[$tagname]['module'] = $module;
+ }
+}
+
+/**
+ * Filters out information from a tagmodules index and returns
+ * the results in a new array.
+ *
+ * Select information based on the tag (e.g. node, vocabulary, etc.),
+ * or on the key (e.g. PATTERNS_CREATE), or on a combination of both.
+ *
+ * @param array $tagmodules Associative array of tagmodules
+ * @param mixed $tag (optional) Forces to return information only
+ * from this tag. Defaults NULL.
+ * @param mixed $key (optional) Forces to return information only
+ * from the tags containing this key. Defaults NULL.
+ *
+ * @return array $tagmodules The filtered tagmodules array
+ */
+function patterns_tagmodules_filter($tagmodules, $tag = NULL, $key = NULL) {
+ if (empty($tagmodules)) return array();
+
+ // Part of tag
+ if (!empty($tag) && !empty($key)) {
+ return $tagmodules[$tag][$key];
+ }
+ // Full tag
+ if (!empty($tag)) {
+ return $tagmodules[$tag];
+ }
+
+ // All the values of key=$key from all tags
+ if (!empty($key)) {
+ $out = array();
+ foreach ($tagmodules as $tagname => $value) {
+ if (isset($value[$key])) {
+ $out[$value['module']][$tagname] = $value[$key];
+ }
+ }
+ return $out;
+ }
+}
+
+/**
+ * Filters out information from a modulestag index and returns
+ * the results in a new array.
+ *
+ * Select information based on the tag (e.g. node, vocabulary, etc.),
+ * or on the key (e.g. PATTERNS_CREATE), or on a combination of both.
+ *
+ * @param array $moduletags Associative array of moduletags
+ * @param mixed $module (optional) Forces to return information only
+ * from this module. Defaults NULL.
+ * @param mixed $key (optional) Forces to return information only
+ * from the modules containing this key. Defaults NULL.
+ *
+ * @return array $tagmodules The filtered tagmodules array
+ */
+function patterns_moduletags_filter($modulestags, $module = NULL, $key = NULL) {
+ if (empty($modulestags)) return array();
+
+ // Part of tag
+ if (!empty($module) && !empty($key)) {
+ return $moduletags[$module][$key];
+ }
+ // Full tag
+ if (!empty($module)) {
+ return $moduletags[$module];
+ }
+
+ // All the values of key=$key from all tags
+ if (!empty($key)) {
+ $out = array();
+ foreach ($moduletags as $module => $value) {
+ if (isset($value[$key])) {
+ $out[$module] = $value[$key];
+ }
+ }
+ return $out;
+ }
+}
+
+
+/**
+ * Builds up an associative array of modules and exposed tags.
+ *
+ * @param mixed $module
+ * (optional) If set, only information about this specific module is returned.
+ * Defaults NULL.
+ * @param mixed $key (optional) If set, only the values for that key
+ * from all modules are returned. E.g. (PATTERNS_CREATE, PATTERNS_MODIFY, etc.)
+ * Defaults NULL.
+ *
+ * @return array $moduletags The tagmodules index built according to the parameters.
+ *
+ * @see patterns_tagmodules_get_index
+ */
+function patterns_moduletags_get_index($data = NULL, $reset = FALSE) {
+ $moduletags = &drupal_static(__FUNCTION__);
+
+ // Index not yet built
+ if ($reset || empty($moduletags)) {
+
+ $moduletags = array();
+ foreach (module_implements('patterns') as $component) {
+ _patterns_moduletags_add_module($moduletags, $component, $data);
+ //$tags = module_invoke($component, 'patterns', $data);
+ //$moduletags[$component] = $tags;
+ }
+ }
+ // We may need to rebuild the index just for the specific tag
+ else if (!empty($data)) {
+ $module = patterns_tagmodules_find_module($data);
+ _patterns_moduletags_add_module($moduletags, $module, $data);
+ }
+
+ // All values from all tags
+ return $moduletags;
+}
+
+/**
+ * Helper function for <patterns_api_build_moduletags_index>.
+ *
+ * Call <hook_patterns> on a given module and merge the results
+ * in the moduletags index.
+ *
+ * @param mixed $module A string representing a module (component) name
+ * @param array $moduletags An array containing the moduletags index
+ * @param array $data (optional) An associative array of data, that the
+ * components can use to build dynamic forms names. Defaults NULL.
+ *
+ * @see patterns_tagmodules_get_index
+ * @see _patterns_add_module_to_tags
+ */
+function _patterns_moduletags_add_module($module, &$moduletags, $data = NULL) {
+ if (empty($moduletags)) $moduletags = array();
+ if (empty($module)) return;
+
+ $tags = module_invoke($module, 'patterns', $data);
+ $moduletags[$module] = $tags;
+}
+
+/**
+ * Tries to find the module responsible for a given action.
+ *
+ * @param array $action Array representing a Patterns action.
+ *
+ * @return mixed|bool The name of the module responsible for the
+ * action or FALSE, it fails to match the action with a module.
+ */
+function patterns_tagmodules_find_module($action) {
+ if (isset($action['module']) && !empty($action['module'])) {
+ return $action['module'];
+ }
+
+ if (isset($data['tag'])) {
+ $tagmodules = patterns_tagmodules_get_index($action);
+ $tag_name = $data['tag'];
+ if (isset($tagmodules[$tag_name]['module'])) {
+ return $tagmodules[$tag_name]['module'];
+ };
+ }
+ return FALSE;
+}
View
149 patterns.module
@@ -55,6 +55,7 @@ module_load_include('inc', 'patterns', 'includes/config');
module_load_include('inc', 'patterns', 'includes/path');
module_load_include('inc', 'patterns', 'includes/utils');
+module_load_include('inc', 'patterns', 'includes/tagmodules');
module_load_include('inc', 'patterns', 'includes/db');
module_load_include('inc', 'patterns', 'includes/error');
module_load_include('inc', 'patterns', 'includes/forms/forms');
@@ -695,9 +696,6 @@ function patterns_start_engine($pattern, $params = array(), $mode = 'batch') {
return TRUE;
}
-// @TODO: Take care patterns_invoke does not return any more the tagmodules index.
-// Use patterns_get_tagmodules_index() instead.
-
/**
* Execute hook_patterns with the given operation and return the results.
*
@@ -720,7 +718,7 @@ function patterns_start_engine($pattern, $params = array(), $mode = 'batch') {
*/
function patterns_invoke($hook, $action, &$data = NULL, $form_id = NULL, &$a = NULL) {
- $tag_modules = patterns_get_tagmodules_index($data); // TODO: check the parameters
+ $tag_modules = patterns_tagmodules_get_index($data); // TODO: check the parameters
// Unsetting it while the module performs operations.
$tag = $data['tag'];
@@ -773,129 +771,6 @@ function patterns_invoke($hook, $action, &$data = NULL, $form_id = NULL, &$a = N
}
/**
- * Builds an associative array of tags and modules (components).
- *
- * Invokes hook_patterns on all modules, and returns an index of
- * available tags and information associated with them (e.g. from which
- * module they are handled).
- *
- * The index is cached, but reloading can be forced.
- *
- * @param array $data (optional) An associative array of data, that the
- * components can use to build dynamic forms names. Defaults NULL.
- * The array usually corresponds to an action in a pattern file, e.g.
- *
- * array('tag' => 'node',
- * 'type' => 'article',
- * 'title' => 'Test Article',
- * 'body', => 'lorem ipsum ...',
- * );
- *
- * @param bool $reset If TRUE, forces rebuilding the index from all
- * the components. Defaults FALSE
- *
- * @return array $tagmodules Index of tag and modules
- */
-function patterns_get_tagmodules_index($data = NULL, $reset = FALSE) {
-
- $tagmodules = &drupal_static(__FUNCTION__);
-
- // Index not yet built or forced rebuilt
- if ($reset || empty($tagmodules)) {
- $tagmodules = array();
- // Get a list of tags and their modules.
- foreach (module_implements('patterns') as $module) {
- _patterns_add_module_to_tagmodules($module, $tagmodules, $data);
- }
- }
- // We may need to rebuild the index just for the specific tag
- else if (!empty($data)) {
- $tag_name = $data['tag'];
- $module = $tagmodules[$tag_name]['module'];
- if (!empty($module)) {
- _patterns_add_module_to_tagmodules($module, $tagmodules, $data);
- }
- }
-
- // All values from all tags
- return $tagmodules;
-}
-
-/**
- * Helper function for <patterns_get_tagmodules_index>.
- *
- * Call <hook_patterns> on a given module and merge the results
- * in the tagmodules index.
- *
- * @param mixed $module A string representing a module (component) name
- * @param array $tagmodules An array containing the tagmodules index
- * @param array $data (optional) An associative array of data, that the
- * components can use to build dynamic forms names. Defaults NULL.
- *
- * @see patterns_get_tagmodules_index
- * @see _patterns_add_module_to_tags
- */
-function _patterns_add_module_to_tagmodules($module, &$tagmodules, $data = NULL) {
- $tags = module_invoke($module, 'patterns', $data);
- _patterns_add_module_to_tags($module, $tags);
- $tagmodules = array_merge($tagmodules, $tags);
-}
-
-/**
- * Helper function for <_patterns_add_module_to_tagmodules>
- *
- * Adds the name of the module to each tag returned from <hook_patterns>.
- *
- * @param mixed $module A string representing a module (component) name
- * @param array $tags Array containing the results of the invocation of
- * <hook_patterns> on a module.
- */
-function _patterns_add_module_to_tags($module, &$tags) {
- foreach ($tags as $tagname => $value) {
- $tags[$tagname]['module'] = $module;
- }
-}
-
-/**
- * Filters out information from a tagmodules index and returns
- * the results in a new array.
- *
- * Select information based on the tag (e.g. node, vocabulary, etc.),
- * or on the key (e.g. PATTERNS_CREATE), or on a combination of both.
- *
- * @param array $tagmodules Associative array of tagmodules
- * @param mixed $tag (optional) Forces to return information only
- * from this tag. Defaults NULL.
- * @param mixed $key (optional) Forces to return information only
- * from the tags containing this key. Defaults NULL.
- *
- * @return array $tagmodules The filtered tagmodules array
- */
-function patterns_tagmodules_filter($tagmodules, $tag = NULL, $key = NULL) {
- if (empty($tagmodules)) return array();
-
- // Part of tag
- if (!empty($tag) && !empty($key)) {
- return $tagmodules[$tag][$key];
- }
- // Full tag
- if (!empty($tag)) {
- return $tagmodules[$tag];
- }
-
- // All the values of key=$key from all tags
- if (!empty($key)) {
- $out = array();
- foreach ($tagmodules as $tagname => $value) {
- if (isset($value[$key])) {
- $out[$value['module']][$tagname] = $value[$key];
- }
- }
- return $out;
- }
-}
-
-/**
* Constructs a Pattern object with default standard values.
*
* @param array $pattern Input array structure of the pattern. Defaults to an empty array.
@@ -926,12 +801,6 @@ function patterns_get_pattern_obj($pattern = array()) {
}
/**
- * @TODO patterns_refresh().
- */
-function patterns_refresh() {
-}
-
-/**
* Returns the current option for included patterns, as it is in
* the database. If the option stored in the database is not valid,
* by default patterns' inclusion is disabled.
@@ -975,16 +844,4 @@ function patterns_install_modules_in_pattern($pattern) {
return FALSE;
}
return TRUE;
-}
-
-function patterns_check_local_updates() {
-
- $updates = variable_get(PATTERNS_UPDATES, array());
-
- if (!empty($updates) && is_array($updates)) {
- foreach($updates as $update) {
- module_invoke_all($update);
- }
- }
-}
-
+}

0 comments on commit f5b863d

Please sign in to comment.
Something went wrong with that request. Please try again.