Commit 1b5f7412 authored by wolfcode's avatar wolfcode

Webman-framework v1.4.9

parent ced1e593
......@@ -25,7 +25,7 @@
},
"require": {
"php": ">=7.2",
"workerman/webman-framework": "1.4.3",
"workerman/webman-framework": "^1.4",
"monolog/monolog": "^2.0",
"webman/auto-route": "^1.0",
"webman/think-orm": "1.0.2",
......@@ -41,7 +41,7 @@
"aliyuncs/oss-sdk-php": "^2.4",
"qcloud/cos-sdk-v5": "^2.5",
"jasongrimes/paginator": "^1.0",
"webman/console": "1.0.27"
"webman/console": "^1.2"
},
"suggest": {
"ext-event": "For better performance. "
......
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "1e7532d2b14481835ed4cafcfb0c520b",
"content-hash": "42b09044b63aa27ff1c20f65be9f5f09",
"packages": [
{
"name": "aliyuncs/oss-sdk-php",
......@@ -3636,19 +3636,20 @@
},
{
"name": "webman/console",
"version": "v1.0.27",
"version": "v1.2.16",
"source": {
"type": "git",
"url": "https://github.com/webman-php/console.git",
"reference": "e450967eaabc43eb0c93cfcd6d8f420c16e22b67"
"reference": "252868f948fe397e386577fa6ce0e7487ffc98ba"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/webman-php/console/zipball/e450967eaabc43eb0c93cfcd6d8f420c16e22b67",
"reference": "e450967eaabc43eb0c93cfcd6d8f420c16e22b67",
"url": "https://api.github.com/repos/webman-php/console/zipball/252868f948fe397e386577fa6ce0e7487ffc98ba",
"reference": "252868f948fe397e386577fa6ce0e7487ffc98ba",
"shasum": ""
},
"require": {
"doctrine/inflector": "^2.0",
"symfony/console": ">=5.0"
},
"require-dev": {
......@@ -3684,7 +3685,7 @@
"source": "https://github.com/webman-php/console",
"wiki": "http://www.workerman.net/doc/webman"
},
"time": "2022-07-01T08:59:02+00:00"
"time": "2022-10-08T13:46:48+00:00"
},
{
"name": "webman/think-cache",
......@@ -3823,16 +3824,16 @@
},
{
"name": "workerman/webman-framework",
"version": "v1.4.3",
"version": "v1.4.9",
"source": {
"type": "git",
"url": "https://github.com/walkor/webman-framework.git",
"reference": "0f4d5b6c58823656bdc9603f762d4be6e41ae380"
"reference": "faf13663be7cad1608217a0ec2e3cb8913f866dc"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/walkor/webman-framework/zipball/0f4d5b6c58823656bdc9603f762d4be6e41ae380",
"reference": "0f4d5b6c58823656bdc9603f762d4be6e41ae380",
"url": "https://api.github.com/repos/walkor/webman-framework/zipball/faf13663be7cad1608217a0ec2e3cb8913f866dc",
"reference": "faf13663be7cad1608217a0ec2e3cb8913f866dc",
"shasum": ""
},
"require": {
......@@ -3863,34 +3864,24 @@
{
"name": "walkor",
"email": "walkor@workerman.net",
"homepage": "http://www.workerman.net",
"homepage": "https://www.workerman.net",
"role": "Developer"
}
],
"description": "High performance HTTP Service Framework.",
"homepage": "http://www.workerman.net",
"homepage": "https://www.workerman.net",
"keywords": [
"High Performance",
"http service"
],
"support": {
"email": "walkor@workerman.net",
"forum": "http://wenda.workerman.net/",
"forum": "https://wenda.workerman.net/",
"issues": "https://github.com/walkor/webman/issues",
"source": "https://github.com/walkor/webman-framework",
"wiki": "http://doc.workerman.net/"
"wiki": "https://doc.workerman.net/"
},
"funding": [
{
"url": "https://opencollective.com/walkor",
"type": "open_collective"
},
{
"url": "https://www.patreon.com/walkor",
"type": "patreon"
}
],
"time": "2022-08-15T12:35:14+00:00"
"time": "2022-10-20T06:42:33+00:00"
},
{
"name": "workerman/workerman",
......
#!/usr/bin/env php
<?php
require_once __DIR__ . '/vendor/autoload.php';
Support\App::run();
support\App::run();
......@@ -22,10 +22,6 @@ use Webman\Util;
$worker = $worker ?? null;
if ($timezone = config('app.default_timezone')) {
date_default_timezone_set($timezone);
}
set_error_handler(function ($level, $message, $file = '', $line = 0) {
if (error_reporting() & $level) {
throw new ErrorException($message, 0, $level, $file, $line);
......@@ -48,7 +44,11 @@ if (class_exists('Dotenv\Dotenv') && file_exists(base_path() . '/.env')) {
}
}
Support\App::loadAllConfig(['route']);
Config::clear();
support\App::loadAllConfig(['route']);
if ($timezone = config('app.default_timezone')) {
date_default_timezone_set($timezone);
}
foreach (config('autoload.files', []) as $file) {
include_once $file;
......
<?php
/**
* This file is part of webman.
*
......@@ -12,10 +13,10 @@
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use support\Container;
use support\Request;
use support\Response;
use support\Translation;
use support\Container;
use support\view\Raw;
use support\view\Blade;
use support\view\ThinkPHP;
......@@ -25,68 +26,95 @@ use Webman\App;
use Webman\Config;
use Webman\Route;
// Phar support.
if (\is_phar()) {
\define('BASE_PATH', dirname(__DIR__));
} else {
\define('BASE_PATH', realpath(__DIR__ . '/../'));
// Webman version
define('WEBMAN_VERSION', '1.4');
// Project base path
define('BASE_PATH', dirname(__DIR__));
/**
* Generate paths based on given information
* @param string $front
* @param string $back
* @return string
*/
function path_combine(string $front, string $back)
{
return $front . ($back ? (DIRECTORY_SEPARATOR . ltrim($back, DIRECTORY_SEPARATOR)) : $back);
}
\define('WEBMAN_VERSION', '1.4');
/**
* @param $return_phar
* return the program execute directory
* @param string $path
* @return string
*/
function run_path(string $path = '')
{
static $run_path = '';
if (!$run_path) {
$run_path = \is_phar() ? \dirname(\Phar::running(false)) : BASE_PATH;
}
return \path_combine($run_path, $path);
}
/**
* if the param $path equal false,will return this program current execute directory
* @param string|false $path
* @return false|string
*/
function base_path(bool $return_phar = true)
function base_path($path = '')
{
static $real_path = '';
if (!$real_path) {
$real_path = \is_phar() ? \dirname(Phar::running(false)) : BASE_PATH;
if (false === $path) {
return \run_path();
}
return $return_phar ? BASE_PATH : $real_path;
return \path_combine(BASE_PATH, $path);
}
/**
* @param string $path
* @return string
*/
function app_path()
function app_path(string $path = '')
{
return BASE_PATH . DIRECTORY_SEPARATOR . 'app';
return \path_combine(BASE_PATH . DIRECTORY_SEPARATOR . 'app', $path);
}
/**
* @param string $path
* @return string
*/
function public_path()
function public_path(string $path = '')
{
static $path = '';
if (!$path) {
$path = \config('app.public_path', BASE_PATH . DIRECTORY_SEPARATOR . 'public');
static $public_path = '';
if (!$public_path) {
$public_path = \config('app.public_path') ? : \run_path('public');
}
return $path;
return \path_combine($public_path, $path);
}
/**
* @param string $path
* @return string
*/
function config_path()
function config_path(string $path = '')
{
return BASE_PATH . DIRECTORY_SEPARATOR . 'config';
return \path_combine(BASE_PATH . DIRECTORY_SEPARATOR . 'config', $path);
}
/**
* Phar support.
* Compatible with the 'realpath' function in the phar file.
*
* @param string $path
* @return string
*/
function runtime_path()
function runtime_path(string $path = '')
{
static $path = '';
if (!$path) {
$path = \config('app.runtime_path', BASE_PATH . DIRECTORY_SEPARATOR . 'runtime');
static $runtime_path = '';
if (!$runtime_path) {
$runtime_path = \config('app.runtime_path') ? : \run_path('runtime');
}
return $path;
return \path_combine($runtime_path, $path);
}
/**
......@@ -210,7 +238,7 @@ function twig_view(string $template, array $vars = [], string $app = null)
}
/**
* @return Request
* @return \Webman\Http\Request|Request|null
*/
function request()
{
......@@ -379,7 +407,7 @@ function worker_bind($worker, $class)
}
}
if (\method_exists($class, 'onWorkerStart')) {
[$class, 'onWorkerStart']($worker);
\call_user_func([$class, 'onWorkerStart'], $worker);
}
}
......@@ -433,7 +461,6 @@ function worker_start($process_name, $config)
$instance = Container::make($config['handler'], $config['constructor'] ?? []);
\worker_bind($worker, $instance);
}
};
}
......
......@@ -3792,26 +3792,27 @@
},
{
"name": "webman/console",
"version": "v1.0.27",
"version_normalized": "1.0.27.0",
"version": "v1.2.16",
"version_normalized": "1.2.16.0",
"source": {
"type": "git",
"url": "https://github.com/webman-php/console.git",
"reference": "e450967eaabc43eb0c93cfcd6d8f420c16e22b67"
"reference": "252868f948fe397e386577fa6ce0e7487ffc98ba"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/webman-php/console/zipball/e450967eaabc43eb0c93cfcd6d8f420c16e22b67",
"reference": "e450967eaabc43eb0c93cfcd6d8f420c16e22b67",
"url": "https://api.github.com/repos/webman-php/console/zipball/252868f948fe397e386577fa6ce0e7487ffc98ba",
"reference": "252868f948fe397e386577fa6ce0e7487ffc98ba",
"shasum": ""
},
"require": {
"doctrine/inflector": "^2.0",
"symfony/console": ">=5.0"
},
"require-dev": {
"workerman/webman": "^1.0"
},
"time": "2022-07-01T08:59:02+00:00",
"time": "2022-10-08T13:46:48+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
......@@ -3991,17 +3992,17 @@
},
{
"name": "workerman/webman-framework",
"version": "v1.4.3",
"version_normalized": "1.4.3.0",
"version": "v1.4.9",
"version_normalized": "1.4.9.0",
"source": {
"type": "git",
"url": "https://github.com/walkor/webman-framework.git",
"reference": "0f4d5b6c58823656bdc9603f762d4be6e41ae380"
"reference": "faf13663be7cad1608217a0ec2e3cb8913f866dc"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/walkor/webman-framework/zipball/0f4d5b6c58823656bdc9603f762d4be6e41ae380",
"reference": "0f4d5b6c58823656bdc9603f762d4be6e41ae380",
"url": "https://api.github.com/repos/walkor/webman-framework/zipball/faf13663be7cad1608217a0ec2e3cb8913f866dc",
"reference": "faf13663be7cad1608217a0ec2e3cb8913f866dc",
"shasum": ""
},
"require": {
......@@ -4013,7 +4014,7 @@
"suggest": {
"ext-event": "For better performance. "
},
"time": "2022-08-15T12:35:14+00:00",
"time": "2022-10-20T06:42:33+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
......@@ -4034,33 +4035,23 @@
{
"name": "walkor",
"email": "walkor@workerman.net",
"homepage": "http://www.workerman.net",
"homepage": "https://www.workerman.net",
"role": "Developer"
}
],
"description": "High performance HTTP Service Framework.",
"homepage": "http://www.workerman.net",
"homepage": "https://www.workerman.net",
"keywords": [
"High Performance",
"http service"
],
"support": {
"email": "walkor@workerman.net",
"forum": "http://wenda.workerman.net/",
"forum": "https://wenda.workerman.net/",
"issues": "https://github.com/walkor/webman/issues",
"source": "https://github.com/walkor/webman-framework",
"wiki": "http://doc.workerman.net/"
"wiki": "https://doc.workerman.net/"
},
"funding": [
{
"url": "https://opencollective.com/walkor",
"type": "open_collective"
},
{
"url": "https://www.patreon.com/walkor",
"type": "patreon"
}
],
"install-path": "../workerman/webman-framework"
},
{
......
<?php return array(
'root' => array(
'name' => 'workerman/webman',
'pretty_version' => '1.0.0+no-version-set',
'version' => '1.0.0.0',
'reference' => NULL,
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => 'ced1e593895e2164fa4d567e8d4685f231f65ae0',
'type' => 'project',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
......@@ -534,9 +534,9 @@
'dev_requirement' => false,
),
'webman/console' => array(
'pretty_version' => 'v1.0.27',
'version' => '1.0.27.0',
'reference' => 'e450967eaabc43eb0c93cfcd6d8f420c16e22b67',
'pretty_version' => 'v1.2.16',
'version' => '1.2.16.0',
'reference' => '252868f948fe397e386577fa6ce0e7487ffc98ba',
'type' => 'library',
'install_path' => __DIR__ . '/../webman/console',
'aliases' => array(),
......@@ -570,18 +570,18 @@
'dev_requirement' => false,
),
'workerman/webman' => array(
'pretty_version' => '1.0.0+no-version-set',
'version' => '1.0.0.0',
'reference' => NULL,
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => 'ced1e593895e2164fa4d567e8d4685f231f65ae0',
'type' => 'project',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'dev_requirement' => false,
),
'workerman/webman-framework' => array(
'pretty_version' => 'v1.4.3',
'version' => '1.4.3.0',
'reference' => '0f4d5b6c58823656bdc9603f762d4be6e41ae380',
'pretty_version' => 'v1.4.9',
'version' => '1.4.9.0',
'reference' => 'faf13663be7cad1608217a0ec2e3cb8913f866dc',
'type' => 'library',
'install_path' => __DIR__ . '/../workerman/webman-framework',
'aliases' => array(),
......
......@@ -23,7 +23,8 @@
"source": "https://github.com/webman-php/console"
},
"require": {
"symfony/console": ">=5.0"
"symfony/console": ">=5.0",
"doctrine/inflector": "^2.0"
},
"autoload": {
"psr-4": {
......
......@@ -17,11 +17,20 @@ class Command extends Application
$dir_iterator = new \RecursiveDirectoryIterator($path);
$iterator = new \RecursiveIteratorIterator($dir_iterator);
foreach ($iterator as $file) {
if (is_dir($file)) {
/** @var \SplFileInfo $file */
if (strpos($file->getFilename(), '.') === 0) {
continue;
}
$class_name = $namspace.'\\'.basename($file, '.php');
if (!is_a($class_name, Commands::class, true)) {
if ($file->getExtension() !== 'php') {
continue;
}
// abc\def.php
$relativePath = str_replace(str_replace('/', '\\', $path . '\\'), '', str_replace('/', '\\', $file->getRealPath()));
// app\command\abc
$realNamespace = trim($namspace . '\\' . trim(dirname(str_replace('\\', DIRECTORY_SEPARATOR, $relativePath)), '.'), '\\');
// app\command\doc\def
$class_name = trim($realNamespace . '\\' . $file->getBasename('.php'), '\\');
if (!class_exists($class_name) || !is_a($class_name, Commands::class, true)) {
continue;
}
$this->add(new $class_name);
......
This diff is collapsed.
......@@ -29,7 +29,7 @@ class MakeCommandCommand extends Command
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$command = $name = $input->getArgument('name');
$command = $name = trim($input->getArgument('name'));
$output->writeln("Make command $name");
// make:command 不支持子目录
......@@ -37,12 +37,14 @@ class MakeCommandCommand extends Command
if (!$command_str = Util::guessPath(app_path(), 'command')) {
$command_str = Util::guessPath(app_path(), 'controller') === 'Controller' ? 'Command' : 'command';
}
$upper = $command_str === 'Command';
$name = ucfirst($name);
$items= explode(':', $name);
$name='';
foreach ($items as $item) {
$name.=ucfirst($item);
}
$file = app_path() . "/$command_str/$name.php";
$upper = $command_str === 'Command';
$namespace = $upper ? 'App\Command' : 'app\command';
$this->createCommand($name, $namespace, $file, $command);
return self::SUCCESS;
......
......@@ -2,6 +2,7 @@
namespace Webman\Console\Commands;
use Doctrine\Inflector\InflectorFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
......@@ -21,6 +22,7 @@ class MakeModelCommand extends Command
protected function configure()
{
$this->addArgument('name', InputArgument::REQUIRED, 'Model name');
$this->addArgument('type', InputArgument::OPTIONAL, 'Type');
}
/**
......@@ -32,6 +34,7 @@ class MakeModelCommand extends Command
{
$name = $input->getArgument('name');
$name = Util::nameToClass($name);
$type = $input->getArgument('type');
$output->writeln("Make model $name");
if (!($pos = strrpos($name, '/'))) {
$name = ucfirst($name);
......@@ -58,7 +61,18 @@ class MakeModelCommand extends Command
$file = app_path() . "/$path/$name.php";
$namespace = str_replace('/', '\\', ($upper ? 'App/' : 'app/') . $path);
}
if (!config('database') && config('thinkorm')) {
if (!$type) {
$database = config('database');
if (isset($database['default']) && strpos($database['default'], 'plugin.') === 0) {
$database = false;
}
$thinkorm = config('thinkorm');
if (isset($thinkorm['default']) && strpos($thinkorm['default'], 'plugin.') === 0) {
$thinkorm = false;
}
$type = !$database && $thinkorm ? 'tp' : 'laravel';
}
if ($type == 'tp') {
$this->createTpModel($name, $namespace, $file);
} else {
$this->createModel($name, $namespace, $file);
......@@ -86,8 +100,10 @@ class MakeModelCommand extends Command
try {
$prefix = config('database.connections.mysql.prefix') ?? '';
$database = config('database.connections.mysql.database');
if (\support\Db::select("show tables like '{$prefix}{$table}s'")) {
$table = "{$prefix}{$table}s";
$inflector = InflectorFactory::create()->build();
$table_plura = $inflector->pluralize($inflector->tableize($class));
if (\support\Db::select("show tables like '{$prefix}{$table_plura}'")) {
$table = "{$prefix}{$table_plura}";
} else if (\support\Db::select("show tables like '{$prefix}{$table}'")) {
$table_val = "'$table'";
$table = "{$prefix}{$table}";
......
......@@ -20,13 +20,13 @@ class RouteListCommand extends Command
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$headers = ['uri', 'method', 'callback', 'middleware'];
$headers = ['uri', 'method', 'callback', 'middleware', 'name'];
$rows = [];
foreach (Route::getRoutes() as $route) {
foreach ($route->getMethods() as $method) {
$cb = $route->getCallback();
$cb = $cb instanceof \Closure ? 'Closure' : (is_array($cb) ? json_encode($cb) : var_export($cb, 1));
$rows[] = [$route->getPath(), $method, $cb, json_encode($route->getMiddleware() ?: null)];
$rows[] = [$route->getPath(), $method, $cb, json_encode($route->getMiddleware() ?: null), $route->getName()];
}
}
......
<?php
namespace Webman\Console;
use Doctrine\Inflector\InflectorFactory;
class Util
{
public static function nameToNamespace($name)
......
# These are supported funding model platforms
open_collective: walkor
patreon: walkor
......@@ -5,22 +5,22 @@
"high performance",
"http service"
],
"homepage": "http://www.workerman.net",
"homepage": "https://www.workerman.net",
"license": "MIT",
"description": "High performance HTTP Service Framework.",
"authors": [
{
"name": "walkor",
"email": "walkor@workerman.net",
"homepage": "http://www.workerman.net",
"homepage": "https://www.workerman.net",
"role": "Developer"
}
],
"support": {
"email": "walkor@workerman.net",
"issues": "https://github.com/walkor/webman/issues",
"forum": "http://wenda.workerman.net/",
"wiki": "http://doc.workerman.net/",
"forum": "https://wenda.workerman.net/",
"wiki": "https://doc.workerman.net/",
"source": "https://github.com/walkor/webman-framework"
},
"require": {
......
......@@ -73,6 +73,14 @@ class Config
static::load($config_path, $exclude_file);
}
/**
* @return void
*/
public static function clear()
{
static::$_config = [];
}
/**
* @return void
*/
......@@ -202,18 +210,18 @@ class Config
}
$key_array = \explode('.', $key);
$value = static::$_config;
$finded = true;
$found = true;
foreach ($key_array as $index) {
if (!isset($value[$index])) {
if (static::$_loaded) {
return $default;
}
$finded = false;
$found = false;
break;
}
$value = $value[$index];
}
if ($finded) {
if ($found) {
return $value;
}
return static::read($key, $default);
......
......@@ -16,6 +16,10 @@ class Container implements ContainerInterface
* @var array
*/
protected $_instances = [];
/**
* @var array
*/
protected $_definitions = [];
/**
* @param string $name
......@@ -25,10 +29,14 @@ class Container implements ContainerInterface
public function get(string $name)
{
if (!isset($this->_instances[$name])) {
if (!\class_exists($name)) {
throw new NotFoundException("Class '$name' not found");
if (isset($this->_definitions[$name])) {
$this->_instances[$name] = call_user_func($this->_definitions[$name], $this);
} else {
if (!\class_exists($name)) {
throw new NotFoundException("Class '$name' not found");
}
$this->_instances[$name] = new $name();
}
$this->_instances[$name] = new $name();
}
return $this->_instances[$name];
}
......@@ -39,7 +47,8 @@ class Container implements ContainerInterface
*/
public function has(string $name): bool
{
return \array_key_exists($name, $this->_instances);
return \array_key_exists($name, $this->_instances)
|| array_key_exists($name, $this->_definitions);
}
/**
......@@ -56,4 +65,14 @@ class Container implements ContainerInterface
return new $name(... array_values($constructor));
}
/**
* @param array $definitions
* @return $this
*/
public function addDefinitions(array $definitions)
{
$this->_definitions = array_merge($this->_definitions, $definitions);
return $this;
}
}
......@@ -21,7 +21,7 @@ use Webman\Http\Response;
/**
* Class Handler
* @package Support\Exception
* @package support\exception
*/
class ExceptionHandler implements ExceptionHandlerInterface
{
......
......@@ -197,8 +197,8 @@ class Request extends \Workerman\Protocols\Http\Request
if ($safe_mode && !static::isIntranetIp($remote_ip)) {
return $remote_ip;
}
return $this->header('client-ip', $this->header('x-forwarded-for',
$this->header('x-real-ip', $this->header('x-client-ip',
return $this->header('x-real-ip', $this->header('x-forwarded-for',
$this->header('client-ip', $this->header('x-client-ip',
$this->header('via', $remote_ip)))));
}
......
......@@ -64,7 +64,7 @@ class UploadFile extends File
/**
* @return string
*/
public function getUploadMineType()
public function getUploadMimeType()
{
return $this->_uploadMimeType;
}
......@@ -93,4 +93,12 @@ class UploadFile extends File
return $this->_uploadErrorCode === UPLOAD_ERR_OK;
}
}
\ No newline at end of file
/**
* @deprecated
* @return string
*/
public function getUploadMineType()
{
return $this->_uploadMimeType;
}
}
......@@ -47,8 +47,12 @@ class Install
mkdir($parent_dir, 0777, true);
}
}
copy_dir(__DIR__ . "/$source", base_path() . "/$dest", true);
$source_file = __DIR__ . "/$source";
copy_dir($source_file, base_path() . "/$dest", true);
echo "Create $dest\r\n";
if (is_file($source_file)) {
@unlink($source_file);
}
}
}
......
......@@ -358,7 +358,7 @@ class Route
require_once $route_config_file;
}
if (!is_dir($plugin_config_path = $config_path . '/plugin')) {
return;
continue;
}
$dir_iterator = new \RecursiveDirectoryIterator($plugin_config_path, \FilesystemIterator::FOLLOW_SYMLINKS);
$iterator = new \RecursiveIteratorIterator($dir_iterator);
......
......@@ -96,7 +96,7 @@ class Route
if ($middleware === null) {
return $this->_middlewares;
}
$this->_middlewares = \array_merge($this->_middlewares, (array)$middleware);
$this->_middlewares = \array_merge($this->_middlewares, is_array($middleware) ? $middleware : [$middleware]);
return $this;
}
......
#!/usr/bin/env php
<?php
require_once __DIR__ . '/vendor/autoload.php';
Support\App::run();
......@@ -19,11 +19,11 @@ class App
ini_set('display_errors', 'on');
error_reporting(E_ALL);
if (class_exists(Dotenv::class) && file_exists(base_path() . '/.env')) {
if (class_exists(Dotenv::class) && file_exists(run_path('.env'))) {
if (method_exists(Dotenv::class, 'createUnsafeImmutable')) {
Dotenv::createUnsafeImmutable(base_path())->load();
Dotenv::createUnsafeImmutable(run_path())->load();
} else {
Dotenv::createMutable(base_path())->load();
Dotenv::createMutable(run_path())->load();
}
}
......@@ -97,7 +97,7 @@ class App
require_once \base_path() . '/support/bootstrap.php';
$app = new \Webman\App(config('app.request_class', Request::class), Log::channel('default'), app_path(), public_path());
$worker->onMessage = [$app, 'onMessage'];
[$app, 'onWorkerStart']($worker);
\call_user_func([$app, 'onWorkerStart'], $worker);
};
}
......
......@@ -7,7 +7,7 @@ use Symfony\Component\Cache\Psr16Cache;
/**
* Class Cache
* @package Support\Bootstrap
* @package support\bootstrap
*
* Strings methods
* @method static mixed get($key, $default = null)
......
......@@ -19,7 +19,7 @@ use Webman\Config;
/**
* Class Container
* @package Support
* @package support
* @method static mixed get($name)
* @method static mixed make($name, array $parameters)
* @method static bool has($name)
......
......@@ -18,7 +18,7 @@ use Illuminate\Database\Capsule\Manager;
/**
* Class Db
* @package Support
* @package support
* @method static array select(string $query, $bindings = [], $useReadPdo = true)
* @method static int insert(string $query, $bindings = [])
* @method static int update(string $query, $bindings = [])
......
......@@ -21,7 +21,7 @@ use Monolog\Logger;
/**
* Class Log
* @package Support
* @package support
*
* @method static void log($level, $message, array $context = [])
* @method static void debug($message, array $context = [])
......
......@@ -23,7 +23,7 @@ use Workerman\Worker;
/**
* Class Redis
* @package Support
* @package support
*
* Strings methods
* @method static int append($key, $value)
......
......@@ -16,7 +16,7 @@ namespace support;
/**
* Class Request
* @package Support
* @package support
*/
class Request extends \Webman\Http\Request
{
......
......@@ -16,7 +16,7 @@ namespace support;
/**
* Class Response
* @package Support
* @package support
*/
class Response extends \Webman\Http\Response
{
......
......@@ -19,7 +19,7 @@ use Webman\Exception\NotFoundException;
/**
* Class Translation
* @package Support
* @package support
* @method static string trans(?string $id, array $parameters = [], string $domain = null, string $locale = null)
* @method static void setLocale(string $locale)
* @method static string getLocale()
......
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use Dotenv\Dotenv;
use support\Log;
use Webman\Bootstrap;
use Webman\Config;
use Webman\Route;
use Webman\Middleware;
use Webman\Util;
$worker = $worker ?? null;
if ($timezone = config('app.default_timezone')) {
date_default_timezone_set($timezone);
}
set_error_handler(function ($level, $message, $file = '', $line = 0) {
if (error_reporting() & $level) {
throw new ErrorException($message, 0, $level, $file, $line);
}
});
if ($worker) {
register_shutdown_function(function ($start_time) {
if (time() - $start_time <= 1) {
sleep(1);
}
}, time());
}
if (class_exists('Dotenv\Dotenv') && file_exists(base_path() . '/.env')) {
if (method_exists('Dotenv\Dotenv', 'createUnsafeImmutable')) {
Dotenv::createUnsafeImmutable(base_path())->load();
} else {
Dotenv::createMutable(base_path())->load();
}
}
Support\App::loadAllConfig(['route']);
foreach (config('autoload.files', []) as $file) {
include_once $file;
}
foreach (config('plugin', []) as $firm => $projects) {
foreach ($projects as $name => $project) {
if (!is_array($project)) {
continue;
}
foreach ($project['autoload']['files'] ?? [] as $file) {
include_once $file;
}
}
foreach ($projects['autoload']['files'] ?? [] as $file) {
include_once $file;
}
}
Middleware::load(config('middleware', []), '');
foreach (config('plugin', []) as $firm => $projects) {
foreach ($projects as $name => $project) {
if (!is_array($project) || $name === 'static') {
continue;
}
Middleware::load($project['middleware'] ?? [], '');
}
Middleware::load($projects['middleware'] ?? [], $firm);
if ($static_middlewares = config("plugin.$firm.static.middleware")) {
Middleware::load(['__static__' => $static_middlewares], $firm);
}
}
Middleware::load(['__static__' => config('static.middleware', [])], '');
foreach (config('bootstrap', []) as $class_name) {
if (!class_exists($class_name)) {
$log = "Warning: Class $class_name setting in config/bootstrap.php not found\r\n";
echo $log;
Log::error($log);
continue;
}
/** @var Bootstrap $class_name */
$class_name::start($worker);
}
foreach (config('plugin', []) as $firm => $projects) {
foreach ($projects as $name => $project) {
if (!is_array($project)) {
continue;
}
foreach ($project['bootstrap'] ?? [] as $class_name) {
if (!class_exists($class_name)) {
$log = "Warning: Class $class_name setting in config/plugin/$firm/$name/bootstrap.php not found\r\n";
echo $log;
Log::error($log);
continue;
}
/** @var Bootstrap $class_name */
$class_name::start($worker);
}
}
foreach ($projects['bootstrap'] ?? [] as $class_name) {
if (!class_exists($class_name)) {
$log = "Warning: Class $class_name setting in plugin/$firm/config/bootstrap.php not found\r\n";
echo $log;
Log::error($log);
continue;
}
/** @var Bootstrap $class_name */
$class_name::start($worker);
}
}
$directory = base_path() . '/plugin';
$paths = [config_path()];
foreach (Util::scanDir($directory) as $path) {
if (is_dir($path = "$path/config")) {
$paths[] = $path;
}
}
Route::load($paths);
......@@ -76,20 +76,13 @@ class LaravelDb implements Bootstrap
// Heartbeat
if ($worker) {
Timer::add(55, function () use ($default, $connections) {
if (!class_exists(Connection::class, false)) {
return;
}
foreach ($connections as $key => $item) {
if ($item['driver'] == 'mysql') {
Timer::add(55, function () use ($default, $connections, $capsule) {
foreach ($capsule->getDatabaseManager()->getConnections() as $connection) {
/* @var \Illuminate\Database\MySqlConnection $connection **/
if ($connection->getConfig('driver') == 'mysql') {
try {
if ($key == $default) {
Db::select('select 1');
} else {
Db::connection($key)->select('select 1');
}
} catch (Throwable $e) {
}
$connection->select('select 1');
} catch (Throwable $e) {}
}
}
});
......@@ -97,14 +90,22 @@ class LaravelDb implements Bootstrap
// Paginator
if (class_exists(Paginator::class)) {
Paginator::queryStringResolver(function () {
return request()->queryString();
});
if (method_exists(Paginator::class, 'queryStringResolver')) {
Paginator::queryStringResolver(function () {
$request = request();
return $request ? $request->queryString() : null;
});
}
Paginator::currentPathResolver(function () {
return request()->path();
$request = request();
return $request ? $request->path(): '/';
});
Paginator::currentPageResolver(function ($page_name = 'page') {
$page = (int)request()->input($page_name, 1);
$request = request();
if (!$request) {
return 1;
}
$page = (int)($request->input($page_name, 1));
return $page > 0 ? $page : 1;
});
}
......
......@@ -21,7 +21,7 @@ use Workerman\Worker;
/**
* Class Session
* @package Support
* @package support
*/
class Session implements Bootstrap
{
......
......@@ -15,12 +15,23 @@
namespace support\exception;
use Exception;
use Webman\Http\Response;
use Webman\Http\Request;
/**
* Class BusinessException
* @package Support\Exception
* @package support\exception
*/
class BusinessException extends Exception
{
}
\ No newline at end of file
public function render(Request $request): ?Response
{
if ($request->expectsJson()) {
$code = $this->getCode();
$json = ['code' => $code ? $code : 500, 'msg' => $this->getMessage()];
return new Response(200, ['Content-Type' => 'application/json'],
\json_encode($json, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
return new Response(200, [], $this->getMessage());
}
}
......@@ -21,7 +21,7 @@ use Webman\Http\Response;
/**
* Class Handler
* @package Support\Exception
* @package support\exception
*/
class Handler extends ExceptionHandler
{
......@@ -36,6 +36,11 @@ class Handler extends ExceptionHandler
public function render(Request $request, Throwable $exception): Response
{
if(($exception instanceof BusinessException) && ($response = $exception->render($request)))
{
return $response;
}
return parent::render($request, $exception);
}
......
......@@ -12,7 +12,7 @@
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace support\View;
namespace support\view;
use Jenssegers\Blade\Blade as BladeView;
use Webman\View;
......@@ -20,7 +20,7 @@ use Webman\View;
/**
* Class Blade
* composer require jenssegers/blade
* @package Support\View
* @package support\view
*/
class Blade implements View
{
......@@ -50,13 +50,19 @@ class Blade implements View
$request = \request();
$plugin = $request->plugin ?? '';
$app = $app === null ? $request->app : $app;
$config_prefix = $plugin ? "plugin.$plugin." : '';
$base_view_path = $plugin ? \base_path() . "/plugin/$plugin/app" : \app_path();
if (!isset($views[$app])) {
$key = "{$plugin}-{$request->app}";
if (!isset($views[$key])) {
$view_path = $app === '' ? "$base_view_path/view" : "$base_view_path/$app/view";
$views[$app] = new BladeView($view_path, \runtime_path() . '/views');
$views[$key] = new BladeView($view_path, \runtime_path() . '/views');
$extension = \config("{$config_prefix}view.extension");
if ($extension) {
$extension($views[$key]);
}
}
$vars = \array_merge(static::$_vars, $vars);
$content = $views[$app]->render($template, $vars);
$content = $views[$key]->render($template, $vars);
static::$_vars = [];
return $content;
}
......
......@@ -12,14 +12,14 @@
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace support\View;
namespace support\view;
use Webman\View;
use Throwable;
/**
* Class Raw
* @package Support\View
* @package support\view
*/
class Raw implements View
{
......@@ -51,14 +51,14 @@ class Raw implements View
$view_suffix = \config("{$config_prefix}view.options.view_suffix", 'html');
$app = $app === null ? $request->app : $app;
$base_view_path = $plugin ? \base_path() . "/plugin/$plugin/app" : \app_path();
$view_path = $app === '' ? "$base_view_path/view/$template.$view_suffix" : "$base_view_path/$app/view/$template.$view_suffix";
$__template_path__ = $app === '' ? "$base_view_path/view/$template.$view_suffix" : "$base_view_path/$app/view/$template.$view_suffix";
\extract(static::$_vars, \EXTR_SKIP);
\extract($vars, \EXTR_SKIP);
\extract(static::$_vars);
\extract($vars);
\ob_start();
// Try to include php file.
try {
include $view_path;
include $__template_path__;
} catch (Throwable $e) {
static::$_vars = [];
\ob_end_clean();
......
......@@ -12,14 +12,14 @@
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace support\View;
namespace support\view;
use think\Template;
use Webman\View;
/**
* Class Blade
* @package Support\View
* @package support\view
*/
class ThinkPHP implements View
{
......
......@@ -12,7 +12,7 @@
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace support\View;
namespace support\view;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
......@@ -20,7 +20,7 @@ use Webman\View;
/**
* Class Blade
* @package Support\View
* @package support\view
*/
class Twig implements View
{
......@@ -57,6 +57,10 @@ class Twig implements View
$base_view_path = $plugin ? \base_path() . "/plugin/$plugin/app" : \app_path();
$view_path = $app === '' ? "$base_view_path/view/" : "$base_view_path/$app/view/";
$views[$key] = new Environment(new FilesystemLoader($view_path), \config("{$config_prefix}view.options", []));
$extension = \config("{$config_prefix}view.extension");
if ($extension) {
$extension($views[$key]);
}
}
$vars = \array_merge(static::$_vars, $vars);
$content = $views[$key]->render("$template.$view_suffix", $vars);
......
<?php
/**
* Start file for windows
*/
require_once __DIR__ . '/vendor/autoload.php';
use process\Monitor;
use support\App;
use Dotenv\Dotenv;
use Workerman\Worker;
ini_set('display_errors', 'on');
error_reporting(E_ALL);
if (class_exists('Dotenv\Dotenv') && file_exists(base_path() . '/.env')) {
if (method_exists('Dotenv\Dotenv', 'createUnsafeImmutable')) {
Dotenv::createUnsafeImmutable(base_path())->load();
} else {
Dotenv::createMutable(base_path())->load();
}
}
App::loadAllConfig(['route']);
$error_reporting = config('app.error_reporting');
if (isset($error_reporting)) {
error_reporting($error_reporting);
}
$runtime_process_path = runtime_path() . DIRECTORY_SEPARATOR . '/windows';
if (!is_dir($runtime_process_path)) {
mkdir($runtime_process_path);
}
$process_files = [
__DIR__ . DIRECTORY_SEPARATOR . 'start.php'
];
foreach (config('process', []) as $process_name => $config) {
$process_files[] = write_process_file($runtime_process_path, $process_name, '');
}
foreach (config('plugin', []) as $firm => $projects) {
foreach ($projects as $name => $project) {
if (!is_array($project)) {
continue;
}
foreach ($project['process'] ?? [] as $process_name => $config) {
$process_files[] = write_process_file($runtime_process_path, $process_name, "$firm.$name");
}
}
foreach ($projects['process'] ?? [] as $process_name => $config) {
$process_files[] = write_process_file($runtime_process_path, $process_name, $firm);
}
}
function write_process_file($runtime_process_path, $process_name, $firm)
{
$process_param = $firm ? "plugin.$firm.$process_name" : $process_name;
$config_param = $firm ? "config('plugin.$firm.process')['$process_name']" : "config('process')['$process_name']";
$file_content = <<<EOF
<?php
require_once __DIR__ . '/../../vendor/autoload.php';
use Workerman\Worker;
use Webman\Config;
use support\App;
ini_set('display_errors', 'on');
error_reporting(E_ALL);
if (is_callable('opcache_reset')) {
opcache_reset();
}
App::loadAllConfig(['route']);
worker_start('$process_param', $config_param);
if (DIRECTORY_SEPARATOR != "/") {
Worker::\$logFile = config('server')['log_file'] ?? Worker::\$logFile;
}
Worker::runAll();
EOF;
$process_file = $runtime_process_path . DIRECTORY_SEPARATOR . "start_$process_param.php";
file_put_contents($process_file, $file_content);
return $process_file;
}
if ($monitor_config = config('process.monitor.constructor')) {
$monitor = new Monitor(...array_values($monitor_config));
}
function popen_processes($process_files)
{
$cmd = "php " . implode(' ', $process_files);
$descriptorspec = [STDIN, STDOUT, STDOUT];
$resource = proc_open($cmd, $descriptorspec, $pipes);
if (!$resource) {
exit("Can not execute $cmd\r\n");
}
return $resource;
}
$resource = popen_processes($process_files);
echo "\r\n";
while (1) {
sleep(1);
if (!empty($monitor) && $monitor->checkAllFilesChange()) {
$status = proc_get_status($resource);
$pid = $status['pid'];
shell_exec("taskkill /F /T /PID $pid");
proc_close($resource);
$resource = popen_processes($process_files);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment