Finds a pattern in a block of code.
public static find ( string $code, string $pattern, array $options = [] ) : array | ||
$code | string | |
$pattern | string | |
$options | array | The list of options to be used when parsing / matching `$code`: - 'ignore': An array of token names to ignore while parsing, defaults to `array('T_WHITESPACE')` - 'lineBreaks': If true, all tokens in a single pattern match must appear on the same line of code, defaults to false - 'startOfLine': If true, the pattern must match starting with the beginning of the line of code to be matched, defaults to false |
Résultat | array |
/** * Gets the static and dynamic dependencies for a class or group of classes. * * @param mixed $classes Either a string specifying a class, or a numerically indexed array * of classes * @param array $options * @return array An array of the static and dynamic class dependencies * @todo Document valid options */ public static function dependencies($classes, array $options = array()) { $defaults = array('type' => null); $options += $defaults; $static = $dynamic = array(); $trim = function ($c) { return trim(trim($c, '\\')); }; $join = function ($i) { return join('', $i); }; foreach ((array) $classes as $class) { $data = explode("\n", file_get_contents(Libraries::path($class))); $data = "<?php \n" . join("\n", preg_grep('/^\\s*use /', $data)) . "\n ?>"; $classes = array_map($join, Parser::find($data, 'use *;', array('return' => 'content', 'lineBreaks' => true, 'startOfLine' => true, 'capture' => array('T_STRING', 'T_NS_SEPARATOR')))); if ($classes) { $static = array_unique(array_merge($static, array_map($trim, $classes))); } $classes = static::info($class . '::$_classes', array('value')); if (isset($classes['value'])) { $dynamic = array_merge($dynamic, array_map($trim, array_values($classes['value']))); } } if (empty($options['type'])) { return array_unique(array_merge($static, $dynamic)); } $type = $options['type']; return isset(${$type}) ? ${$type} : null; }
public function testFindingTokenPatterns() { $code = file_get_contents(Libraries::path('lithium\\analysis\\Parser')); $expected = array('tokenize', 'matchToken', '_prepareMatchParams', 'token'); $results = array_values(array_unique(array_map(function ($i) { return $i[0]; }, Parser::find($code, 'static::_(*)', array('capture' => array('T_STRING'), 'return' => 'content'))))); $this->assertEqual($expected, $results); $expected = array('lithium\\util\\Set', 'lithium\\util\\Collection'); $results = array_map(function ($i) { return join('', $i); }, $results = Parser::find($code, 'use *;', array('return' => 'content', 'lineBreaks' => true, 'startOfLine' => true, 'capture' => array('T_STRING', 'T_NS_SEPARATOR')))); $this->assertEqual($expected, $results); $code = 'function test($options) { return function($foo) use ($options) {'; $code .= ' ClassName::method($options); ' . "\n" . ' $foo->method($options); }; }'; list($results) = Parser::find($code, '_::_(', array('capture' => array('T_STRING'), 'return' => 'content')); $expected = array('ClassName', 'method'); $this->assertEqual($expected, $results); }