本文整理汇总了PHP中ReflectionClass::inNamespace方法的典型用法代码示例。如果您正苦于以下问题:PHP ReflectionClass::inNamespace方法的具体用法?PHP ReflectionClass::inNamespace怎么用?PHP ReflectionClass::inNamespace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ReflectionClass
的用法示例。
在下文中一共展示了ReflectionClass::inNamespace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: load_class
/**
* Load classes
*
* @param String Class name of class for loading
* @param string Directory of class
* @param boolean If class need instantiation
* @return object
*/
function load_class($class, $directory = 'core', $instantiate = FALSE )
{
static $_classes = array();
$class_path = ROOT . '/' . $directory . '/' . $class;
// return classe instance if exists (singleton pattern)
if (isset($_classes[$class]))
{
return $_classes[$class];
}
if (file_exists($class_path . '.php'))
{
require $class_path . '.php';
$reflection = new ReflectionClass("\\$directory\\$class");
if($reflection->inNamespace())
{
$class = $reflection->getNamespaceName() . '\\' . $class;
}
if($instantiate === TRUE)
{
$_classes[$class] = new $class();
return $_classes[$class];
}
}
}
示例2: getInheritanceInspection
/**
* Returns inheritance inspection.
*
* The inspection result:
*
* must:
*
* * shortName
* * name
* * userDefined
*
* optional:
*
* * namespace
*
* @param \ReflectionClass $declaringClass ReflectionClass object.
* @return array Inheritance inspection.
*/
protected function getInheritanceInspection(\ReflectionClass $declaringClass)
{
$inheritanceInspection = array('shortname' => $declaringClass->getShortName(), 'name' => $declaringClass->getName(), 'filename' => $declaringClass->getFileName(), 'userDefined' => $declaringClass->isUserDefined());
if ($declaringClass->inNamespace()) {
$inheritanceInspection['namespace'] = $declaringClass->getNamespaceName();
}
return $inheritanceInspection;
}
示例3: get
public function get($entity)
{
$function = new \ReflectionClass($entity);
$className = $function->getShortName();
$transformerClassName = sprintf("AppBundle\\Response\\Transformer\\%sTransformer", $className);
if (class_exists($transformerClassName) === false) {
throw new \Exception(sprintf("Transformer class for entity %s does not exist.", $function->inNamespace()));
}
$transformerClass = new $transformerClassName();
return $transformerClass;
}
示例4: getExistingClasses
/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function getExistingClasses(array $paths) : array
{
$namespaceList = [];
$classList = [];
$list = [];
foreach ($paths as $path) {
if (false === $path->isSourceCode()) {
continue;
}
$iter = new ClassIterator($this->getPhpFiles($path->getPathBase()));
foreach (array_keys($iter->getClassMap()) as $classname) {
if (in_array(substr($classname, -4), ['Test', 'Spec'])) {
continue;
}
$reflection = new \ReflectionClass($classname);
if ($reflection->isAbstract() || $reflection->isTrait() || $reflection->isInterface()) {
continue;
}
if ($reflection->inNamespace()) {
$namespaces = explode('\\', $reflection->getNamespaceName());
$namespace = '';
foreach ($namespaces as $key => $namespacePart) {
if ($namespace !== '') {
$namespace .= '/';
}
$namespace .= $namespacePart;
if (!array_key_exists($key, $namespaceList)) {
$namespaceList[$key] = [];
}
$namespaceList[$key][$namespace] = $namespace;
}
}
$escapedName = str_replace('\\', '/', $classname);
$classList[$escapedName] = $escapedName;
}
}
foreach ($namespaceList as $namespaceDepthList) {
$list = array_merge($list, $namespaceDepthList);
}
$list = array_merge($list, $classList);
return $list;
}
示例5: expandClassName
/**
* Expands class name into FQN.
* @param string
* @return string fully qualified class name
* @throws Nette\InvalidArgumentException
*/
public static function expandClassName($name, \ReflectionClass $reflector)
{
if (empty($name)) {
throw new Nette\InvalidArgumentException('Class name must not be empty.');
} elseif ($name === 'self') {
return $reflector->getName();
} elseif ($name[0] === '\\') { // already fully qualified
return ltrim($name, '\\');
}
$filename = $reflector->getFileName();
$parsed = static::getCache()->load($filename, function (& $dp) use ($filename) {
if (AnnotationsParser::$autoRefresh) {
$dp[Nette\Caching\Cache::FILES] = $filename;
}
return AnnotationsParser::parsePhp(file_get_contents($filename));
});
$uses = array_change_key_case((array) $tmp = & $parsed[$reflector->getName()]['use']);
$parts = explode('\\', $name, 2);
$parts[0] = strtolower($parts[0]);
if (isset($uses[$parts[0]])) {
$parts[0] = $uses[$parts[0]];
return implode('\\', $parts);
} elseif ($reflector->inNamespace()) {
return $reflector->getNamespaceName() . '\\' . $name;
} else {
return $name;
}
}
示例6: load
/**
* Loads a list of classes and caches them in one big file.
*
* @param array $classes An array of classes to load
* @param string $cacheDir A cache directory
* @param string $name The cache name prefix
* @param Boolean $autoReload Whether to flush the cache when the cache is stale or not
* @param Boolean $adaptive Whether to remove already declared classes or not
* @param string $extension File extension of the resulting file
*
* @throws \InvalidArgumentException When class can't be loaded
*/
static public function load($classes, $cacheDir, $name, $autoReload, $adaptive = false, $extension = '.php')
{
// each $name can only be loaded once per PHP process
if (isset(self::$loaded[$name])) {
return;
}
self::$loaded[$name] = true;
if ($adaptive) {
// don't include already declared classes
$classes = array_diff($classes, get_declared_classes(), get_declared_interfaces());
// the cache is different depending on which classes are already declared
$name = $name.'-'.substr(md5(implode('|', $classes)), 0, 5);
}
$cache = $cacheDir.'/'.$name.$extension;
// auto-reload
$reload = false;
if ($autoReload) {
$metadata = $cacheDir.'/'.$name.$extension.'.meta';
if (!is_file($metadata) || !is_file($cache)) {
$reload = true;
} else {
$time = filemtime($cache);
$meta = unserialize(file_get_contents($metadata));
if ($meta[1] != $classes) {
$reload = true;
} else {
foreach ($meta[0] as $resource) {
if (!is_file($resource) || filemtime($resource) > $time) {
$reload = true;
break;
}
}
}
}
}
if (!$reload && is_file($cache)) {
require_once $cache;
return;
}
$files = array();
$content = '';
foreach ($classes as $class) {
if (!class_exists($class) && !interface_exists($class) && (!function_exists('trait_exists') || !trait_exists($class))) {
throw new \InvalidArgumentException(sprintf('Unable to load class "%s"', $class));
}
$r = new \ReflectionClass($class);
$files[] = $r->getFileName();
$c = preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', file_get_contents($r->getFileName()));
// add namespace declaration for global code
if (!$r->inNamespace()) {
$c = "\nnamespace\n{\n".self::stripComments($c)."\n}\n";
} else {
$c = self::fixNamespaceDeclarations('<?php '.$c);
$c = preg_replace('/^\s*<\?php/', '', $c);
}
$content .= $c;
}
// cache the core classes
if (!is_dir(dirname($cache))) {
mkdir(dirname($cache), 0777, true);
}
self::writeCacheFile($cache, '<?php '.$content);
if ($autoReload) {
// save the resources
self::writeCacheFile($metadata, serialize(array($files, $classes)));
}
}
示例7: getMagicMethods
/**
* Returns array of magic methods defined by annotation @method.
* @return array
*/
public static function getMagicMethods($class)
{
$rc = new \ReflectionClass($class);
preg_match_all('~^
[ \\t*]* @method [ \\t]+
(?: [^\\s(]+ [ \\t]+ )?
(set|get|is|add) ([A-Z]\\w*) [ \\t]*
(?: \\( [ \\t]* ([^)$\\s]+) )?
()~mx', $rc->getDocComment(), $matches, PREG_SET_ORDER);
$methods = array();
foreach ($matches as $m) {
list(, $op, $prop, $type) = $m;
$name = $op . $prop;
$prop = strtolower($prop[0]) . substr($prop, 1) . ($op === 'add' ? 's' : '');
if ($rc->hasProperty($prop) && ($rp = $rc->getProperty($prop)) && !$rp->isStatic()) {
$rp->setAccessible(TRUE);
if ($op === 'get' || $op === 'is') {
$type = NULL;
$op = 'get';
} elseif (!$type && preg_match('#@var[ \\t]+(\\S+)' . ($op === 'add' ? '\\[\\]#' : '#'), $rp->getDocComment(), $m)) {
$type = $m[1];
}
if ($rc->inNamespace() && preg_match('#^[A-Z]\\w+(\\[|\\||\\z)#', $type)) {
$type = $rc->getNamespaceName() . '\\' . $type;
}
$methods[$name] = array($op, $rp, $type);
}
}
return $methods;
}
示例8: getMagicMethods
/**
* Returns array of magic methods defined by annotation @method.
* @return array
*/
public static function getMagicMethods(string $class) : array
{
$rc = new \ReflectionClass($class);
preg_match_all('~^
[ \\t*]* @method [ \\t]+
(?: [^\\s(]+ [ \\t]+ )?
(set|get|is|add) ([A-Z]\\w*)
(?: ([ \\t]* \\() [ \\t]* ([^)$\\s]*) )?
()~mx', (string) $rc->getDocComment(), $matches, PREG_SET_ORDER);
$methods = [];
foreach ($matches as list(, $op, $prop, $bracket, $type)) {
if ($bracket !== '(') {
trigger_error("Bracket must be immediately after @method {$op}{$prop}() in class {$class}.", E_USER_WARNING);
}
$name = $op . $prop;
$prop = strtolower($prop[0]) . substr($prop, 1) . ($op === 'add' ? 's' : '');
if ($rc->hasProperty($prop) && ($rp = $rc->getProperty($prop)) && !$rp->isStatic()) {
$rp->setAccessible(TRUE);
if ($op === 'get' || $op === 'is') {
$type = NULL;
$op = 'get';
} elseif (!$type && preg_match('#@var[ \\t]+(\\S+)' . ($op === 'add' ? '\\[\\]#' : '#'), (string) $rp->getDocComment(), $m)) {
$type = $m[1];
}
if ($rc->inNamespace() && preg_match('#^[A-Z]\\w+(\\[|\\||\\z)#', (string) $type)) {
$type = $rc->getNamespaceName() . '\\' . $type;
}
$methods[$name] = [$op, $rp, $type];
}
}
return $methods;
}
示例9: expandClassName
/**
* Expands class name into FQN.
* @param string
* @return string fully qualified class name
* @throws Nette\InvalidArgumentException
*/
public static function expandClassName($name, \ReflectionClass $reflector)
{
if (empty($name)) {
throw new Nette\InvalidArgumentException('Class name must not be empty.');
}
if ($name[0] === '\\') {
// already fully qualified
return ltrim($name, '\\');
}
$parsed = static::parsePhp(file_get_contents($reflector->getFileName()));
$uses = array_change_key_case((array) ($tmp =& $parsed[$reflector->getName()]['use']));
$parts = explode('\\', $name, 2);
$parts[0] = strtolower($parts[0]);
if (isset($uses[$parts[0]])) {
$parts[0] = $uses[$parts[0]];
return implode('\\', $parts);
} elseif ($reflector->inNamespace()) {
return $reflector->getNamespaceName() . '\\' . $name;
} else {
return $name;
}
}
示例10: testGeneratedEntityRepositoryClassCustomDefaultRepository
/**
* @group DDC-3231
*/
public function testGeneratedEntityRepositoryClassCustomDefaultRepository()
{
$em = $this->_getTestEntityManager();
$ns = $this->_namespace;
require_once __DIR__ . '/../../Models/DDC3231/DDC3231User2.php';
$className = $ns . '\\DDC3231User2Tmp';
$this->writeEntityClass('Doctrine\\Tests\\Models\\DDC3231\\DDC3231User2', $className);
$rpath = $this->writeRepositoryClass($className, 'Doctrine\\Tests\\Models\\DDC3231\\DDC3231EntityRepository');
$this->assertNotNull($rpath);
$this->assertFileExists($rpath);
require $rpath;
$repo = new \ReflectionClass($em->getRepository($className));
$this->assertTrue($repo->inNamespace());
$this->assertSame($className . 'Repository', $repo->getName());
$this->assertSame('Doctrine\\Tests\\Models\\DDC3231\\DDC3231EntityRepository', $repo->getParentClass()->getName());
require_once __DIR__ . '/../../Models/DDC3231/DDC3231User2NoNamespace.php';
$className2 = 'DDC3231User2NoNamespaceTmp';
$this->writeEntityClass('DDC3231User2NoNamespace', $className2);
$rpath2 = $this->writeRepositoryClass($className2, 'Doctrine\\Tests\\Models\\DDC3231\\DDC3231EntityRepository');
$this->assertNotNull($rpath2);
$this->assertFileExists($rpath2);
require $rpath2;
$repo2 = new \ReflectionClass($em->getRepository($className2));
$this->assertFalse($repo2->inNamespace());
$this->assertSame($className2 . 'Repository', $repo2->getName());
$this->assertSame('Doctrine\\Tests\\Models\\DDC3231\\DDC3231EntityRepository', $repo2->getParentClass()->getName());
}
示例11: load
public static function load($classes, $cacheDir, $name, $autoReload, $adaptive = false)
{
if (isset(self::$loaded[$name])) {
return;
}
self::$loaded[$name] = true;
$classes = array_unique($classes);
if ($adaptive) {
$classes = array_diff($classes, get_declared_classes(), get_declared_interfaces());
$name = $name . '-' . substr(md5(implode('|', $classes)), 0, 5);
}
$cache = $cacheDir . '/' . $name . '.php';
$reload = false;
if ($autoReload) {
$metadata = $cacheDir . '/' . $name . '.meta';
if (!file_exists($metadata) || !file_exists($cache)) {
$reload = true;
} else {
$time = filemtime($cache);
$meta = unserialize(file_get_contents($metadata));
if ($meta[1] != $classes) {
$reload = true;
} else {
foreach ($meta[0] as $resource) {
if (!file_exists($resource) || filemtime($resource) > $time) {
$reload = true;
break;
}
}
}
}
}
if (!$reload && file_exists($cache)) {
require_once $cache;
return;
}
$files = array();
$content = '';
foreach ($classes as $class) {
if (!class_exists($class) && !interface_exists($class)) {
throw new \InvalidArgumentException(sprintf('Unable to load class "%s"', $class));
}
$r = new \ReflectionClass($class);
$files[] = $r->getFileName();
$c = preg_replace(array('/^\\s*<\\?php/', '/\\?>\\s*$/'), '', file_get_contents($r->getFileName()));
if (!$r->inNamespace()) {
$c = "\nnamespace\n{\n{$c}\n}\n";
} else {
$c = self::fixNamespaceDeclarations('<?php ' . $c);
$c = preg_replace('/^\\s*<\\?php/', '', $c);
}
$content .= $c;
}
if (!is_dir(dirname($cache))) {
mkdir(dirname($cache), 0777, true);
}
self::writeCacheFile($cache, Kernel::stripComments('<?php ' . $content));
if ($autoReload) {
self::writeCacheFile($metadata, serialize(array($files, $classes)));
}
}
示例12: catch
/**
* Return the local class for an object or a class that can be loaded.
*
* @param object|string $object
*
* @return string
*/
static function get_local_class($object)
{
$class_name = is_object($object) ? get_class($object) : $object;
try {
$reflector = new \ReflectionClass($class_name);
if ($reflector->inNamespace()) {
$base_class = $reflector->getShortName();
} else {
$base_class = $class_name;
}
} catch (\Exception $e) {
$base_class = $class_name;
}
return $base_class;
}
示例13: inNamespace
/**
* {@inheritdoc}
*/
public function inNamespace()
{
return $this->reflectionClass->inNamespace();
}
示例14: inspectNamespace
/**
* Inspect namespace.
*
* @return void
*/
protected function inspectNamespace()
{
if ($this->class->inNamespace()) {
$this->inspection['namespace'] = $this->class->getNamespaceName();
}
}
示例15: expandClassName
/**
* Expands class name into full name.
* @param string
* @return string full name
* @throws Nette\InvalidArgumentException
*/
public static function expandClassName(string $name, \ReflectionClass $rc) : string
{
$lower = strtolower($name);
if (empty($name)) {
throw new Nette\InvalidArgumentException('Class name must not be empty.');
} elseif (in_array($lower, ['string', 'int', 'float', 'bool', 'array', 'callable'], TRUE)) {
return $lower;
} elseif ($lower === 'self') {
return $rc->getName();
} elseif ($name[0] === '\\') {
// fully qualified name
return ltrim($name, '\\');
}
$uses =& self::$cache[$rc->getName()];
if ($uses === NULL) {
self::$cache = self::parseUseStatemenets(file_get_contents($rc->getFileName()), $rc->getName()) + self::$cache;
$uses =& self::$cache[$rc->getName()];
}
$parts = explode('\\', $name, 2);
if (isset($uses[$parts[0]])) {
$parts[0] = $uses[$parts[0]];
return implode('\\', $parts);
} elseif ($rc->inNamespace()) {
return $rc->getNamespaceName() . '\\' . $name;
} else {
return $name;
}
}