Browse Source

fix run_path() (#7668)

walkor 2 years ago
parent
commit
11552dc1a6

+ 8 - 2
frameworks/PHP/webman/composer.json

@@ -42,8 +42,14 @@
     ]
   },
   "scripts": {
-    "post-autoload-dump": [
-      "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
+    "post-package-install": [
+      "support\\Plugin::install"
+    ],
+    "post-package-update": [
+      "support\\Plugin::install"
+    ],
+    "pre-package-uninstall": [
+      "support\\Plugin::uninstall"
     ]
   }
 }

+ 5 - 5
frameworks/PHP/webman/support/bootstrap.php

@@ -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;

+ 58 - 31
frameworks/PHP/webman/support/helpers.php

@@ -1,4 +1,5 @@
 <?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);
         }
-
     };
 }