PHPDaemon\Core\ClassFinder::find PHP Method

find() public static method

Find class
public static find ( string $class, string $namespace = null, string $rootns = null ) : string
$class string Class
$namespace string Namespace
$rootns string Root namespace
return string
    public static function find($class, $namespace = null, $rootns = null)
    {
        $e = explode('\\', $class);
        if ($e[0] === '') {
            return $class;
        }
        if ('Pool' === $class || 'TransportContext' === $class) {
            return '\\PHPDaemon\\Core\\' . $class;
        }
        if (mb_orig_strpos($class, '\\') === false && $namespace === null) {
            if ('Example' === substr($class, 0, 7)) {
                array_unshift($e, 'Examples');
            }
            if ('Server' === substr($class, -6)) {
                $path = '\\PHPDaemon\\Servers\\' . substr($class, 0, -6) . '\\Pool';
                $r = str_replace('\\Servers\\Servers', '\\Servers', $path);
                Daemon::log('ClassFinder: \'' . $class . '\' -> \'' . $r . '\', you should change your code.');
                return $r;
            }
            if ('Client' === substr($class, -6)) {
                $path = '\\PHPDaemon\\Clients\\' . substr($class, 0, -6) . '\\Pool';
                $r = str_replace('\\Clients\\Clients', '\\Clients', $path);
                Daemon::log('ClassFinder: \'' . $class . '\' -> \'' . $r . '\', you should change your code.');
                return $r;
            }
            if ('ClientAsync' === substr($class, -11)) {
                $path = '\\PHPDaemon\\Clients\\' . substr($class, 0, -11) . '\\Pool';
                $r = str_replace('\\Client\\Clients', '\\Clients', $path);
                Daemon::log('ClassFinder: \'' . $class . '\' -> \'' . $r . '\', you should change your code.');
                return $r;
            }
        }
        if ($namespace !== null && sizeof($e) < 2) {
            array_unshift($e, $namespace);
        }
        array_unshift($e, '\\' . ($rootns !== null ? $rootns : Daemon::$config->defaultns->value));
        return implode('\\', $e);
    }

Usage Example

 /**
  * Gets instance of application
  * @param  string  $appName  Application name
  * @param  string  $instance Instance name
  * @param  boolean $spawn    If true, we spawn an instance if absent
  * @param  boolean $preload  If true, worker is at the preload stage
  * @return object $instance AppInstance
  */
 public function getInstance($appName, $instance = '', $spawn = true, $preload = false)
 {
     $class = ClassFinder::find($appName, 'Applications');
     if (isset(Daemon::$appInstances[$class][$instance])) {
         return Daemon::$appInstances[$class][$instance];
     }
     if (!$spawn) {
         return false;
     }
     $fullname = $this->getAppFullname($appName, $instance);
     if (!class_exists($class)) {
         Daemon::$process->log(__METHOD__ . ': unable to find application class ' . $class . '\'');
         return false;
     }
     if (!is_subclass_of($class, '\\PHPDaemon\\Core\\AppInstance')) {
         Daemon::$process->log(__METHOD__ . ': class ' . $class . '\' found, but skipped as long as it is not subclass of \\PHPDaemon\\Core\\AppInstance');
         return false;
     }
     $fullnameClass = $this->getAppFullname($class, $instance);
     if ($fullname !== $fullnameClass && isset(Daemon::$config->{$fullname})) {
         Daemon::$config->renameSection($fullname, $fullnameClass);
     }
     if (!$preload) {
         /** @noinspection PhpUndefinedVariableInspection */
         if (!$class::$runOnDemand) {
             return false;
         }
         if (isset(Daemon::$config->{$fullnameClass}->limitinstances)) {
             return false;
         }
     }
     return new $class($instance);
 }
All Usage Examples Of PHPDaemon\Core\ClassFinder::find