From 6da52acb3ce849b25adbf0ef9533a0acb7286ddf Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 18 Apr 2014 12:50:23 +0200 Subject: [ticket/12273] Rename exporter to php_exporter PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 559 +++++++++++++++++++++++++++++++++++++ 1 file changed, 559 insertions(+) create mode 100644 phpBB/phpbb/event/php_exporter.php (limited to 'phpBB/phpbb/event/php_exporter.php') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php new file mode 100644 index 0000000000..51fe878d41 --- /dev/null +++ b/phpBB/phpbb/event/php_exporter.php @@ -0,0 +1,559 @@ +root_path = $phpbb_root_path; + $this->events = $this->file_lines = array(); + $this->current_file = $this->current_event = ''; + $this->current_event_line = 0; + } + + public function get_events() + { + return $this->events; + } + + public function set_current_event($name, $line) + { + $this->current_event = $name; + $this->current_event_line = $line; + } + + public function set_content($content) + { + $this->file_lines = $content; + } + + /** + * Crawl the phpBB/ directory for php events + * @return int The number of events found + */ + public function crawl_phpbb_directory_php() + { + $files = $this->get_recursive_file_list($this->root_path); + $this->events = array(); + foreach ($files as $file) + { + $this->crawl_php_file($file); + } + ksort($this->events); + + return sizeof($this->events); + } + + /** + * Returns a list of files in $dir + * + * Works recursive with any depth + * + * @param string $dir Directory to go through + * @param string $path Path from root to $dir + * @return array List of files (including directories) + */ + public function get_recursive_file_list($dir, $path = '') + { + try + { + $iterator = new \DirectoryIterator($dir); + } + catch (\Exception $e) + { + return array(); + } + + $files = array(); + foreach ($iterator as $file_info) + { + /** @var \DirectoryIterator $file_info */ + if ($file_info->isDot()) + { + continue; + } + + // Do not scan some directories + if ($file_info->isDir() && ( + ($path == '' && in_array($file_info->getFilename(), array( + 'cache', + 'develop', + 'ext', + 'files', + 'language', + 'store', + 'vendor', + ))) + || ($path == '/includes' && in_array($file_info->getFilename(), array('utf'))) + || ($path == '/phpbb/db/migration' && in_array($file_info->getFilename(), array('data'))) + || ($path == '/phpbb' && in_array($file_info->getFilename(), array('event'))) + )) + { + continue; + } + else if ($file_info->isDir()) + { + $sub_dir = $this->get_recursive_file_list($file_info->getPath() . '/' . $file_info->getFilename(), $path . '/' . $file_info->getFilename()); + foreach ($sub_dir as $file) + { + $files[] = $file_info->getFilename() . '/' . $file; + } + } + else if ($file_info->getExtension() == 'php') + { + $files[] = $file_info->getFilename(); + } + } + + return $files; + } + + /** + * Format the php events as a wiki table + * @return string + */ + public function export_events_for_wiki() + { + $wiki_page = '= PHP Events (Hook Locations) =' . "\n"; + $wiki_page .= '{| class="sortable zebra" cellspacing="0" cellpadding="5"' . "\n"; + $wiki_page .= '! Identifier !! Placement !! Arguments !! Added in Release !! Explanation' . "\n"; + foreach ($this->events as $event) + { + $wiki_page .= '|- id="' . $event['event'] . '"' . "\n"; + $wiki_page .= '| [[#' . $event['event'] . '|' . $event['event'] . ']] || ' . $event['file'] . ' || ' . implode(', ', $event['arguments']) . ' || ' . $event['since'] . ' || ' . $event['description'] . "\n"; + } + $wiki_page .= '|}' . "\n"; + + return $wiki_page; + } + + /** + * @param $file + * @throws \LogicException + */ + public function crawl_php_file($file) + { + $this->current_file = $file; + $this->file_lines = array(); + $content = file_get_contents($this->root_path . $this->current_file); + + if (strpos($content, "dispatcher->trigger_event('") || strpos($content, "dispatcher->dispatch('")) + { + $this->set_content(explode("\n", $content)); + for ($i = 0, $num_lines = sizeof($this->file_lines); $i < $num_lines; $i++) + { + $event_line = false; + $found_trigger_event = strpos($this->file_lines[$i], "dispatcher->trigger_event('"); + $arguments = array(); + if ($found_trigger_event !== false) + { + $event_line = $i; + $this->set_current_event($this->get_trigger_event_name($this->file_lines[$event_line]), $event_line); + + // Find variables of the event + $arguments = $this->get_vars_from_array(); + $doc_vars = $this->get_vars_from_docblock(); + $this->validate_vars_docblock_array($arguments, $doc_vars); + } + else + { + $found_dispatch = strpos($this->file_lines[$i], "dispatcher->dispatch('"); + if ($found_dispatch !== false) + { + $event_line = $i; + $this->set_current_event($this->get_dispatch_name($this->file_lines[$event_line]), $event_line); + } + } + + if ($event_line) + { + // Validate @event + $event_line_num = $this->find_event(); + $this->validate_event($this->current_event, $this->file_lines[$event_line_num]); + + // Validate @since + $since_line_num = $this->find_since(); + $since = $this->validate_since($this->file_lines[$since_line_num]); + + // Find event description line + $description_line_num = $this->find_description(); + $description = substr(trim($this->file_lines[$description_line_num]), strlen('* ')); + + if (isset($this->events[$this->current_event])) + { + throw new \LogicException('The event "' . $this->current_event . '" from file "' . $this->current_file + . '" already exists in file "'. $this->events[$this->current_event]['file'] . '"', 10); + } + + $this->events[$this->current_event] = array( + 'event' => $this->current_event, + 'file' => $this->current_file, + 'arguments' => $arguments, + 'since' => $since, + 'description' => $description, + ); + } + } + } + } + + /** + * Find the name of the event inside the dispatch() line + * + * @param string $event_line + * @return int Absolute line number + * @throws \LogicException + */ + public function get_dispatch_name($event_line) + { + $event_line = ltrim($event_line, "\t"); + + $regex = '#\$([a-z](?:[a-z0-9_]|->)*)'; + $regex .= '->dispatch\('; + $regex .= '\'' . $this->preg_match_event_name() . '\''; + $regex .= '\);#'; + + $match = array(); + preg_match($regex, $event_line, $match); + if (!isset($match[2])) + { + throw new \LogicException('Can not find event name in line "' . $event_line . '" in file "' . $this->current_file . '"', 1); + } + + return $match[2]; + } + + /** + * Find the name of the event inside the trigger_event() line + * + * @param string $event_line + * @return int Absolute line number + * @throws \LogicException + */ + public function get_trigger_event_name($event_line) + { + $event_line = ltrim($event_line, "\t"); + + $regex = '#extract\(\$([a-z](?:[a-z0-9_]|->)*)'; + $regex .= '->trigger_event\('; + $regex .= '\'' . $this->preg_match_event_name() . '\''; + $regex .= ', compact\(\$vars\)\)\);#'; + + $match = array(); + preg_match($regex, $event_line, $match); + if (!isset($match[2])) + { + throw new \LogicException('Can not find event name in line "' . $event_line . '" in file "' . $this->current_file . '"', 1); + } + + return $match[2]; + } + + /** + * Returns a regex match for the event name + * + * @return string + */ + protected function preg_match_event_name() + { + return '([a-z][a-z0-9_]*(?:\.[a-z][a-z0-9_]*)+)'; + } + + /** + * Find the $vars array + * + * @return array List of variables + * @throws \LogicException + */ + public function get_vars_from_array() + { + $vars_line = ltrim($this->file_lines[$this->current_event_line - 1], "\t"); + if (strpos($vars_line, "\$vars = array('") !== 0 || substr($vars_line, -3) !== '\');') + { + throw new \LogicException('Can not find "$vars = array();"-line for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + } + + $vars_array = substr($vars_line, strlen("\$vars = array('"), 0 - strlen('\');')); + if ($vars_array === '') + { + throw new \LogicException('Found empty $vars array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + } + + $vars_array = explode("', '", $vars_array); + + foreach ($vars_array as $var) + { + if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) + { + throw new \LogicException('Found invalid var "' . $var . '" in array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); + } + } + + sort($vars_array); + return $vars_array; + } + + /** + * Find the $vars array + * + * @return array List of variables + * @throws \LogicException + */ + public function get_vars_from_docblock() + { + $doc_vars = array(); + $current_doc_line = 1; + $found_comment_end = false; + while (ltrim($this->file_lines[$this->current_event_line - $current_doc_line], "\t") !== '/**') + { + if (ltrim($this->file_lines[$this->current_event_line - $current_doc_line], "\t") === '*/') + { + $found_comment_end = true; + } + + if ($found_comment_end) + { + $var_line = trim($this->file_lines[$this->current_event_line - $current_doc_line]); + $var_line = preg_replace('!\s+!', ' ', $var_line); + if (strpos($var_line, '* @var ') === 0) + { + $doc_line = explode(' ', $var_line, 5); + if (sizeof($doc_line) !== 5) + { + throw new \LogicException('Found invalid line "' . $this->file_lines[$this->current_event_line - $current_doc_line] + . '" for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + } + $doc_vars[] = $doc_line[3]; + } + } + + $current_doc_line++; + if ($current_doc_line > $this->current_event_line) + { + // Reached the start of the file + throw new \LogicException('Can not find end of docblock for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + } + } + + if (empty($doc_vars)) + { + // Reached the start of the file + throw new \LogicException('Can not find @var lines for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); + } + + foreach ($doc_vars as $var) + { + if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) + { + throw new \LogicException('Found invalid @var "' . $var . '" in docblock for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 4); + } + } + + sort($doc_vars); + return $doc_vars; + } + + /** + * Find the "@since" Information line + * + * @return int Absolute line number + * @throws \LogicException + */ + public function find_since() + { + return $this->find_tag('since', array('event', 'var')); + } + + /** + * Find the "@event" Information line + * + * @return int Absolute line number + */ + public function find_event() + { + return $this->find_tag('event', array()); + } + + /** + * Find a "@*" Information line + * + * @param string $find_tag Name of the tag we are trying to find + * @param array $disallowed_tags List of tags that must not appear between + * the tag and the actual event + * @return int Absolute line number + * @throws \LogicException + */ + public function find_tag($find_tag, $disallowed_tags) + { + $find_tag_line = 0; + $found_comment_end = false; + while (strpos(ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t"), '* @' . $find_tag . ' ') !== 0) + { + if ($found_comment_end && ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t") === '/**') + { + // Reached the start of this doc block + throw new \LogicException('Can not find @' . $find_tag . ' information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + } + + foreach ($disallowed_tags as $disallowed_tag) + { + if ($found_comment_end && strpos(ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t"), '* @' . $disallowed_tag) === 0) + { + // Found @var after the @since + throw new \LogicException('Found @' . $disallowed_tag . ' information after @' . $find_tag . ' for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); + } + } + + if (ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t") === '*/') + { + $found_comment_end = true; + } + + $find_tag_line++; + if ($find_tag_line >= $this->current_event_line) + { + // Reached the start of the file + throw new \LogicException('Can not find @' . $find_tag . ' information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + } + } + + return $this->current_event_line - $find_tag_line; + } + + /** + * Find a "@*" Information line + * + * @return int Absolute line number + * @throws \LogicException + */ + public function find_description() + { + $find_desc_line = 0; + while (ltrim($this->file_lines[$this->current_event_line - $find_desc_line], "\t") !== '/**') + { + $find_desc_line++; + if ($find_desc_line > $this->current_event_line) + { + // Reached the start of the file + throw new \LogicException('Can not find a description for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + } + } + + $find_desc_line = $this->current_event_line - $find_desc_line + 1; + + $desc = trim($this->file_lines[$find_desc_line]); + if (strpos($desc, '* @') === 0 || $desc[0] !== '*' || substr($desc, 1) == '') + { + // First line of the doc block is a @-line, empty or only contains "*" + throw new \LogicException('Can not find a description for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + } + + return $find_desc_line; + } + + /** + * Validate "@since" Information + * + * @param string $line + * @return string + * @throws \LogicException + */ + public function validate_since($line) + { + $since = substr(ltrim($line, "\t"), strlen('* @since ')); + + if ($since !== trim($since)) + { + throw new \LogicException('Invalid @since information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + } + + $since = ($since === '3.1-A1') ? '3.1.0-a1' : $since; + + if (!preg_match('#^\d+\.\d+\.\d+(?:-(?:a|b|rc|pl)\d+)?$#', $since)) + { + throw new \LogicException('Invalid @since information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + } + + return $since; + } + + /** + * Validate "@event" Information + * + * @param string $event_name + * @param string $line + * @return string + * @throws \LogicException + */ + public function validate_event($event_name, $line) + { + $event = substr(ltrim($line, "\t"), strlen('* @event ')); + + if ($event !== trim($event)) + { + throw new \LogicException('Invalid @event information for event "' . $event_name . '" in file "' . $this->current_file . '"', 1); + } + + if ($event !== $event_name) + { + throw new \LogicException('Event name does not match @event tag for event "' . $event_name . '" in file "' . $this->current_file . '"', 2); + } + + return $event; + } + + /** + * Validates that two arrays contain the same strings + * + * @param array $vars_array Variables found in the array line + * @param array $vars_docblock Variables found in the doc block + * @return null + * @throws \LogicException + */ + public function validate_vars_docblock_array($vars_array, $vars_docblock) + { + $vars_array = array_unique($vars_array); + $vars_docblock = array_unique($vars_docblock); + $sizeof_vars_array = sizeof($vars_array); + + if ($sizeof_vars_array !== sizeof($vars_docblock) || $sizeof_vars_array !== sizeof(array_intersect($vars_array, $vars_docblock))) + { + throw new \LogicException('$vars array does not match the list of @var tags for event "' . $this->current_event . '" in file "' . $this->current_file . '"'); + } + } +} -- cgit v1.2.1 From 9ba9a024d6a72eb81d77347eb866e73556b81051 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 18 Apr 2014 13:06:13 +0200 Subject: [ticket/12273] Make event exporter compatible with php 5.3.3 PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/phpbb/event/php_exporter.php') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 51fe878d41..eb9e8f72b3 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -134,7 +134,7 @@ class php_exporter $files[] = $file_info->getFilename() . '/' . $file; } } - else if ($file_info->getExtension() == 'php') + else if (substr($file_info->getFilename(), -4) == '.php') { $files[] = $file_info->getFilename(); } -- cgit v1.2.1 From 8ddc9ff185527128d74b618107edde2f57637dde Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 20 Apr 2014 14:15:54 +0200 Subject: [ticket/12273] Allow multiple $vars lines PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'phpBB/phpbb/event/php_exporter.php') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index eb9e8f72b3..9044168980 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -302,25 +302,43 @@ class php_exporter */ public function get_vars_from_array() { - $vars_line = ltrim($this->file_lines[$this->current_event_line - 1], "\t"); - if (strpos($vars_line, "\$vars = array('") !== 0 || substr($vars_line, -3) !== '\');') + $vars_array_line = 1; + $found_vars_array = false; + $vars_array = array(); + while (ltrim($this->file_lines[$this->current_event_line - $vars_array_line], "\t") !== '*/') { - throw new \LogicException('Can not find "$vars = array();"-line for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + $line = ltrim($this->file_lines[$this->current_event_line - $vars_array_line], "\t"); + $match = array(); + preg_match('#^\$vars (?:\+)?= array\(\'([a-zA-Z0-9_\' ,]+)\'\);$#', $line, $match); + + if (isset($match[1])) + { + $found_vars_array = true; + if (strlen($match[1]) > 90) + { + throw new \LogicException('Should use multiple lines for $vars definition' + . ' for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + } + $vars_array = array_merge($vars_array, explode("', '", $match[1])); + } + + $vars_array_line++; + if ($this->current_event_line - $vars_array_line === 0) + { + throw new \LogicException('Can not find "$vars = array();"-line for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + } } - $vars_array = substr($vars_line, strlen("\$vars = array('"), 0 - strlen('\');')); - if ($vars_array === '') + if (!$found_vars_array) { - throw new \LogicException('Found empty $vars array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + throw new \LogicException('Can not find "$vars = array();"-line for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); } - $vars_array = explode("', '", $vars_array); - foreach ($vars_array as $var) { if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) { - throw new \LogicException('Found invalid var "' . $var . '" in array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); + throw new \LogicException('Found invalid var "' . $var . '" in array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 4); } } -- cgit v1.2.1 From b83a555da5df5b6916e4ae15aee10815bdac65d5 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 20 Apr 2014 14:28:09 +0200 Subject: [ticket/12273] Move phpBB test to new file and use a data provider PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'phpBB/phpbb/event/php_exporter.php') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 9044168980..1e680ec896 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -163,7 +163,8 @@ class php_exporter } /** - * @param $file + * @param string $file + * @return int Number of events found in this file * @throws \LogicException */ public function crawl_php_file($file) @@ -171,6 +172,7 @@ class php_exporter $this->current_file = $file; $this->file_lines = array(); $content = file_get_contents($this->root_path . $this->current_file); + $num_events_found = 0; if (strpos($content, "dispatcher->trigger_event('") || strpos($content, "dispatcher->dispatch('")) { @@ -227,9 +229,12 @@ class php_exporter 'since' => $since, 'description' => $description, ); + $num_events_found++; } } } + + return $num_events_found; } /** -- cgit v1.2.1 From 6849e8b36f5d097a413c87a1321a09b9209a9d71 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 20 Apr 2014 14:57:18 +0200 Subject: [ticket/12273] Add file line to exception message PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 73 +++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 29 deletions(-) (limited to 'phpBB/phpbb/event/php_exporter.php') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 1e680ec896..88301f86f6 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -185,7 +185,7 @@ class php_exporter if ($found_trigger_event !== false) { $event_line = $i; - $this->set_current_event($this->get_trigger_event_name($this->file_lines[$event_line]), $event_line); + $this->set_current_event($this->get_trigger_event_name($event_line), $event_line); // Find variables of the event $arguments = $this->get_vars_from_array(); @@ -198,7 +198,7 @@ class php_exporter if ($found_dispatch !== false) { $event_line = $i; - $this->set_current_event($this->get_dispatch_name($this->file_lines[$event_line]), $event_line); + $this->set_current_event($this->get_dispatch_name($event_line), $event_line); } } @@ -240,13 +240,14 @@ class php_exporter /** * Find the name of the event inside the dispatch() line * - * @param string $event_line + * @param int $event_line * @return int Absolute line number * @throws \LogicException */ public function get_dispatch_name($event_line) { - $event_line = ltrim($event_line, "\t"); + $event_text_line = $this->file_lines[$event_line]; + $event_text_line = ltrim($event_text_line, "\t"); $regex = '#\$([a-z](?:[a-z0-9_]|->)*)'; $regex .= '->dispatch\('; @@ -254,10 +255,11 @@ class php_exporter $regex .= '\);#'; $match = array(); - preg_match($regex, $event_line, $match); + preg_match($regex, $event_text_line, $match); if (!isset($match[2])) { - throw new \LogicException('Can not find event name in line "' . $event_line . '" in file "' . $this->current_file . '"', 1); + throw new \LogicException("Can not find event name in line '{$event_text_line}' " + . "in file '{$this->current_file}:{$event_line}'", 1); } return $match[2]; @@ -266,13 +268,14 @@ class php_exporter /** * Find the name of the event inside the trigger_event() line * - * @param string $event_line + * @param int $event_line * @return int Absolute line number * @throws \LogicException */ public function get_trigger_event_name($event_line) { - $event_line = ltrim($event_line, "\t"); + $event_text_line = $this->file_lines[$event_line]; + $event_text_line = ltrim($event_text_line, "\t"); $regex = '#extract\(\$([a-z](?:[a-z0-9_]|->)*)'; $regex .= '->trigger_event\('; @@ -280,10 +283,11 @@ class php_exporter $regex .= ', compact\(\$vars\)\)\);#'; $match = array(); - preg_match($regex, $event_line, $match); + preg_match($regex, $event_text_line, $match); if (!isset($match[2])) { - throw new \LogicException('Can not find event name in line "' . $event_line . '" in file "' . $this->current_file . '"', 1); + throw new \LogicException("Can not find event name in line '{$event_text_line}' " + . "in file '{$this->current_file}:{$event_line}'", 1); } return $match[2]; @@ -322,7 +326,7 @@ class php_exporter if (strlen($match[1]) > 90) { throw new \LogicException('Should use multiple lines for $vars definition' - . ' for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + . "for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); } $vars_array = array_merge($vars_array, explode("', '", $match[1])); } @@ -330,20 +334,20 @@ class php_exporter $vars_array_line++; if ($this->current_event_line - $vars_array_line === 0) { - throw new \LogicException('Can not find "$vars = array();"-line for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + throw new \LogicException("Can not find '\$vars = array();'-line for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } } if (!$found_vars_array) { - throw new \LogicException('Can not find "$vars = array();"-line for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); + throw new \LogicException("Can not find '\$vars = array();'-line for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); } foreach ($vars_array as $var) { if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) { - throw new \LogicException('Found invalid var "' . $var . '" in array for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 4); + throw new \LogicException("Found invalid var '{$var}' in array for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 4); } } @@ -378,8 +382,8 @@ class php_exporter $doc_line = explode(' ', $var_line, 5); if (sizeof($doc_line) !== 5) { - throw new \LogicException('Found invalid line "' . $this->file_lines[$this->current_event_line - $current_doc_line] - . '" for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + throw new \LogicException("Found invalid line '{$this->file_lines[$this->current_event_line - $current_doc_line]}'" + . "for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 1); } $doc_vars[] = $doc_line[3]; } @@ -389,21 +393,22 @@ class php_exporter if ($current_doc_line > $this->current_event_line) { // Reached the start of the file - throw new \LogicException('Can not find end of docblock for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + throw new \LogicException("Can not find end of docblock for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } } if (empty($doc_vars)) { // Reached the start of the file - throw new \LogicException('Can not find @var lines for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); + throw new \LogicException("Can not find @var lines for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); } foreach ($doc_vars as $var) { if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) { - throw new \LogicException('Found invalid @var "' . $var . '" in docblock for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 4); + throw new \LogicException("Found invalid @var '{$var}' in docblock for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 4); } } @@ -450,7 +455,8 @@ class php_exporter if ($found_comment_end && ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t") === '/**') { // Reached the start of this doc block - throw new \LogicException('Can not find @' . $find_tag . ' information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + throw new \LogicException("Can not find '@{$find_tag}' information for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 1); } foreach ($disallowed_tags as $disallowed_tag) @@ -458,7 +464,8 @@ class php_exporter if ($found_comment_end && strpos(ltrim($this->file_lines[$this->current_event_line - $find_tag_line], "\t"), '* @' . $disallowed_tag) === 0) { // Found @var after the @since - throw new \LogicException('Found @' . $disallowed_tag . ' information after @' . $find_tag . ' for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 3); + throw new \LogicException("Found '@{$disallowed_tag}' information after '@{$find_tag}' for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); } } @@ -471,7 +478,8 @@ class php_exporter if ($find_tag_line >= $this->current_event_line) { // Reached the start of the file - throw new \LogicException('Can not find @' . $find_tag . ' information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + throw new \LogicException("Can not find '@{$find_tag}' information for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } } @@ -493,7 +501,8 @@ class php_exporter if ($find_desc_line > $this->current_event_line) { // Reached the start of the file - throw new \LogicException('Can not find a description for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + throw new \LogicException("Can not find a description for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 1); } } @@ -503,7 +512,8 @@ class php_exporter if (strpos($desc, '* @') === 0 || $desc[0] !== '*' || substr($desc, 1) == '') { // First line of the doc block is a @-line, empty or only contains "*" - throw new \LogicException('Can not find a description for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + throw new \LogicException("Can not find a description for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } return $find_desc_line; @@ -522,14 +532,16 @@ class php_exporter if ($since !== trim($since)) { - throw new \LogicException('Invalid @since information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 1); + throw new \LogicException("Invalid '@since' information for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 1); } $since = ($since === '3.1-A1') ? '3.1.0-a1' : $since; if (!preg_match('#^\d+\.\d+\.\d+(?:-(?:a|b|rc|pl)\d+)?$#', $since)) { - throw new \LogicException('Invalid @since information for event "' . $this->current_event . '" in file "' . $this->current_file . '"', 2); + throw new \LogicException("Invalid '@since' information for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } return $since; @@ -549,12 +561,14 @@ class php_exporter if ($event !== trim($event)) { - throw new \LogicException('Invalid @event information for event "' . $event_name . '" in file "' . $this->current_file . '"', 1); + throw new \LogicException("Invalid '@event' information for event " + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 1); } if ($event !== $event_name) { - throw new \LogicException('Event name does not match @event tag for event "' . $event_name . '" in file "' . $this->current_file . '"', 2); + throw new \LogicException("Event name does not match '@event' tag for event" + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } return $event; @@ -576,7 +590,8 @@ class php_exporter if ($sizeof_vars_array !== sizeof($vars_docblock) || $sizeof_vars_array !== sizeof(array_intersect($vars_array, $vars_docblock))) { - throw new \LogicException('$vars array does not match the list of @var tags for event "' . $this->current_event . '" in file "' . $this->current_file . '"'); + throw new \LogicException("\$vars array does not match the list of '@var' tags for event" + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'"); } } } -- cgit v1.2.1 From 08cce5fba548374c8c067ee8bed8aa6d388367e5 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 20 Apr 2014 14:58:24 +0200 Subject: [ticket/12273] Remove duplicated code PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 49 +++++++++++++------------------------- 1 file changed, 16 insertions(+), 33 deletions(-) (limited to 'phpBB/phpbb/event/php_exporter.php') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 88301f86f6..c076d7567d 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -185,7 +185,7 @@ class php_exporter if ($found_trigger_event !== false) { $event_line = $i; - $this->set_current_event($this->get_trigger_event_name($event_line), $event_line); + $this->set_current_event($this->get_event_name($event_line, false), $event_line); // Find variables of the event $arguments = $this->get_vars_from_array(); @@ -198,7 +198,7 @@ class php_exporter if ($found_dispatch !== false) { $event_line = $i; - $this->set_current_event($this->get_dispatch_name($event_line), $event_line); + $this->set_current_event($this->get_event_name($event_line, true), $event_line); } } @@ -241,46 +241,29 @@ class php_exporter * Find the name of the event inside the dispatch() line * * @param int $event_line + * @param bool $is_dispatch Do we look for dispatch() or trigger_event() ? * @return int Absolute line number * @throws \LogicException */ - public function get_dispatch_name($event_line) + public function get_event_name($event_line, $is_dispatch) { $event_text_line = $this->file_lines[$event_line]; $event_text_line = ltrim($event_text_line, "\t"); - $regex = '#\$([a-z](?:[a-z0-9_]|->)*)'; - $regex .= '->dispatch\('; - $regex .= '\'' . $this->preg_match_event_name() . '\''; - $regex .= '\);#'; - - $match = array(); - preg_match($regex, $event_text_line, $match); - if (!isset($match[2])) + if ($is_dispatch) { - throw new \LogicException("Can not find event name in line '{$event_text_line}' " - . "in file '{$this->current_file}:{$event_line}'", 1); + $regex = '#\$([a-z](?:[a-z0-9_]|->)*)'; + $regex .= '->dispatch\('; + $regex .= '\'' . $this->preg_match_event_name() . '\''; + $regex .= '\);#'; + } + else + { + $regex = '#extract\(\$([a-z](?:[a-z0-9_]|->)*)'; + $regex .= '->trigger_event\('; + $regex .= '\'' . $this->preg_match_event_name() . '\''; + $regex .= ', compact\(\$vars\)\)\);#'; } - - return $match[2]; - } - - /** - * Find the name of the event inside the trigger_event() line - * - * @param int $event_line - * @return int Absolute line number - * @throws \LogicException - */ - public function get_trigger_event_name($event_line) - { - $event_text_line = $this->file_lines[$event_line]; - $event_text_line = ltrim($event_text_line, "\t"); - - $regex = '#extract\(\$([a-z](?:[a-z0-9_]|->)*)'; - $regex .= '->trigger_event\('; - $regex .= '\'' . $this->preg_match_event_name() . '\''; - $regex .= ', compact\(\$vars\)\)\);#'; $match = array(); preg_match($regex, $event_text_line, $match); -- cgit v1.2.1 From bc1ab3dc79b63091c82a29f5c5175558423ac8cd Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 20 Apr 2014 15:11:23 +0200 Subject: [ticket/12273] Fix missing space in some Exceptions PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'phpBB/phpbb/event/php_exporter.php') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index c076d7567d..3956be600b 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -218,8 +218,9 @@ class php_exporter if (isset($this->events[$this->current_event])) { - throw new \LogicException('The event "' . $this->current_event . '" from file "' . $this->current_file - . '" already exists in file "'. $this->events[$this->current_event]['file'] . '"', 10); + throw new \LogicException("The event '{$this->current_event}' from file " + . "'{$this->current_file}:{$event_line_num}' already exists in file " + . "'{$this->events[$this->current_event]['file']}'", 10); } $this->events[$this->current_event] = array( @@ -308,7 +309,7 @@ class php_exporter $found_vars_array = true; if (strlen($match[1]) > 90) { - throw new \LogicException('Should use multiple lines for $vars definition' + throw new \LogicException('Should use multiple lines for $vars definition ' . "for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); } $vars_array = array_merge($vars_array, explode("', '", $match[1])); @@ -365,7 +366,7 @@ class php_exporter $doc_line = explode(' ', $var_line, 5); if (sizeof($doc_line) !== 5) { - throw new \LogicException("Found invalid line '{$this->file_lines[$this->current_event_line - $current_doc_line]}'" + throw new \LogicException("Found invalid line '{$this->file_lines[$this->current_event_line - $current_doc_line]}' " . "for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 1); } $doc_vars[] = $doc_line[3]; @@ -550,7 +551,7 @@ class php_exporter if ($event !== $event_name) { - throw new \LogicException("Event name does not match '@event' tag for event" + throw new \LogicException("Event name does not match '@event' tag for event " . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } @@ -573,7 +574,7 @@ class php_exporter if ($sizeof_vars_array !== sizeof($vars_docblock) || $sizeof_vars_array !== sizeof(array_intersect($vars_array, $vars_docblock))) { - throw new \LogicException("\$vars array does not match the list of '@var' tags for event" + throw new \LogicException("\$vars array does not match the list of '@var' tags for event " . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'"); } } -- cgit v1.2.1 From b0a9acd8ff953736d6aeb39095aca278c0869563 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 24 Apr 2014 17:14:12 +0200 Subject: [ticket/12273] Use array_merge instead of += PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'phpBB/phpbb/event/php_exporter.php') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 3956be600b..8de3051d9b 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -302,17 +302,17 @@ class php_exporter { $line = ltrim($this->file_lines[$this->current_event_line - $vars_array_line], "\t"); $match = array(); - preg_match('#^\$vars (?:\+)?= array\(\'([a-zA-Z0-9_\' ,]+)\'\);$#', $line, $match); + preg_match('#^\$vars = (array_merge\(\$vars, )?array\(\'([a-zA-Z0-9_\' ,]+)\'\)(?(1)\));$#', $line, $match); - if (isset($match[1])) + if (isset($match[2])) { $found_vars_array = true; - if (strlen($match[1]) > 90) + if (strlen($match[2]) > 90) { throw new \LogicException('Should use multiple lines for $vars definition ' . "for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); } - $vars_array = array_merge($vars_array, explode("', '", $match[1])); + $vars_array = array_merge($vars_array, explode("', '", $match[2])); } $vars_array_line++; -- cgit v1.2.1 From 142fe1a0e57797aa77059edda42ebedd91b11397 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 25 Apr 2014 11:45:54 +0200 Subject: [ticket/12273] Use multiline arrays instead of array_merge() PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 84 ++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 26 deletions(-) (limited to 'phpBB/phpbb/event/php_exporter.php') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 8de3051d9b..2a69f15802 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -295,48 +295,80 @@ class php_exporter */ public function get_vars_from_array() { - $vars_array_line = 1; - $found_vars_array = false; - $vars_array = array(); - while (ltrim($this->file_lines[$this->current_event_line - $vars_array_line], "\t") !== '*/') + $line = ltrim($this->file_lines[$this->current_event_line - 1], "\t"); + if ($line === ');') { - $line = ltrim($this->file_lines[$this->current_event_line - $vars_array_line], "\t"); - $match = array(); - preg_match('#^\$vars = (array_merge\(\$vars, )?array\(\'([a-zA-Z0-9_\' ,]+)\'\)(?(1)\));$#', $line, $match); + $vars_array = $this->get_vars_from_multi_line_array(); + } + else + { + $vars_array = $this->get_vars_from_single_line_array($line); + } - if (isset($match[2])) + foreach ($vars_array as $var) + { + if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) { - $found_vars_array = true; - if (strlen($match[2]) > 90) - { - throw new \LogicException('Should use multiple lines for $vars definition ' - . "for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); - } - $vars_array = array_merge($vars_array, explode("', '", $match[2])); + throw new \LogicException("Found invalid var '{$var}' in array for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); } + } + + sort($vars_array); + return $vars_array; + } + + /** + * Find the variables in single line array + * + * @param string $line + * @return array List of variables + * @throws \LogicException + */ + public function get_vars_from_single_line_array($line, $throw_multiline = true) + { + $match = array(); + preg_match('#^\$vars = array\(\'([a-zA-Z0-9_\' ,]+)\'\);$#', $line, $match); - $vars_array_line++; - if ($this->current_event_line - $vars_array_line === 0) + if (isset($match[1])) + { + $vars_array = explode("', '", $match[1]); + if ($throw_multiline && sizeof($vars_array) > 6) { - throw new \LogicException("Can not find '\$vars = array();'-line for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); + throw new \LogicException('Should use multiple lines for $vars definition ' + . "for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } + return $vars_array; } - - if (!$found_vars_array) + else { - throw new \LogicException("Can not find '\$vars = array();'-line for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 3); + throw new \LogicException("Can not find '\$vars = array();'-line for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 1); } + } - foreach ($vars_array as $var) + /** + * Find the variables in single line array + * + * @param string $line + * @return array List of variables + * @throws \LogicException + */ + public function get_vars_from_multi_line_array() + { + $current_vars_line = 2; + $var_lines = array(); + while (ltrim($this->file_lines[$this->current_event_line - $current_vars_line], "\t") !== '$vars = array(') { - if (!preg_match('#^([a-zA-Z_][a-zA-Z0-9_]*)$#', $var)) + $var_lines[] = substr(trim($this->file_lines[$this->current_event_line - $current_vars_line]), 0, -1); + + $current_vars_line++; + if ($current_vars_line > $this->current_event_line) { - throw new \LogicException("Found invalid var '{$var}' in array for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 4); + // Reached the start of the file + throw new \LogicException("Can not find end of \$vars array for event '{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); } } - sort($vars_array); - return $vars_array; + return $this->get_vars_from_single_line_array('$vars = array(' . implode(", ", $var_lines) . ');', false); } /** -- cgit v1.2.1 From 48278f122cf9a2b23c39eccdd998b0c9b0552b2a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 25 Apr 2014 12:31:57 +0200 Subject: [ticket/12273] Do not allow 3.1-A1 version PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) (limited to 'phpBB/phpbb/event/php_exporter.php') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 2a69f15802..ac88ffa8b5 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -544,23 +544,15 @@ class php_exporter */ public function validate_since($line) { - $since = substr(ltrim($line, "\t"), strlen('* @since ')); - - if ($since !== trim($since)) - { - throw new \LogicException("Invalid '@since' information for event " - . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 1); - } - - $since = ($since === '3.1-A1') ? '3.1.0-a1' : $since; - - if (!preg_match('#^\d+\.\d+\.\d+(?:-(?:a|b|rc|pl)\d+)?$#', $since)) + $match = array(); + preg_match('#^\* @since (\d+\.\d+\.\d+(?:-(?:a|b|rc|pl)\d+)?)$#', ltrim($line, "\t"), $match); + if (!isset($match[1])) { throw new \LogicException("Invalid '@since' information for event " - . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'", 2); + . "'{$this->current_event}' in file '{$this->current_file}:{$this->current_event_line}'"); } - return $since; + return $match[1]; } /** -- cgit v1.2.1 From 3bcda97e28f096558baa46be63e8bb40f43c59c8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 25 Apr 2014 12:37:39 +0200 Subject: [ticket/12273] Sort arguments alphabetically before exporting PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 1 + 1 file changed, 1 insertion(+) (limited to 'phpBB/phpbb/event/php_exporter.php') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index ac88ffa8b5..a92bd2671d 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -223,6 +223,7 @@ class php_exporter . "'{$this->events[$this->current_event]['file']}'", 10); } + sort($arguments); $this->events[$this->current_event] = array( 'event' => $this->current_event, 'file' => $this->current_file, -- cgit v1.2.1 From 6aa8596d4dc206797e4740465e3035c65bcdba3f Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 26 Apr 2014 16:30:19 +0200 Subject: [ticket/12273] Fix doc blocks PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'phpBB/phpbb/event/php_exporter.php') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index a92bd2671d..41058216e8 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -46,17 +46,35 @@ class php_exporter $this->current_event_line = 0; } + /** + * Get the list of all events + * + * @return array Array with events: name => details + */ public function get_events() { return $this->events; } + /** + * Set current event data + * + * @param string $name Name of the current event (used for error messages) + * @param int $line Line where the current event is placed in + * @return null + */ public function set_current_event($name, $line) { $this->current_event = $name; $this->current_event_line = $line; } + /** + * Set the content of this file + * + * @param array $content Array with the lines of the file + * @return null + */ public function set_content($content) { $this->file_lines = $content; @@ -322,6 +340,8 @@ class php_exporter * Find the variables in single line array * * @param string $line + * @param bool $throw_multiline Throw an exception when there are too + * many arguments in one line. * @return array List of variables * @throws \LogicException */ @@ -349,7 +369,6 @@ class php_exporter /** * Find the variables in single line array * - * @param string $line * @return array List of variables * @throws \LogicException */ -- cgit v1.2.1 From 927c219b1fc1cc69f724827771d567384b2cda8c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 26 Apr 2014 18:04:47 +0200 Subject: [ticket/12273] Use RecursiveDirectoryIterator with filter in php_exporter PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 56 ++++++++++---------------------------- 1 file changed, 14 insertions(+), 42 deletions(-) (limited to 'phpBB/phpbb/event/php_exporter.php') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 41058216e8..8e4a9d399d 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -100,17 +100,22 @@ class php_exporter /** * Returns a list of files in $dir * - * Works recursive with any depth - * * @param string $dir Directory to go through - * @param string $path Path from root to $dir - * @return array List of files (including directories) + * @return array List of files (including the path) */ - public function get_recursive_file_list($dir, $path = '') + public function get_recursive_file_list($dir) { try { - $iterator = new \DirectoryIterator($dir); + $iterator = new \RecursiveIteratorIterator( + new \phpbb\event\recursive_event_filter_iterator( + new \RecursiveDirectoryIterator( + $dir, + \FilesystemIterator::SKIP_DOTS + ) + ), + \RecursiveIteratorIterator::LEAVES_ONLY + ); } catch (\Exception $e) { @@ -120,42 +125,9 @@ class php_exporter $files = array(); foreach ($iterator as $file_info) { - /** @var \DirectoryIterator $file_info */ - if ($file_info->isDot()) - { - continue; - } - - // Do not scan some directories - if ($file_info->isDir() && ( - ($path == '' && in_array($file_info->getFilename(), array( - 'cache', - 'develop', - 'ext', - 'files', - 'language', - 'store', - 'vendor', - ))) - || ($path == '/includes' && in_array($file_info->getFilename(), array('utf'))) - || ($path == '/phpbb/db/migration' && in_array($file_info->getFilename(), array('data'))) - || ($path == '/phpbb' && in_array($file_info->getFilename(), array('event'))) - )) - { - continue; - } - else if ($file_info->isDir()) - { - $sub_dir = $this->get_recursive_file_list($file_info->getPath() . '/' . $file_info->getFilename(), $path . '/' . $file_info->getFilename()); - foreach ($sub_dir as $file) - { - $files[] = $file_info->getFilename() . '/' . $file; - } - } - else if (substr($file_info->getFilename(), -4) == '.php') - { - $files[] = $file_info->getFilename(); - } + /** @var \RecursiveDirectoryIterator $file_info */ + $relative_path = $iterator->getInnerIterator()->getSubPathname(); + $files[] = str_replace(DIRECTORY_SEPARATOR, '/', $relative_path); } return $files; -- cgit v1.2.1 From dbac4bb5c07973a14469169a6bcc2feeb024306b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 27 Apr 2014 23:51:06 +0200 Subject: [ticket/12273] Add root path to recursive_event_filter_iterator PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'phpBB/phpbb/event/php_exporter.php') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 8e4a9d399d..e95426244b 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -112,7 +112,8 @@ class php_exporter new \RecursiveDirectoryIterator( $dir, \FilesystemIterator::SKIP_DOTS - ) + ), + $dir ), \RecursiveIteratorIterator::LEAVES_ONLY ); -- cgit v1.2.1 From ed669982c2a8225e262d57b60f5654658799becd Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 28 Apr 2014 21:03:59 +0200 Subject: [ticket/12273] Allow to filter events for extensions PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'phpBB/phpbb/event/php_exporter.php') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index e95426244b..4e77d336b1 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -17,7 +17,10 @@ namespace phpbb\event; */ class php_exporter { - /** @var string */ + /** @var string Path where we look for files*/ + protected $path; + + /** @var string phpBB Root Path */ protected $root_path; /** @var string */ @@ -41,6 +44,7 @@ class php_exporter public function __construct($phpbb_root_path) { $this->root_path = $phpbb_root_path; + $this->path = $phpbb_root_path; $this->events = $this->file_lines = array(); $this->current_file = $this->current_event = ''; $this->current_event_line = 0; @@ -82,11 +86,18 @@ class php_exporter /** * Crawl the phpBB/ directory for php events + * @param mixed $extension String 'vendor/ext' to filter, null for phpBB core * @return int The number of events found */ - public function crawl_phpbb_directory_php() + public function crawl_phpbb_directory_php($extension = null) { - $files = $this->get_recursive_file_list($this->root_path); + $this->path = $this->root_path; + if ($extension) + { + $this->path .= 'ext/' . $extension . '/'; + } + + $files = $this->get_recursive_file_list(); $this->events = array(); foreach ($files as $file) { @@ -100,20 +111,19 @@ class php_exporter /** * Returns a list of files in $dir * - * @param string $dir Directory to go through * @return array List of files (including the path) */ - public function get_recursive_file_list($dir) + public function get_recursive_file_list() { try { $iterator = new \RecursiveIteratorIterator( new \phpbb\event\recursive_event_filter_iterator( new \RecursiveDirectoryIterator( - $dir, + $this->path, \FilesystemIterator::SKIP_DOTS ), - $dir + $this->path ), \RecursiveIteratorIterator::LEAVES_ONLY ); @@ -162,7 +172,7 @@ class php_exporter { $this->current_file = $file; $this->file_lines = array(); - $content = file_get_contents($this->root_path . $this->current_file); + $content = file_get_contents($this->path . $this->current_file); $num_events_found = 0; if (strpos($content, "dispatcher->trigger_event('") || strpos($content, "dispatcher->dispatch('")) -- cgit v1.2.1 From da6eb0282dd90ac4c8f019d8516d274e60db4861 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 28 Apr 2014 21:08:48 +0200 Subject: [ticket/12273] Fix return description PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/phpbb/event/php_exporter.php') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 4e77d336b1..879a7eb5ab 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -245,7 +245,7 @@ class php_exporter * * @param int $event_line * @param bool $is_dispatch Do we look for dispatch() or trigger_event() ? - * @return int Absolute line number + * @return string Name of the event * @throws \LogicException */ public function get_event_name($event_line, $is_dispatch) -- cgit v1.2.1 From 7f90ff70acd036a022db924bb8d0e0d3f64b8ff8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 28 Apr 2014 23:39:37 +0200 Subject: [ticket/12273] Move $extension to constructor so the path is always set PHPBB3-12273 --- phpBB/phpbb/event/php_exporter.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'phpBB/phpbb/event/php_exporter.php') diff --git a/phpBB/phpbb/event/php_exporter.php b/phpBB/phpbb/event/php_exporter.php index 879a7eb5ab..d86ee3c045 100644 --- a/phpBB/phpbb/event/php_exporter.php +++ b/phpBB/phpbb/event/php_exporter.php @@ -40,14 +40,21 @@ class php_exporter /** * @param string $phpbb_root_path + * @param mixed $extension String 'vendor/ext' to filter, null for phpBB core */ - public function __construct($phpbb_root_path) + public function __construct($phpbb_root_path, $extension = null) { $this->root_path = $phpbb_root_path; $this->path = $phpbb_root_path; $this->events = $this->file_lines = array(); $this->current_file = $this->current_event = ''; $this->current_event_line = 0; + + $this->path = $this->root_path; + if ($extension) + { + $this->path .= 'ext/' . $extension . '/'; + } } /** @@ -86,17 +93,10 @@ class php_exporter /** * Crawl the phpBB/ directory for php events - * @param mixed $extension String 'vendor/ext' to filter, null for phpBB core * @return int The number of events found */ - public function crawl_phpbb_directory_php($extension = null) + public function crawl_phpbb_directory_php() { - $this->path = $this->root_path; - if ($extension) - { - $this->path .= 'ext/' . $extension . '/'; - } - $files = $this->get_recursive_file_list(); $this->events = array(); foreach ($files as $file) -- cgit v1.2.1