Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CommentParser.php
Go to the documentation of this file.
1 <?php
7 
12 
17 {
23  private $filesystem;
24 
30  private $configFilePool;
31 
36  public function __construct(
37  Filesystem $filesystem,
38  ConfigFilePool $configFilePool
39  ) {
40  $this->filesystem = $filesystem;
41  $this->configFilePool = $configFilePool;
42  }
43 
64  public function execute($fileName)
65  {
66  $result = [];
67  $dirReader = $this->filesystem->getDirectoryRead(DirectoryList::CONFIG);
68 
69  if (!$dirReader->isExist($fileName)) {
70  return $result;
71  }
72 
73  $fileContent = $dirReader->readFile($fileName);
74  $commentBlocks = array_filter(
75  token_get_all($fileContent),
76  function ($entry) {
77  return T_DOC_COMMENT == $entry[0];
78  }
79  );
80 
81  foreach ($commentBlocks as $commentBlock) {
82  $text = $this->getCommentText($commentBlock[1]);
83  $section = $this->getSectionName($commentBlock[1]);
84 
85  if ($section && $text) {
86  $result[$section] = $text;
87  }
88  }
89 
90  return $result;
91  }
92 
99  private function getCommentText($commentBlock)
100  {
101  $commentsLine = [];
102  foreach (preg_split("/(\r?\n)/", $commentBlock) as $commentLine) {
103  if (preg_match('/^(?=\s+?\*[^\/])(.+)/', $commentLine, $matches)
104  && false === strpos($commentLine, 'For the section')
105  ) {
106  $commentsLine[] = preg_replace('/^(\*\s?)/', '', trim($matches[1]));
107  }
108  }
109 
110  return empty($commentsLine) ? null : implode(PHP_EOL, $commentsLine);
111  }
112 
119  private function getSectionName($comment)
120  {
121  $pattern = '/\s+\* For the section: (.+)\s/';
122  preg_match_all($pattern, $comment, $matches);
123 
124  return empty($matches[1]) ? null : trim(array_shift($matches[1]));
125  }
126 }
$pattern
Definition: website.php:22
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
$fileName
Definition: translate.phtml:15
__construct(Filesystem $filesystem, ConfigFilePool $configFilePool)
$filesystem