designModeDemo/bin/Base.php

137 lines
3.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace bin;
use ReflectionClass;
/**
* 基础类
*/
class Base
{
/**
* @var string 源目录名
*/
protected string $dir = 'demo';
/**
* @var array 命名空间列表
*/
protected array $dirFiles = [];
/**
* @var string|mixed 获取url参数
*/
protected string $queryString = '';
/**
* 初始化入口
*/
public function __construct()
{
if ($this->queryString = $_SERVER['QUERY_STRING']) {
self::init();
} else {
self::setDirFiles();
self::outDirFiles();
}
}
/**
* 通过命名空间调用对应类方法
*
* @return void
*/
public function init()
{
try {
// 初始化类,再执行对应方法
$class = $this->newClass($this->queryString);
$designName = $class->designName;
echo "<pre class='sf-dump' style='font-size: large;background-color: transparent;padding: 30px 0px;'>$designName<a href='/' style='margin-left:20px;'><span style='color:green'>返回首页</span></a></pre>";
$class->main();
} catch (\ReflectionException $e) {
var_dump($e->getMessage());
}
}
/**
* 输出可访问的命名空间路径到页面
*
* @return void
*/
public function outDirFiles()
{
$li = '';
foreach ($this->dirFiles as $file) {
$designUrl = '';
$newClass = $this->newClass($file);
try {
if (is_object($newClass)) {
$designName = $newClass->designName;
$designUrl = $newClass->designRefUrl;
} else {
$designName = $newClass;
}
} catch (\Exception $e) {
$designName = $e->getMessage();
}
$nameUrl = $designUrl === '' ? $designName : "<a href='$designUrl' target='_blank'>$designName</a>";
$li .= "<li>$nameUrl <a href='index.php?$file'>$file</a></li>";
}
echo "<div><ul>$li</ul></div>";
}
/**
* 根据命名空间获取对应对象
*
* @param $namespace
* @return object|string
*/
protected function newClass($namespace) : object|string
{
try {
// 先获取类信息
$reflect = new ReflectionClass($namespace);
// 初始化类,再执行对应方法
return $reflect->newInstanceArgs();
} catch (\ReflectionException $e) {
return $e->getMessage();
}
}
/**
* 设置demo所有可用文件
*
* @return void
*/
protected function setDirFiles()
{
// $dir = ROOTDIR . DIRECTORY_SEPARATOR . $this->dir;
$this->scandir($this->dir);
}
/**
* 循环目录
*
* @param $dir
* @return void
*/
protected function scandir($dir) : void
{
$dirFiles = scandir($dir);
$oldDir = $dir . DIRECTORY_SEPARATOR;
foreach ($dirFiles as $file) {
if ($file === '.' || $file === '..') continue;
if (is_dir($newsDir = $oldDir . $file)) {
$this->scandir($newsDir);
} elseif ($file === 'Run.php') {
// 这样可以屏蔽非入口
$this->dirFiles[] = $oldDir . 'Run';
}
}
}
}