public function testOnlyClass() { $classes = ['User' => [null, 'User'], 'Post' => [null, 'Post'], 'Hello_World' => [null, 'Hello_World']]; foreach ($classes as $class => $result) { $this->assertEquals($result, parse_class($class)); } }
public function buildClass(string $class) { $tpl = __DIR__ . '/Templates/Controller.tp'; $vars = []; list($vars['namespace'], $vars['class']) = parse_class($class); if (!empty($vars['namespace'])) { $vars['namespace'] = "namespace {$vars['namespace']};"; } if (empty($this->extend)) { $vars['extend'] = 'Controller'; $vars['use'] = 'use Ke\\Web\\Controller;'; } else { $vars['extend'] = $this->extend; } return substitute(file_get_contents($tpl), $vars); }
function parse_args($args) { $result = []; foreach ($args as $key => $item) { switch (true) { case is_object($item): $value = sprintf('<em>object</em>(%s)', parse_class(get_class($item))); break; case is_array($item): if (count($item) > 3) { $value = sprintf('[%s, ...]', parse_args(array_slice($item, 0, 3))); } else { $value = sprintf('[%s]', parse_args($item)); } break; case is_string($item): if (strlen($item) > 20) { $value = sprintf('\'<a class="toggle" title="%s">%s...</a>\'', htmlentities($item), htmlentities(substr($item, 0, 20))); } else { $value = sprintf("'%s'", htmlentities($item)); } break; case is_int($item): case is_float($item): $value = $item; break; case is_null($item): $value = '<em>null</em>'; break; case is_bool($item): $value = '<em>' . ($item ? 'true' : 'false') . '</em>'; break; case is_resource($item): $value = '<em>resource</em>'; break; default: $value = htmlentities(str_replace("\n", '', var_export(strval($item), true))); break; } $result[] = is_int($key) ? $value : "'{$key}' => {$value}"; } return implode(', ', $result); }
public function buildModel(string $table, string $class, string $path) { list($namespace, $pureClass) = parse_class($class); $dir = dirname($path); $tpl = $this->getTplContent(); $forge = $this->adapter->getForge(); $vars = $forge->buildTableProps($table); $vars['namespace'] = $namespace; $vars['class'] = $pureClass; $vars['tableName'] = $table; $vars['datetime'] = date('Y-m-d H:i:s'); if (is_file($path)) { return ['Fail', PHP_EOL, "File {$path} is existing!"]; } if (!is_dir($dir)) { mkdir($dir, 0755, true); } if (file_put_contents($path, substitute($tpl, $vars))) { return ['Success']; } else { return ['Fail', PHP_EOL, 'I/O error, please try again!']; } }
public final function __construct(string $root = null, array $dirs = null) { if (isset(self::$app)) { throw new PhpException('Create App repeated instances!'); } self::$app = $this; // 检查根目录 if (($this->root = real_dir($root)) === false) { throw new PhpException('App directory (root) does not exist or is not a directory!'); } /** App的根目录的绝对路径 */ define('KE_APP_ROOT', $this->root); /** App的目录名 */ define('KE_APP_DIR', basename($this->root)); // 后注册,让后继承的App类,可以以声明属性的方式来添加 if (!isset($this->aliases['web'])) { $this->aliases['web'] = 'public'; } /** @var string $kephp kephp的根目录 */ $this->dirs['kephp'] = dirname(__DIR__); if (!empty($dirs)) { $this->setDirs($dirs); } // CLI模式加载特定的环境配置文件 if (KE_APP_MODE === KE_CLI) { // 先尝试加载环境配置文件,这个文件以后会扩展成为json格式,以装载更多的信息 $envFile = $this->root . '/env'; if (is_file($envFile) && is_readable($envFile)) { $_SERVER['SERVER_NAME'] = trim(file_get_contents($envFile)); } else { $_SERVER['SERVER_NAME'] = 'localhost'; } } // 绑定servers $this->servers += self::$knownServers; // 匹配当前的运行环境 $env = $this->detectEnv(); // 不是开发模式或者测试模式,就必然是发布模式,确保在未知的模式下,返回发布模式 if ($env !== KE_DEV && $env !== KE_TEST) { $env = KE_PRO; } /** App当前的运行环境 */ define('KE_APP_ENV', $env); /** @var string $appSrc App的src目录 */ $appSrc = $this->path('src'); // 项目的基础的类、命名空间和命名空间对应的路径 $appClass = static::class; $appNs = null; $appNsPath = $appSrc; if ($appClass !== __CLASS__) { list($appNs) = parse_class($appClass); if (!empty($appNs)) { $appNsPath .= DS . $appNs; } if (!KE_IS_WIN) { $appNsPath = str_replace('\\', '/', $appNsPath); } } /** 记录下全局的App的类名称 */ define('KE_APP_CLASS', $appClass); /** 当前的App类名的namespace */ define('KE_APP_NS', $appNs); /** 当前App类的Namespace指向的绝对路径 */ define('KE_APP_NS_PATH', $appNsPath); $this->dirs['appNs'] = $appNsPath; $this->loader = new Loader(['dirs' => ['appSrc' => [$appSrc, 100, Loader::CLS], 'appHelper' => ["{$appNsPath}/Helper", 100, Loader::HELPER], 'keHelper' => ["{$this->dirs['kephp']}/Ke/Helper", 1000, Loader::HELPER]], 'classes' => import("{$this->dirs['kephp']}/classes.php"), 'prepend' => true]); $this->loader->start(); if (!empty($this->helpers)) { $this->loader->loadHelper(...$this->helpers); } // Uri准备 Uri::prepare(); $this->onConstruct($this->loader); }
public static function mkTableName(string $source = null, $model, $tableName = null, $prefix = null) { if (empty($source)) { $source = self::DEFAULT_SOURCE; } if (!isset(self::$sources[$source])) { throw new Exception("DB[{$source}]: undefined database config!"); } if (strpos($tableName, '.') > 0) { return $tableName; } $config = self::$sources[$source]; if (empty($tableName)) { list(, $class) = parse_class($model); $tableName = strtolower($class); } if (empty($prefix)) { $prefix = empty($config['prefix']) ? '' : trim($config['prefix'], '_'); } else { $prefix = trim($config['prefix'], '_'); } if (!empty($prefix)) { $tableName = $prefix . '_' . $tableName; } return $tableName; }
protected function createClass() { $class = $this->prepareClasses[$this->selected]; $path = $this->preparePaths[$this->selected]; $cmd = $this->prepareCommands[$this->selected]; $tplFile = $this->isRef ? 'ReflectionCommand.tp' : 'Command.tp'; $tplPath = __DIR__ . '/Templates/' . $tplFile; if (is_file($path)) { return ['Lost!', "The file \"{$path}\" is exists!"]; } if (!is_file($tplPath)) { return ['Lost!', "The template file \"{$tplFile}\" cannot found!"]; } $dir = dirname($path); if (!is_dir($dir)) { mkdir($dir, 0755, true); } $tplContent = file_get_contents($tplPath); list($ns, $cls) = parse_class($class); $vars = ['class' => $cls, 'command' => $this->name, 'namespace' => $ns, 'datetime' => date('Y-m-d H:i')]; if (file_put_contents($path, substitute($tplContent, $vars))) { return ['Success!', 'Please type: "php ' . KE_SCRIPT_FILE . ' ' . $cmd . ' --help"']; } return ['Lost!', "{$path} save error!"]; }