catchAdmin/extend/catcher/library/ParseClass.php

145 lines
2.8 KiB
PHP
Raw Normal View History

2020-06-05 15:20:22 +08:00
<?php
2020-06-05 15:35:51 +08:00
namespace catcher\library;
2020-06-05 16:20:50 +08:00
2020-09-06 10:58:13 +08:00
use think\exception\ClassNotFoundException;
2020-06-05 15:35:51 +08:00
class ParseClass
{
protected $namespace;
protected $module;
protected $controller;
2020-06-06 09:21:24 +08:00
/**
* 获取父类方法
*
* @return array
2020-08-10 11:16:42 +08:00
* @throws \ReflectionException
2020-06-06 09:21:24 +08:00
*/
2020-06-05 15:35:51 +08:00
public function parentMethods()
{
$class = $this->getClass();
$parent = $class->getParentClass();
$methods = [];
foreach ($parent->getMethods() as $method) {
if (!$this->isMagicMethod($method->getName())) {
$methods[] = $method->getName();
}
}
return $methods;
}
2020-06-06 09:21:24 +08:00
/**
* 获取所有方法
*
* @return array
2020-08-10 11:16:42 +08:00
* @throws \ReflectionException
2020-06-06 09:21:24 +08:00
*/
2020-06-05 15:35:51 +08:00
public function methods()
{
$class = $this->getClass();
$methods = [];
foreach ($class->getMethods() as $method) {
if (!$this->isMagicMethod($method->getName())) {
$methods[] = $method->getName();
}
}
return $methods;
}
/**
* @return mixed
2020-08-10 11:16:42 +08:00
* @throws \ReflectionException
2020-06-05 15:35:51 +08:00
*/
public function onlySelfMethods()
{
$methods = [];
$parentMethods = $this->parentMethods();
foreach ($this->methods() as $method) {
if (!in_array($method, $parentMethods)) {
$methods[] = $method;
}
}
return $methods;
}
2020-09-06 10:58:13 +08:00
2020-06-06 09:21:24 +08:00
/**
2020-09-06 10:58:13 +08:00
* 获取class
2020-06-06 09:21:24 +08:00
*
2020-09-06 10:58:13 +08:00
* @time 2020年09月06日
2020-06-06 09:21:24 +08:00
* @throws \ReflectionException
2020-09-06 10:58:13 +08:00
* @return \ReflectionClass
2020-06-06 09:21:24 +08:00
*/
2020-06-05 15:35:51 +08:00
public function getClass()
{
2020-09-06 10:58:13 +08:00
$class = $this->namespace . $this->module . '\\controller\\'. ucfirst($this->controller);
2020-06-05 15:35:51 +08:00
2020-09-06 10:58:13 +08:00
if (class_exists($class)) {
return new \ReflectionClass($class);
}
2020-06-05 15:35:51 +08:00
2020-09-06 10:58:13 +08:00
throw new ClassNotFoundException($this->controller . ' not found');
2020-06-05 15:35:51 +08:00
}
2020-06-06 09:21:24 +08:00
/**
* @param $method
* @return bool
*/
2020-06-05 15:35:51 +08:00
protected function isMagicMethod($method)
{
return strpos($method, '__') !== false;
}
2020-06-06 09:21:24 +08:00
/**
*
* @param $module
* @return $this
* @author JaguarJack <njphper@gmail.com>
* @date 2020/6/6
*/
2020-06-05 15:35:51 +08:00
public function setModule($module)
{
2020-08-10 11:16:42 +08:00
$psr4 = (new Composer())->psr4Autoload();
2020-06-05 15:35:51 +08:00
foreach ($psr4 as $key => $_module) {
if ($_module == $module) {
$this->namespace = $key;
break;
}
}
return $this;
}
2020-06-06 09:21:24 +08:00
/**
*
* @param $module
* @param $controller
* @return $this
* @author JaguarJack <njphper@gmail.com>
* @date 2020/6/6
*/
2020-06-05 15:35:51 +08:00
public function setRule($module, $controller)
{
$this->module = $module;
$this->controller = $controller;
return $this;
}
}