public function import(\ReflectionFunction $function) { $this->name = $function->name; $this->function = $function->getShortName(); $this->namespace = $function->getNamespaceName(); $this->_importFromReflection($function); }
public function onWildcardEvent() { $name = $this->events->firing(); $time = microtime(true); // Get the arguments passed to the event $params = $this->prepareParams(func_get_args()); // Find all listeners for the current event foreach ($this->events->getListeners($name) as $i => $listener) { // Check if it's an object + method name if (is_array($listener) && count($listener) > 1 && is_object($listener[0])) { list($class, $method) = $listener; // Skip this class itself if ($class instanceof static) { continue; } // Format the listener to readable format $listener = get_class($class) . '@' . $method; // Handle closures } elseif ($listener instanceof \Closure) { $reflector = new \ReflectionFunction($listener); // Skip our own listeners if ($reflector->getNamespaceName() == 'Barryvdh\\Debugbar') { continue; } // Format the closure to a readable format $filename = ltrim(str_replace(base_path(), '', $reflector->getFileName()), '/'); $listener = $reflector->getName() . ' (' . $filename . ':' . $reflector->getStartLine() . '-' . $reflector->getEndLine() . ')'; } else { // Not sure if this is possible, but to prevent edge cases $listener = $this->formatVar($listener); } $params['listeners.' . $i] = $listener; } $this->addMeasure($name, $time, $time, $params); }
/** * Hash anything, return the unique identity. * * @param $object * @return string */ protected function hash($object) { array_walk_recursive($object, function (&$item) { if ($item instanceof \Closure) { $reflection = new \ReflectionFunction($item); $item = serialize($reflection->getClosureScopeClass()) . $reflection->getNumberOfParameters() . $reflection->getNamespaceName() . $reflection->getStartLine() . $reflection->getEndLine(); } }); return md5(serialize($object)); }
/** * Hash anything (except PDO connection stuff, for now). * * @param mixed $object * @return string */ public static function hash($object) { $object = is_array($object) ? $object : [$object]; array_walk_recursive($object, function ($item) { if ($item instanceof \Closure) { $reflection = new \ReflectionFunction($item); // Unique and fast. $item = serialize($reflection->getClosureScopeClass()) . $reflection->getNumberOfParameters() . $reflection->getNamespaceName() . $reflection->getStartLine() . $reflection->getEndLine(); } }); return md5(serialize($object)); }
protected function execute(InputInterface $input, OutputInterface $output) { $term = $input->getArgument('search_term'); $listeners = $this->get('event.dispatcher')->getListeners(); $events = array(); foreach ($listeners as $eventName => $handlers) { foreach ($handlers as $order => $listener) { if (is_array($listener)) { list($object, $methodName) = $listener; $priority = 0; $object = get_class($object); // get the priority $objectEvents = $object::getSubscribedEvents(); $eventMethods = $objectEvents[$eventName]; if (is_array($eventMethods)) { foreach ($eventMethods as $eventMethod) { if ($eventMethod[0] == $methodName) { $priority = isset($eventMethod[1]) ? $eventMethod[1] : 0; } } } } else { $reflection = new \ReflectionFunction($listener); $file = new \Message\Cog\Filesystem\File($reflection->getFileName()); $object = $reflection->getNamespaceName() . '\\' . $file->getFilenameWithoutExtension(); $methodName = 'Closure (L:' . $reflection->getStartLine() . ' - L:' . $reflection->getEndLine() . ')'; $priority = ''; } // look for the search term if ($term && strpos(strtolower($object), strtolower($term)) === false && strpos(strtolower($methodName), strtolower($term)) === false && strpos(strtolower($eventName), strtolower($term)) === false) { continue; } $events[] = array($object, $methodName, $eventName, $priority, $order); } } // Sort events by event name, then by order of execution. uasort($events, function ($a, $b) { if ($a[2] == $b[2]) { if ($a[4] == $b[4]) { return 0; } return $a[4] < $b[4] ? -1 : 1; } return $a[2] < $b[2] ? -1 : 1; }); $output->writeln('<info>Found ' . count($events) . ' registered event listeners.</info>'); $table = $this->_getTable($output)->setHeaders(array('Class', 'Method', 'Event', 'Priority')); foreach ($events as $event) { $table->addRow(array($event[0], $event[1], $event[2], $event[3])); } $table->render($output); }
function add_internal($internal_classes) { global $functions, $internal_arginfo; foreach ($internal_classes as $class_name) { add_class($class_name, 0); } foreach (get_declared_interfaces() as $class_name) { add_class($class_name); } foreach (get_declared_traits() as $class_name) { add_class($class_name); } foreach (get_defined_functions()['internal'] as $function_name) { $function = new \ReflectionFunction($function_name); $required = $function->getNumberOfRequiredParameters(); $optional = $function->getNumberOfParameters() - $required; $functions[strtolower($function_name)] = ['file' => 'internal', 'namespace' => $function->getNamespaceName(), 'avail' => true, 'conditional' => false, 'flags' => 0, 'lineno' => 0, 'endLineno' => 0, 'name' => $function_name, 'docComment' => '', 'required' => $required, 'optional' => $optional, 'ret' => null, 'params' => []]; add_param_info($function_name); } foreach (array_keys($internal_arginfo) as $function_name) { if (strpos($function_name, ':') !== false) { continue; } $ln = strtolower($function_name); $functions[$ln] = ['file' => 'internal', 'avail' => false, 'conditional' => false, 'flags' => 0, 'lineno' => 0, 'endLineno' => 0, 'name' => $function_name, 'docComment' => '', 'ret' => null, 'params' => []]; add_param_info($function_name); } }
<?php namespace foo; $a = function () { }; $r = new \ReflectionFunction($a); var_dump($r->inNamespace()); var_dump($r->getNamespaceName()); var_dump($r->getName());
<?php namespace A\B; function foo() { } $function = new \ReflectionFunction('sort'); var_dump($function->inNamespace()); var_dump($function->getName()); var_dump($function->getNamespaceName()); var_dump($function->getShortName()); $function = new \ReflectionFunction('A\\B\\foo'); var_dump($function->inNamespace()); var_dump($function->getName()); var_dump($function->getNamespaceName()); var_dump($function->getShortName());
$param .= '$' . $rp->getName(); if ($rp->isOptional() && $rm->isUserDefined()) { $param .= '=' . json_encode($rp->getDefaultValue()); } $params[] = $param; } $info['type'] = '(' . implode(', ', $params) . ')'; fwrite($fp, implode("\t", array($rm->getName(), $filename, $rm->getStartLine() . ';"', '')) . $build($info) . PHP_EOL); } } unset($it); // functions $funcs = get_defined_functions(); foreach ($funcs['internal'] as $func) { $rf = new ReflectionFunction($func); $info = array('kind' => 'f', 'namespace' => $rf->getNamespaceName()); $params = array(); foreach ($rf->getParameters() as $rp) { $class = ''; if (!defined('HHVM_VERSION')) { $class = $rp->getClass(); } $param = ''; if ($class) { $param = $class->getName() . ' '; } elseif ($rp->isArray()) { $param = 'array '; } $param .= '$' . $rp->getName(); if ($rp->isOptional() && $rf->isUserDefined()) { $param .= '=' . json_encode($rp->getDefaultValue());
/** * Analyses a closure. * * @param Model $model * The closure we want to analyse. * * @return string * The generated markup. */ public function analyseClosure(Model $model) { $ref = new \ReflectionFunction($model->getData()); $result = array(); // Adding comments from the file. $methodclass = new ThroughMethods($this->storage); $result['comments'] = $methodclass->prettifyComment($ref->getDocComment()); // Adding the sourcecode $highlight = $ref->getStartLine() - 1; $from = $highlight - 3; $to = $ref->getEndLine() - 1; $file = $ref->getFileName(); $result['source'] = $this->storage->readSourcecode($file, $highlight, $from, $to); // Adding the place where it was declared. $result['declared in'] = $ref->getFileName() . "\n"; $result['declared in'] .= 'in line ' . $ref->getStartLine(); // Adding the namespace, but only if we have one. $namespace = $ref->getNamespaceName(); if (!empty($namespace)) { $result['namespace'] = $namespace; } // Adding the parameters. $parameters = $ref->getParameters(); $paramList = ''; foreach ($parameters as $parameter) { preg_match('/(.*)(?= \\[ )/', $parameter, $key); $parameter = str_replace($key[0], '', $parameter); $result[$key[0]] = trim($parameter, ' []'); $paramList .= trim($result[$key[0]]) . ', '; } $paramList = str_replace(array('<required> ', '<optional> '), '', $this->storage->encodeString($paramList)); // Remove the ',' after the last char. $paramList = '<small>' . trim($paramList, ', ') . '</small>'; $model->setType($model->getAdditional() . ' closure')->setAdditional('. . .')->setConnector2($model->getConnector2() . '(' . $paramList . ')')->setDomid($this->generateDomIdFromObject($model->getData()))->addParameter('data', $result)->initCallback('Iterate\\ThroughMethodAnalysis'); return $this->storage->render->renderExpandableChild($model); }
/** * {@inheritdoc} * * @throws \LogicException */ protected function execute(InputInterface $input, OutputInterface $output) { $verbose = OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity(); $invoke = $input->getOption('invoke'); $sm = $this->getServiceManager()->get('ServiceManager'); $registeredServices = $sm->getRegisteredServicesReal(); $lines = array(); $pad = array('id' => 0, 'type' => strlen('Instance '), 'class' => strlen('Class name|type|alias')); $serviceTypeToColumnName = array('invokableClasses' => 'Invokable', 'factories' => 'Factory', 'aliases' => 'Alias', 'instances' => 'Instance'); foreach ($registeredServices as $type => $services) { foreach ($services as $key => $service) { $lines[$key]['type'] = $serviceTypeToColumnName[$type]; if (strlen($key) > $pad['id']) { $pad['id'] = strlen($key); } if (is_object($service)) { // As of PHP 5.4 you can rely on Closure being a Closure: php.net/manual/en/class.closure.php if ($service instanceof \Closure) { $r = new \ReflectionFunction($service); if ($ns = $r->getNamespaceName()) { $filename = basename($r->getFileName(), '.php'); $lines[$key]['class'] = $ns . '\\' . $filename . '\\{closure}'; } else { $lines[$key]['class'] = 'Closure in ' . $r->getFileName(); } } else { $r = new \ReflectionObject($service); $lines[$key]['class'] = $r->getName(); } } elseif (is_array($service)) { $lines[$key]['class'] = 'Array'; } elseif (is_string($service) && $type != 'aliases') { $r = new \ReflectionClass($service); $lines[$key]['class'] = $r->getName(); } else { // Alias $lines[$key]['class'] = $service; } $len = strlen($lines[$key]['class']); if ('aliases' == $type) { $len += 10; // add the "alias for " prefix } if ($len > $pad['class']) { $pad['class'] = $len; } } } ksort($lines); $output->write(sprintf('<comment>%s</comment> <comment>%s</comment> <comment>%s</comment>', str_pad('Service Id', $pad['id']), str_pad('Type', $pad['type']), str_pad('Class Name|Type|Alias', $pad['class']))); $output->writeln($invoke ? ' <comment>Invokation Status [result]</comment>' : ''); foreach ($lines as $id => $line) { $output->write(sprintf('<info>%s</info> ', str_pad($id, $pad['id']))); $output->write(sprintf('%s ', str_pad($line['type'], $pad['type']))); if ('Alias' == $line['type']) { $output->write(sprintf('<comment>alias for</comment> <info>%s </info>', str_pad($line['class'], $pad['class'] - 10))); } else { $output->write(sprintf('%s ', str_pad($line['class'], $pad['class']))); } if ($invoke) { try { $service = $sm->get($id); $output->write(sprintf(' <info>OK</info> [%s]', is_object($service) ? get_class($service) : gettype($service))); } catch (\Exception $e) { $output->write(' <error>FAIL</error> [' . $e->getMessage() . ']'); } } $output->writeln(''); } }