Task/Collect/HooksCollector.php
protected function processHookFile($filepath) {
    $contents = file_get_contents("$filepath");

    // Prepare a replacement set of the import statements, so we can replace
    // short class names will fully-qualified ones, so the sample code can
    // actually run.
    $imports_pattern = "@^use (.+);@m";
    $matches = [];
    preg_match_all($imports_pattern, $contents, $matches);
    $imports = $matches[1];

    $import_replacements = [];
    foreach ($imports as $import) {
      $pieces = explode('\\', $import);
      $short_class_name = array_pop($pieces);

      $import_replacements['@\b' . $short_class_name . '\b@'] = '\\' . $import;
    }

    // The pattern for extracting function data: capture first line of doc,
    // function declaration, and hook name.
    $pattern = '[
             / \* \* \n     # start phpdoc
            \  \* \  ( .* ) \n  # first line of phpdoc: capture the text for the description
      ( (?: \  \* .* \n )* )  # lines of phpdoc: capture the documentation
            \  \* /  \n     # end phpdoc
           ( function \ ( ( hook | callback ) \w+ ) .* ) \ { # function declaration: capture...
      #    ^ entire definition and name
      #                 ^ function name
      #                   ^ whether this is a hook or a callback
       ( (?: .* \n )*? )    # function body: capture example code
           ^ }
    ]mx';

    $matches = [];
    preg_match_all($pattern, $contents, $matches);

    // We don't care about the full matches.
    //array_shift($matches);

    $data = array(
      'descriptions'  => $matches[1],
      'documentation' => $matches[2],
      'definitions'   => $matches[3],
      'names'         => $matches[4],
      'type'          => $matches[5],
      'bodies'        => $matches[6],
    );

    // Replace all the short class names in body code with fully-qualified ones.
    foreach ($data['bodies'] as &$hook_body) {
      $hook_body = preg_replace(array_keys($import_replacements), array_values($import_replacements), $hook_body);
    }

    return $data;
  }
protected function processHookFile($filepath) {
    $contents = file_get_contents("$filepath");

    // Prepare a replacement set of the import statements, so we can replace
    // short class names will fully-qualified ones, so the sample code can
    // actually run.
    $imports_pattern = "@^use (.+);@m";
    $matches = [];
    preg_match_all($imports_pattern, $contents, $matches);
    $imports = $matches[1];

    $import_replacements = [];
    foreach ($imports as $import) {
      $pieces = explode('\\', $import);
      $short_class_name = array_pop($pieces);

      $import_replacements['@\b' . $short_class_name . '\b@'] = '\\' . $import;
    }

    // The pattern for extracting function data: capture first line of doc,
    // function declaration, and hook name.
    $pattern = '[
             / \* \* \n     # start phpdoc
            \  \* \  ( .* ) \n  # first line of phpdoc: capture the text for the description
      ( (?: \  \* .* \n )* )  # lines of phpdoc: capture the documentation
            \  \* /  \n     # end phpdoc
           ( function \ ( ( hook | callback ) \w+ ) .* ) \ { # function declaration: capture...
      #    ^ entire definition and name
      #                 ^ function name
      #                   ^ whether this is a hook or a callback
       ( (?: .* \n )*? )    # function body: capture example code
           ^ }
    ]mx';

    $matches = [];
    preg_match_all($pattern, $contents, $matches);

    // We don't care about the full matches.
    //array_shift($matches);

    $data = array(
      'descriptions'  => $matches[1],
      'documentation' => $matches[2],
      'definitions'   => $matches[3],
      'names'         => $matches[4],
      'type'          => $matches[5],
      'bodies'        => $matches[6],
    );

    // Replace all the short class names in body code with fully-qualified ones.
    foreach ($data['bodies'] as &$hook_body) {
      $hook_body = preg_replace(array_keys($import_replacements), array_values($import_replacements), $hook_body);
    }

    return $data;
  }
protected function processHookFile ( $ filepath ) { $ contents = file_get_contents ( "$filepath" ) ; // Prepare a replacement set of the import statements, so we can replace // short class names will fully-qualified ones, so the sample code can // actually run. $ imports_pattern = "@^use (.+);@m" ; $ matches = [ ] ; preg_match_all ( $ imports_pattern , $ contents , $ matches ) ; $ imports = $ matches [ 1 ] ; $ import_replacements = [ ] ; foreach ( $ imports as $ import ) { $ pieces = explode ( '\\' , $ import ) ; $ short_class_name = array_pop ( $ pieces ) ; $ import_replacements [ '@\b' . $ short_class_name . '\b@' ] = '\\' . $ import ; } // The pattern for extracting function data: capture first line of doc, // function declaration, and hook name. $ pattern = '[
             / \* \* \n     # start phpdoc
            \  \* \  ( .* ) \n  # first line of phpdoc: capture the text for the description
      ( (?: \  \* .* \n )* )  # lines of phpdoc: capture the documentation
            \  \* /  \n     # end phpdoc
           ( function \ ( ( hook | callback ) \w+ ) .* ) \ { # function declaration: capture...
      #    ^ entire definition and name
      #                 ^ function name
      #                   ^ whether this is a hook or a callback
       ( (?: .* \n )*? )    # function body: capture example code
           ^ }
    ]mx' ; $ matches = [ ] ; preg_match_all ( $ pattern , $ contents , $ matches ) ; // We don't care about the full matches. //array_shift($matches); $ data = array ( 'descriptions' => $ matches [ 1 ] , 'documentation' => $ matches [ 2 ] , 'definitions' => $ matches [ 3 ] , 'names' => $ matches [ 4 ] , 'type' => $ matches [ 5 ] , 'bodies' => $ matches [ 6 ] , ) ; // Replace all the short class names in body code with fully-qualified ones. foreach ( $ data [ 'bodies' ] as & $ hook_body ) { $ hook_body = preg_replace ( array_keys ( $ import_replacements ) , array_values ( $ import_replacements ) , $ hook_body ) ; } return $ data ; }
Extracts raw hook data from a downloaded hook documentation file.

@param string $filepath
Path to hook file.

@return array
Array of hook data, in different arrays, where each array is keyed
numerically and all the indexes match up. The arrays are:
- 'type': Whether this is a hook or a callback.
- 'descriptions': Each hook's user-friendly description, taken from the
first line of the PHPdoc.
- 'documentation': The rest of the PHPdoc.
- 'definitions: Each hook's entire function declaration: "function name($params)"
- 'names': The long names of the hooks, i.e. 'hook_foo'.
- 'bodies': The function bodies of each hook.
Extracts raw hook data from a downloaded hook documentation file .