Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 11 | class AppResolver |
||
| 12 | { |
||
| 13 | use \PHPDaemon\Traits\ClassWatchdog; |
||
| 14 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Preloads applications |
||
| 18 | * @param boolean $privileged If true, we are in the pre-fork stage |
||
| 19 | * @return void |
||
| 20 | */ |
||
| 21 | public function preload($privileged = false) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Gets instance of application |
||
| 51 | * @param string $appName Application name |
||
| 52 | * @param string $instance Instance name |
||
| 53 | * @param boolean $spawn If true, we spawn an instance if absent |
||
| 54 | * @param boolean $preload If true, worker is at the preload stage |
||
| 55 | * @return object $instance AppInstance |
||
| 56 | */ |
||
| 57 | public function getInstance($appName, $instance = '', $spawn = true, $preload = false) |
||
| 58 | { |
||
| 59 | $class = ClassFinder::find($appName, 'Applications'); |
||
| 60 | if (isset(Daemon::$appInstances[$class][$instance])) { |
||
| 61 | return Daemon::$appInstances[$class][$instance]; |
||
| 62 | } |
||
| 63 | if (!$spawn) { |
||
| 64 | return false; |
||
| 65 | } |
||
| 66 | $fullname = $this->getAppFullName($appName, $instance); |
||
| 67 | View Code Duplication | if (!class_exists($class)) { |
|
| 68 | Daemon::$process->log(__METHOD__ . ': unable to find application class ' . json_encode($class) . '\''); |
||
| 69 | return false; |
||
| 70 | } |
||
| 71 | View Code Duplication | if (!is_subclass_of($class, '\\PHPDaemon\\Core\\AppInstance')) { |
|
| 72 | Daemon::$process->log(__METHOD__ . ': class ' . json_encode($class) . '\' found, but skipped as long as it is not subclass of \\PHPDaemon\\Core\\AppInstance'); |
||
| 73 | return false; |
||
| 74 | } |
||
| 75 | $fullnameClass = $this->getAppFullName($class, $instance); |
||
| 76 | if ($fullname !== $fullnameClass && isset(Daemon::$config->{$fullname})) { |
||
| 77 | Daemon::$config->renameSection($fullname, $fullnameClass); |
||
| 78 | } |
||
| 79 | if (!$preload) { |
||
| 80 | /** @noinspection PhpUndefinedVariableInspection */ |
||
| 81 | if (!$class::$runOnDemand) { |
||
| 82 | return false; |
||
| 83 | } |
||
| 84 | if (isset(Daemon::$config->{$fullnameClass}->limitinstances)) { |
||
| 85 | return false; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | return new $class($instance); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Resolve full name of application by its class and name |
||
| 94 | * @param string $appName Application name |
||
| 95 | * @param string $instance Application class |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function getAppFullName($appName, $instance = '') |
||
| 99 | { |
||
| 100 | return $appName . ($instance !== '' ? ':' . $instance : ''); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Routes incoming request to related application |
||
| 105 | * @param object $req Generic |
||
| 106 | * @param object $upstream AppInstance of Upstream |
||
| 107 | * @param string $responder |
||
| 108 | * @return object Request |
||
| 109 | */ |
||
| 110 | public function getRequest($req, $upstream, $responder = null) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Routes incoming request to related application. Method is for overloading |
||
| 139 | * @param object $req Generic |
||
| 140 | * @param object $upstream AppInstance of Upstream |
||
| 141 | * @return string Application's name |
||
| 142 | */ |
||
| 143 | public function getRequestRoute($req, $upstream) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Gets instance of application |
||
| 149 | * @alias AppResolver::getInstance |
||
| 150 | * @param string $appName Application name |
||
| 151 | * @param string $instance Instance name |
||
| 152 | * @param boolean $spawn If true, we spawn an instance if absent |
||
| 153 | * @param boolean $preload If true, worker is at the preload stage |
||
| 154 | * @return object AppInstance |
||
| 155 | */ |
||
| 156 | public function getInstanceByAppName($appName, $instance = '', $spawn = true, $preload = false) |
||
| 160 | } |
||
| 161 |