Completed
Pull Request — master (#234)
by Дмитрий
07:51
created

Pool::init()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 8
dl 0
loc 22
rs 6.6037
eloc 16
c 2
b 1
f 0
nc 9
nop 0
1
<?php
2
namespace PHPDaemon\Core;
3
4
use PHPDaemon\Core\AppInstance;
5
use PHPDaemon\Core\ClassFinder;
6
use PHPDaemon\Core\Daemon;
7
8
/**
9
 * Pool application instance
10
 *
11
 * @package Core
12
 *
13
 * @author  Vasily Zorin <[email protected]>
14
 */
15
class Pool extends AppInstance
16
{
17
    /**
18
     * @var Pool
19
     */
20
    public $pool;
21
22
    /**
23
     * Setting default config options
24
     * Overriden from AppInstance::getConfigDefaults
25
     * Uncomment and return array with your default options
26
     * @return boolean
27
     */
28
    protected function getConfigDefaults()
29
    {
30
        return false;
31
    }
32
33
    /**
34
     * Constructor.
35
     * @return void
36
     */
37
    protected function init()
38
    {
39
        if ($this->isEnabled()) {
40
            list($class, $name) = explode(':', $this->name . ':');
41
            $realclass = ClassFinder::find($class);
42
            $e = explode('\\', $realclass);
43
            if (($e[sizeof($e) - 1] !== 'Pool') && class_exists($realclass . '\\Pool')) {
44
                $realclass .= '\\Pool';
45
            }
46
            if ($realclass !== $class) {
47
                $base = '\\PHPDaemon\\Core\\Pool:';
48
                Daemon::$config->renameSection($base . $class . ($name !== '' ? ':' . $name : ''), $base . $realclass . ($name !== '' ? ':' . $name : ''));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 155 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
49
            }
50
            if (!class_exists($realclass)) {
51
                Daemon::log($realclass . ' class not exists.');
52
                return;
53
            }
54
            $func = [$realclass, 'getInstance'];
55
            $this->pool = $func($name);
56
            $this->pool->appInstance = $this;
57
        }
58
    }
59
60
    /**
61
     * Function handles incoming Remote Procedure Calls
62
     * You can override it
63
     * @param string $method Method name.
64
     * @param array $args    Arguments.
65
     * @return mixed Result
66
     */
67
    public function RPCall($method, $args)
68
    {
69
        if (!is_callable($f = [$this->pool, 'RPCall'])) {
70
            return false;
71
        }
72
        return $f($method, $args);
73
    }
74
75
    /**
76
     * Called when the worker is ready to go.
77
     * @return void
78
     */
79
    public function onReady()
80
    {
81
        if ($this->pool) {
82
            $this->pool->onReady();
83
        }
84
    }
85
86
    /**
87
     * Called when worker is going to update configuration.
88
     * @return void
89
     */
90
    public function onConfigUpdated()
91
    {
92
        if ($this->pool) {
93
            $this->pool->config = $this->config;
94
            $this->pool->onConfigUpdated();
95
        }
96
    }
97
98
    /**
99
     * Called when application instance is going to shutdown.
100
     * @return boolean Ready to shutdown?
101
     */
102
    public function onShutdown($graceful = false)
103
    {
104
        if ($this->pool) {
105
            return $this->pool->onShutdown($graceful);
106
        }
107
        return true;
108
    }
109
}
110