本文整理汇总了PHP中ReflectionClass::isInstantiable方法的典型用法代码示例。如果您正苦于以下问题:PHP ReflectionClass::isInstantiable方法的具体用法?PHP ReflectionClass::isInstantiable怎么用?PHP ReflectionClass::isInstantiable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ReflectionClass
的用法示例。
在下文中一共展示了ReflectionClass::isInstantiable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
public function __construct(DiInterface $di, $class)
{
if (!is_string($class) && !is_object($class)) {
throw new Exception("第二个参数只能接受字符串或对象");
}
$this->_di = $di;
$this->_class = new ReflectionClass($class);
if (is_object($class)) {
$this->_instance = $class;
} elseif (is_string($class) && !$this->_class->isInstantiable()) {
throw new Exception("不能对一个无法实例化的类进行注入");
}
$this->_interfaceNames = $this->_class->getInterfaceNames();
}
示例2: run
public static function run()
{
// run setup and prepare env
static::setup();
// handle the requested uri
$uri = static::parse();
$segments = array();
if (strlen($uri)) {
$segments = explode('/', $uri);
}
// lets log our translated uri
Log::info('Translated URI: ' . $uri);
// set our action or our default if none is set
$action = count($segments) ? array_shift($segments) : 'page';
// default to our front end router
$controller = 'Routes';
// set the template path
$theme = Config::get('metadata.theme');
Template::path(PATH . 'themes/' . $theme . '/');
// remove admin as an argument and set the default action if there isnt one
if ($action == 'admin') {
// set default controller for the admin
$controller = (count($segments) ? array_shift($segments) : 'posts') . '_controller';
// set default action
$action = count($segments) ? array_shift($segments) : 'index';
// public admin actions
$public = array('users/login', 'users/amnesia', 'users/reset');
// redirect to login
if (Users::authed() === false and in_array(trim($controller, '_controller') . '/' . $action, $public) === false) {
return Response::redirect(Config::get('application.admin_folder') . '/users/login');
}
// set template path for admin
Template::path(PATH . 'system/admin/theme/');
}
// log the controller we are going to use and the action
Log::info('Controller action: ' . $controller . '/' . $action);
// check we can find a action
$reflector = new ReflectionClass($controller);
if ($reflector->isInstantiable() === false or $reflector->hasMethod($action) === false) {
// default back to front end template for 404 page
Template::path(PATH . 'themes/' . $theme . '/');
// report error
Log::warning($reflector->isInstantiable() === false ? 'Controller was not Instantiable' : 'Action does not exist');
// method not found in controller
return Response::error(404);
}
$reflector->getMethod($action)->invokeArgs(new $controller(), $segments);
}
示例3: doaction
public function doaction($name)
{
if (isset(self::$action_info[$name])) {
try {
global $plugin;
//var_dump(self::$action_info);
if (file_exists(dirname(__FILE__) . "/{$plugin}/" . self::$action_info[$name]['file'])) {
include_once dirname(__FILE__) . "/{$plugin}/" . self::$action_info[$name]['file'];
}
if (isset(self::$action_info[$name]['class'])) {
$classname = self::$action_info[$name]['class'];
$class = new ReflectionClass($classname);
if ($class->isInstantiable()) {
$methodname = self::$action_info[$name]['method'];
$method = $class->newInstance();
$path = $method->{$methodname}();
//global $plugin;
header("Location: " . $plugin . "/" . self::$action_info[$name]['returns'][$path]);
//require(dirname(__file__)."../../".self::$action_info[$name]['returns'][$path]);
}
} else {
$method = self::$action_info[$name]['method'];
$path = $method();
global $plugin;
header("Location: " . $plugin . "/" . self::$action_info[$name]['returns'][$path]);
}
} catch (Exception $e) {
echo "在反射解释{$name} 出现错误\n";
}
} else {
header("Location: help/noaction.html");
}
}
示例4: getAccessibilityScope
/**
* Get Global Application CMS accessibility scope.
*
* @access public
* @static
* @uses Core\Config()
*
* @return array
*/
public static function getAccessibilityScope()
{
$scope = glob(Core\Config()->paths('mode') . 'controllers' . DIRECTORY_SEPARATOR . '*.php');
$builtin_scope = array('CMS\\Controllers\\CMS');
$builtin_actions = array();
$accessibility_scope = array();
foreach ($builtin_scope as $resource) {
$builtin_actions = array_merge($builtin_actions, get_class_methods($resource));
}
$builtin_actions = array_filter($builtin_actions, function ($action) {
return !in_array($action, array('create', 'show', 'edit', 'delete', 'export'), true);
});
foreach ($scope as $resource) {
$resource = basename(str_replace('.php', '', $resource));
if ($resource !== 'cms') {
$controller_name = '\\CMS\\Controllers\\' . $resource;
$controller_class = new \ReflectionClass($controller_name);
if (!$controller_class->isInstantiable()) {
continue;
}
/* Create instance only if the controller class is instantiable */
$controller_object = new $controller_name();
if ($controller_object instanceof CMS\Controllers\CMS) {
$accessibility_scope[$resource] = array_diff(get_class_methods($controller_name), $builtin_actions);
array_push($accessibility_scope[$resource], 'index');
foreach ($accessibility_scope[$resource] as $key => $action_with_acl) {
if (in_array($action_with_acl, $controller_object->skipAclFor, true)) {
unset($accessibility_scope[$resource][$key]);
}
}
}
}
}
return $accessibility_scope;
}
示例5: run
public function run()
{
foreach (Finder::findFiles('*Test.php')->from(__DIR__) as $fileInfo) {
/** @var \SplFileInfo $fileInfo*/
$baseName = $fileInfo->getBasename('.php');
if ($baseName === 'PhpRQTest') {
continue;
}
$className = 'PhpRQ\\' . $baseName;
$reflection = new \ReflectionClass($className);
if (!$reflection->isInstantiable()) {
continue;
}
foreach ($reflection->getMethods() as $method) {
if (!$method->isPublic() || strpos($methodName = $method->getName(), 'test') === false) {
continue;
}
$phpdoc = $method->getDocComment();
if ($phpdoc !== false && ($providerPos = strpos($phpdoc, '@dataProvider')) !== false) {
$providerMethodPos = $providerPos + 14;
$providerMethodLen = strpos($phpdoc, "\n", $providerMethodPos) - $providerMethodPos;
$providerMethod = substr($phpdoc, $providerMethodPos, $providerMethodLen);
$testCase = new $className($this->provider->getRedisClient());
foreach ($testCase->{$providerMethod}() as $args) {
$testCase = new $className($this->provider->getRedisClient());
call_user_func_array([$testCase, $methodName], (array) $args);
}
} else {
$testCase = new $className($this->provider->getRedisClient());
$testCase->{$methodName}();
}
}
}
}
示例6: find_classes
function find_classes($input, $recursive = true)
{
$classes = array();
if (is_file($input)) {
if (preg_match('/\\.php$/', $input)) {
$classes = determine_classes($input);
}
} elseif (is_dir($input)) {
$iter = null;
if ($recursive) {
$iter = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($input), RecursiveIteratorIterator::LEAVES_ONLY);
} else {
$iter = new DirectoryIterator($input);
}
foreach ($iter as $i) {
if ($i->isFile()) {
if (preg_match('/\\.php$/', $i)) {
$found = determine_classes($i->getPathname());
$classes = array_merge($classes, $found);
}
}
}
}
$classes = array_unique($classes);
$found = array();
foreach ($classes as $c) {
$rc = new \ReflectionClass($c);
if ($rc->isInstantiable()) {
$found[] = $c;
}
}
return $found;
}
示例7: build
/**
* 自动绑定(Autowiring)自动解析(Automatic Resolution)
*
* @param string $className
* @return object
* @throws Exception
*/
public function build($className)
{
var_dump($className);
// 如果是匿名函数(Anonymous functions),也叫闭包函数(closures)
if ($className instanceof \Closure) {
// 执行闭包函数,并将结果
return $className($this);
}
/** @var ReflectionClass $reflector */
$reflector = new \ReflectionClass($className);
// 检查类是否可实例化, 排除抽象类abstract和对象接口interface
if (!$reflector->isInstantiable()) {
throw new Exception("Can't instantiate this.");
}
/** @var ReflectionMethod $constructor 获取类的构造函数 */
$constructor = $reflector->getConstructor();
// 若无构造函数,直接实例化并返回
if (is_null($constructor)) {
return new $className();
}
// 取构造函数参数,通过 ReflectionParameter 数组返回参数列表
$parameters = $constructor->getParameters();
// 递归解析构造函数的参数
$dependencies = $this->getDependencies($parameters);
// 创建一个类的新实例,给出的参数将传递到类的构造函数。
return $reflector->newInstanceArgs($dependencies);
}
示例8: getInstance
/**
* @param $name
* @return mixed
* @throws \Exception
*/
public function getInstance($name)
{
if (!is_string($name)) {
throw new \Exception('Name must be a string');
}
$name = rtrim($name, '\\');
if (isset($this->instance[$name])) {
return $this->instance[$name]();
} else {
$reflectionClass = new \ReflectionClass($name);
if (!$reflectionClass->isInstantiable()) {
throw new \Exception('Class ' . $name . ' is not instantiable');
}
$constructor = $reflectionClass->getConstructor();
if (!is_null($constructor)) {
$parameters = $constructor->getParameters();
$params = $this->getReflectionParametersValues($parameters);
} else {
$params = array();
}
$this->setInstance($name, function () use($reflectionClass, $params) {
return $reflectionClass->newInstanceArgs($params);
});
return $this->getInstance($name);
}
}
示例9: dump
/**
* Returns the definition as string representation.
*
* @return string
*/
public function dump(ObjectDefinition $definition)
{
$className = $definition->getClassName();
$classExist = class_exists($className) || interface_exists($className);
// Class
if (!$classExist) {
$warning = '#UNKNOWN# ';
} else {
$class = new \ReflectionClass($className);
$warning = $class->isInstantiable() ? '' : '#NOT INSTANTIABLE# ';
}
$str = sprintf(' class = %s%s', $warning, $className);
// Scope
$str .= PHP_EOL . ' scope = ' . $definition->getScope();
// Lazy
$str .= PHP_EOL . ' lazy = ' . var_export($definition->isLazy(), true);
if ($classExist) {
// Constructor
$str .= $this->dumpConstructor($className, $definition);
// Properties
$str .= $this->dumpProperties($definition);
// Methods
$str .= $this->dumpMethods($className, $definition);
}
return sprintf('Object (' . PHP_EOL . '%s' . PHP_EOL . ')', $str);
}
示例10: find_handler
function find_handler($path)
{
global $g_maps;
$route_path = substr($path, 5);
if ($g_maps && isset($g_maps[$route_path])) {
$object = $g_maps[$route_path]['class'];
$action = $g_maps[$route_path]['method'];
} else {
if (preg_match('/^\\/rest\\/(\\w+)\\/(\\w+)\\/?[^\\/]*$/i', $path, $ret)) {
$object = $ret[1];
$action = $ret[2];
}
}
if (isset($object) && isset($action)) {
try {
$class_file = CLASSES_PATH . "/{$object}.php";
if (file_exists($class_file)) {
require_once $class_file;
$class = new ReflectionClass($object);
if ($class->isInstantiable()) {
return array('class' => $class->newInstance(), 'method' => new ReflectionMethod($object, $action));
}
}
} catch (Exception $e) {
}
}
return null;
}
示例11: getCallableMethods
/**
* get callable methods within the classObj
*
* @param string|object $classObj
* @param int $filter
* @param bool $filterOwn
* @return array array of \ReflectionMethod
*/
public function getCallableMethods($classObj, $filter, $filterOwn = true)
{
if (is_string($classObj) && !class_exists($classObj, false)) {
throw new ClassNotDefinedException('Class name: ' . $classObj . ' not defined yet!');
}
$className = $classObj;
if (is_object($classObj)) {
$className = get_class($classObj);
}
$reflectClass = new \ReflectionClass($classObj);
$methods = array();
if (!$reflectClass->isUserDefined() || !$reflectClass->isInstantiable()) {
return $methods;
}
$methods = $reflectClass->getMethods($filter);
if ($filterOwn) {
$needNeaten = false;
foreach ($methods as $k => $method) {
if ($method->class != $className) {
unset($methods[$k]);
!$needNeaten && ($needNeaten = true);
}
}
$needNeaten && ($methods = array_values($methods));
}
return $methods;
}
示例12: loadInformation
/**
* @param object $entity
*
* @return MetaInformation
*
* @throws \RuntimeException if no declaration for document found in $entity
*/
public function loadInformation($entity)
{
$className = $this->getClass($entity);
if (!is_object($entity)) {
$reflectionClass = new \ReflectionClass($className);
if (!$reflectionClass->isInstantiable()) {
throw new \RuntimeException(sprintf('cannot instantiate entity %s', $className));
}
$entity = $reflectionClass->newInstanceWithoutConstructor();
}
if (!$this->annotationReader->hasDocumentDeclaration($entity)) {
throw new \RuntimeException(sprintf('no declaration for document found in entity %s', $className));
}
$metaInformation = new MetaInformation();
$metaInformation->setEntity($entity);
$metaInformation->setClassName($className);
$metaInformation->setDocumentName($this->getDocumentName($className));
$metaInformation->setFieldMapping($this->annotationReader->getFieldMapping($entity));
$metaInformation->setFields($this->annotationReader->getFields($entity));
$metaInformation->setRepository($this->annotationReader->getRepository($entity));
$metaInformation->setIdentifier($this->annotationReader->getIdentifier($entity));
$metaInformation->setBoost($this->annotationReader->getEntityBoost($entity));
$metaInformation->setSynchronizationCallback($this->annotationReader->getSynchronizationCallback($entity));
$metaInformation->setIndex($this->annotationReader->getDocumentIndex($entity));
$metaInformation->setIsDoctrineEntity($this->annotationReader->isDoctrineEntity($entity));
return $metaInformation;
}
示例13: loadResourceMap
/**
* Load the resource map
*
* @return $this
*/
public function loadResourceMap()
{
$dirIterator = new \RecursiveDirectoryIterator($this->path->getDir('vendor/devvoh/parable/src'), \RecursiveDirectoryIterator::SKIP_DOTS);
$iteratorIterator = new \RecursiveIteratorIterator($dirIterator);
foreach ($iteratorIterator as $path => $file) {
/** @var \SplFileInfo $file */
/*
* Specifically exclude all non-php files and Bootstrap, since it will attempt to register everything again
* and isn't a class anyway.
*/
if ($file->getFilename() === 'Bootstrap.php' || $file->getFilename() === 'parable.php' || $file->getExtension() !== 'php' || strpos($file->getRealPath(), '/Cli/') !== false) {
continue;
}
$className = str_replace('.' . $file->getExtension(), '', $file->getFilename());
$fullClassName = str_replace($this->path->getDir('vendor/devvoh/parable/src'), '', $file->getRealPath());
$fullClassName = str_replace('.' . $file->getExtension(), '', $fullClassName);
$fullClassName = str_replace('/', '\\', 'Parable' . $fullClassName);
$reflectionClass = new \ReflectionClass($fullClassName);
if (!$reflectionClass->isInstantiable()) {
continue;
}
$this->resourceMap[$className] = $fullClassName;
}
return $this;
}
示例14: variants
/**
* Returns an array of variants.
*
* With no arguments, returns all variants
*
* With a classname as the first argument, returns the variants that apply to that class
* (optionally including subclasses)
*
* @static
* @param string $class - The class name to get variants for
* @param bool $includeSubclasses - True if variants should be included if they apply to at least one subclass of $class
* @return array - An array of (string)$variantClassName => (Object)$variantInstance pairs
*/
public static function variants($class = null, $includeSubclasses = true)
{
if (!$class) {
if (self::$variants === null) {
$classes = ClassInfo::subclassesFor('SearchVariant');
$concrete = array();
foreach ($classes as $variantclass) {
$ref = new ReflectionClass($variantclass);
if ($ref->isInstantiable()) {
$variant = singleton($variantclass);
if ($variant->appliesToEnvironment()) {
$concrete[$variantclass] = $variant;
}
}
}
self::$variants = $concrete;
}
return self::$variants;
} else {
$key = $class . '!' . $includeSubclasses;
if (!isset(self::$class_variants[$key])) {
self::$class_variants[$key] = array();
foreach (self::variants() as $variantclass => $instance) {
if ($instance->appliesTo($class, $includeSubclasses)) {
self::$class_variants[$key][$variantclass] = $instance;
}
}
}
return self::$class_variants[$key];
}
}
示例15: test__construct
public function test__construct()
{
$metadata = new ClassMetadata(__NAMESPACE__ . '\\ModelTest');
$metadata->addModel('entity', __NAMESPACE__ . '\\EntityTest');
$metadata->setIdentifier('entity', 'id');
$metadataFactory = $this->getMock('Pok\\PoolDBM\\Mapping\\ClassMetadataFactory', array('getMetadataFor', 'setModelManager'));
$metadataFactory->expects($this->any())->method('getMetadataFor')->will($this->returnValue($metadata));
$refl = new \ReflectionClass('Pok\\PoolDBM\\Manager\\BaseManager');
$this->assertTrue($refl->isInstantiable());
$pool = new Pool();
$pool->addManager('entity', new EntityManager());
$manager = new TestManager(__NAMESPACE__ . '\\ModelTest', new ModelManager($pool, $metadataFactory));
$this->assertInstanceOf('Pok\\PoolDBM\\ModelRepository', $manager->getRepository($manager));
$this->assertInstanceOf(__NAMESPACE__ . '\\ModelTest', $manager->create());
$manager->save(new ModelTest());
$manager->save(new ModelTest(), true);
try {
$manager->save(new \stdClass());
} catch (\RuntimeException $e) {
$this->assertEquals('Manager "Pok\\PoolDBM\\Tests\\Manager\\TestManager" is unable to save model "stdClass"', $e->getMessage());
}
$manager->clear();
$this->assertInstanceOf(__NAMESPACE__ . '\\ModelTest', $manager->find(null));
$this->assertEquals(1, count($manager->findBy(array())));
$this->assertInstanceOf(__NAMESPACE__ . '\\ModelTest', $manager->findOneBy(array()));
$this->assertEquals(1, count($manager->findAll()));
}