You are here

public function ReadmeHelpMarkdownConverter::insertPhpSnippets in README Help 8

Replaces PHP file tokens with a snippet from the file.

The LINE and PADD arguments in the token are optional. Use them to highligth a particular LINE in the snippet having PADD lines added before and after the line. If omitted then the whole PHP file is highlighted. @code @PHPFILE: /absolute/path/to/MY_FILE.php LINE:123 PADD:10 :PHPFILE@

## If PADD is omitted then by default 10 lines are added. @PHPFILE: related/to/Drupal/root/path/MY_FILE.php LINE:123 :PHPFILE@

Parameters

string $text: The text string to be filtered.

string $path: (optional) The relative path to the module's folder.

Return value

string The text with HTML markup.

See also

::highlightPhp()

1 call to ReadmeHelpMarkdownConverter::insertPhpSnippets()
ReadmeHelpMarkdownConverter::convertMarkdownFile in src/ReadmeHelpMarkdownConverter.php
Converts markdown into HTML markup in a file.

File

src/ReadmeHelpMarkdownConverter.php, line 242

Class

ReadmeHelpMarkdownConverter
Default implementation of the ReadmeHelpMarkdownConverter.

Namespace

Drupal\readmehelp

Code

public function insertPhpSnippets($text, $path = '') {
  $root = $this->root;
  return preg_replace_callback('{([^\\s])(@PHPFILE:).*?(:PHPFILE@)}s', function ($matches) use ($root, $path) {
    $pattern = '/(\\ *@PHPFILE)|(PHPFILE@\\ *)|(\\ *LINE)|(\\ *PADD)/';
    $match = explode(':', preg_replace($pattern, '', $matches[0]));
    $prefix = '';
    if (isset($match[1]) && ($file = $orig = trim($match[1]))) {
      $line = isset($match[2]) ? (int) trim($match[2]) : 1;
      $padding = isset($match[3]) ? (int) trim($match[3]) : 10;
      $prefix = $match[0];
      $file = $file[0] == '/' ? $file : "{$root}/{$file}";
      if (!is_file($file)) {
        $name = basename($orig);
        $file = "{$root}/{$path}/{$name}";
        if (!is_file($file)) {
          $file = "{$root}/{$path}/{$orig}";
        }
      }
    }
    return $prefix . $this
      ->highlightPhp($file, $line, $padding);
  }, $text);
}