You are here

function patterns_scan_pattern in Patterns 7

Same name and namespace in other branches
  1. 7.2 includes/parser/scan.inc \patterns_scan_pattern()

Scans a pattern and returns a brief summary of its properties.

@TODO Expand this function to include much more detailed validation. @TODO Rewrite in an object oriented fashion

Parameters

array $pattern : Pattern array obtained by parsing pattern file.

bool $include (optional) : If TRUE, actions can be found outside of sections. Default FALSE.

integer $level (optional) : The level of of the analysis. Defaults PATTERNS_VALIDATE_ALL

Return value

array $result Summary of the pattern.

See also

_patterns_scan_action()

_patterns_scan_analyze_patternscan()

8 calls to patterns_scan_pattern()
PatternsParserTestCase::basic_checks in tests/parser/parser.test
patterns_enable_pattern in ./patterns.module
Form constructor for the Patterns enabling form.
patterns_import_validate in includes/forms/import.inc
patterns_parser_get_pattern_details in includes/parser/parser.inc
Returns an array with detailed information about the pattern(s) referenced in the pattern files (included).
patterns_validate_pattern in includes/parser/parser.inc
Check if pattern array or content from a file is valid.

... See full list

File

includes/parser/scan.inc, line 32
Helping functions for the parser.

Code

function patterns_scan_pattern($pattern, $include = FALSE, $level = PATTERNS_VALIDATE_ALL) {
  $generic_error = t('pattern not loaded correctly');
  $result = array(
    'info' => 0,
    'modules' => 0,
    'empties' => array(),
    'invalid_actions' => array(),
    'extra_actions' => array(),
    'missing_tag' => array(),
    'other_sections' => array(),
    'contain_includes' => FALSE,
    'include_scans' => array(),
    'generic_errors' => array(),
    'unknown_tag' => array(),
    'tag_errors' => array(),
  );
  if (empty($pattern)) {
    $result['empties'][] = $generic_error;
    return $result;
  }

  // Patterns is valid if contains:
  //  - exactly 1 topmost section 'info'
  //  - at most 1 topmost section 'modules'
  //  - at least 1 more other sections
  //
  // All the sections must be a non-empty array.
  // If a section is called 'include;
  // it means that we are including a new pattern
  // and we apply patterns_scan_pattern recursively.
  foreach ($pattern as $section_name => $section) {

    // INFO
    if ($section_name === 'info') {
      $result['info']++;
      continue;
    }

    // MODULES
    if ($section_name === 'modules') {
      $result['modules']++;
      continue;
    }

    // SECTIONS or ACTIONS (if it is an include)
    if (!_patterns_scan_is_section($section)) {
      if (!$include) {
        $result['generic_errors'][] = $section_name;
        continue;
      }
      $result = _patterns_scan_action($section, $result);
      continue;
    }

    // SECTIONS: invalid
    // included patterns can be just the name or the id
    // of the pattern
    if (!is_array($section) && !$include) {

      // If the YAML is not loaded correctly you get 0.
      $result['empties'][] = $section_name === 0 ? $generic_error : $section_name;
      continue;
    }

    // SECTIONS: valid

    //$resultactions = array();

    //$newactions = array();

    // Collect info specific to each section
    $section_info = array();
    $section_info[PATTERNS_CREATE] = 0;
    $section_info[PATTERNS_MODIFY] = 0;
    $section_info[PATTERNS_DELETE] = 0;
    $section_info[PATTERNS_INCLUDE] = 0;
    foreach ($section as $key => &$action) {
      $action_name = key($action);
      if (isset($section_info[$action_name])) {

        // Incremente the count of valid actions
        $section_info[$action_name] = $section_info[$action_name] + 1;
      }
      $result = _patterns_scan_action($action, $result, $level);
    }
    $section_info_str = '(' . PATTERNS_CREATE . ':' . $section_info[PATTERNS_CREATE] . ', ' . PATTERNS_MODIFY . ':' . $section_info[PATTERNS_MODIFY] . ', ' . PATTERNS_DELETE . ':' . $section_info[PATTERNS_DELETE] . ', ' . PATTERNS_INCLUDE . ':' . $section_info[PATTERNS_INCLUDE] . ')';
    $result['other_sections'][$section_name] = $section_info_str;
  }
  return $result;
}