Jelajahi Sumber

workerman Autoloader

walkor 10 tahun lalu
induk
melakukan
a6d9e875d7

+ 0 - 6
GatewayWorker/BusinessWorker.php

@@ -8,7 +8,6 @@ use \Workerman\Lib\Timer;
 use \GatewayWorker\Lib\Lock;
 use \GatewayWorker\Lib\Store;
 use \GatewayWorker\Lib\Context;
-use \GatewayWorker\Lib\Autoloader;
 use \Event;
 
 class BusinessWorker extends Worker
@@ -19,19 +18,14 @@ class BusinessWorker extends Worker
     
     public $badGatewayAddress = array();
     
-    protected $_rootPath = '';
-    
     public function __construct($socket_name = '', $context_option = array())
     {
         $this->onWorkerStart = array($this, 'onWorkerStart');
-        $backrace = debug_backtrace();
-        $this->_rootPath = dirname($backrace[0]['file']);
         parent::__construct($socket_name, $context_option);
     }
     
     protected function onWorkerStart()
     {
-        Autoloader::setRootPath($this->_rootPath);
         Timer::add(1, array($this, 'checkGatewayConnections'));
         $this->checkGatewayConnections();
         \GatewayWorker\Lib\Gateway::setBusinessWorker($this);

+ 0 - 7
GatewayWorker/Gateway.php

@@ -6,7 +6,6 @@ use \Workerman\Lib\Timer;
 use \Workerman\Protocols\GatewayProtocol;
 use \GatewayWorker\Lib\Lock;
 use \GatewayWorker\Lib\Store;
-use \GatewayWorker\Lib\Autoloader;
 
 class Gateway extends Worker
 {
@@ -30,8 +29,6 @@ class Gateway extends Worker
     
     protected $_innerUdpWorker = null;
     
-    protected $_rootPath = '';
-    
     public function __construct($socket_name, $context_option = array())
     {
         $this->onWorkerStart = array($this, 'onWorkerStart');
@@ -39,8 +36,6 @@ class Gateway extends Worker
         $this->onMessage = array($this, 'onClientMessage');
         $this->onClose = array($this, 'onClientClose');
         $this->onWorkerStop = array($this, 'onWorkerStop');
-        $backrace = debug_backtrace();
-        $this->_rootPath = dirname($backrace[0]['file']);
         parent::__construct($socket_name, $context_option);
     }
     
@@ -153,8 +148,6 @@ class Gateway extends Worker
     
     public function onWorkerStart()
     {
-        Autoloader::setRootPath($this->_rootPath);
-        
         $this->lanPort = $this->startPort - posix_getppid() + posix_getpid();
     
         if($this->lanPort<0 || $this->lanPort >=65535)

+ 0 - 28
GatewayWorker/Lib/Autoloader.php

@@ -1,28 +0,0 @@
-<?php
-namespace GatewayWorker\Lib;
-
-class Autoloader
-{
-    protected static $_rootPath = '';
-    
-    public static function setRootPath($root_path)
-    {
-        self::$_rootPath = $root_path;
-        spl_autoload_register('\GatewayWorker\Lib\Autoloader::loadByNamespace');
-    }
-    
-    public static function loadByNamespace($name)
-    {
-        $class_path = str_replace('\\', DIRECTORY_SEPARATOR ,$name);
-        $class_file = self::$_rootPath . '/' . $class_path.'.php';
-        if(is_file($class_file))
-        {
-            require_once($class_file);
-            if(class_exists($name, false))
-            {
-                return true;
-            }
-        }
-        return false;
-    }
-}

+ 25 - 9
Workerman/Autoloader.php

@@ -1,4 +1,6 @@
 <?php
+namespace Workerman;
+
 if(!defined('WORKERMAN_ROOT_DIR'))
 {
     define('WORKERMAN_ROOT_DIR', realpath(__DIR__  . '/../'));
@@ -6,18 +8,32 @@ if(!defined('WORKERMAN_ROOT_DIR'))
 
 require_once WORKERMAN_ROOT_DIR.'/Workerman/Lib/Constants.php';
 
-function workerman_loader($name)
+class Autoloader
 {
-    $class_path = str_replace('\\', DIRECTORY_SEPARATOR ,$name);
-    $class_file = WORKERMAN_ROOT_DIR . DIRECTORY_SEPARATOR . "$class_path.php";
-    if(is_file($class_file))
+    protected static $_appInitPath = '';
+
+    public static function setRootPath($root_path)
     {
-        require_once($class_file);
-        if(class_exists($name, false))
+        self::$_appInitPath = $root_path;
+        spl_autoload_register('\GatewayWorker\Lib\Autoloader::loadByNamespace');
+    }
+
+    public static function loadByNamespace($name)
+    {
+        $class_path = str_replace('\\', DIRECTORY_SEPARATOR ,$name);
+        $class_file = self::$_appInitPath . '/' . $class_path.'.php';
+        if(is_file($class_file))
+        {
+            $class_file = WORKERMAN_ROOT_DIR . DIRECTORY_SEPARATOR . "$class_path.php";
+        }
+        if(is_file($class_file))
         {
-            return true;
+            require_once($class_file);
+            if(class_exists($name, false))
+            {
+                return true;
+            }
         }
+        return false;
     }
-    return false;
 }
-spl_autoload_register('workerman_loader');

+ 12 - 1
Workerman/Worker.php

@@ -8,6 +8,7 @@ use \Workerman\Connection\ConnectionInterface;
 use \Workerman\Connection\TcpConnection;
 use \Workerman\Connection\UdpConnection;
 use \Workerman\Lib\Timer;
+use \Workerman\Autoloader;
 use \Exception;
 
 /**
@@ -20,7 +21,7 @@ class Worker
      * workerman version
      * @var string
      */
-    const VERSION = '3.0.1';
+    const VERSION = '3.0.2';
     
     /**
      * status starting
@@ -139,6 +140,12 @@ class Worker
     protected $_protocol = '';
     
     /**
+     * app init path
+     * @var string
+     */
+    protected $_appInitPath = '';
+    
+    /**
      * if run as daemon
      * @var bool
      */
@@ -957,6 +964,9 @@ class Worker
         self::$_workers[$this->workerId] = $this;
         self::$_pidMap[$this->workerId] = array();
         
+        $backrace = debug_backtrace();
+        $this->_appInitPath = dirname($backrace[0]['file']);
+        
         if($socket_name)
         {
             $this->_socketName = $socket_name;
@@ -1068,6 +1078,7 @@ class Worker
         
         if($this->onWorkerStart)
         {
+            Autoloader::setRootPath($this->_appInitPath);
             call_user_func($this->onWorkerStart, $this);
         }
         self::$globalEvent->loop();