Passed
Pull Request — master (#1109)
by Tim
06:50
created

GeneratorThread::getDefaultLogFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * \AppserverIo\Appserver\Core\GeneratorThread
4
 *
5
 * NOTICE OF LICENSE
6
 *
7
 * This source file is subject to the Open Software License (OSL 3.0)
8
 * that is available through the world-wide-web at this URL:
9
 * http://opensource.org/licenses/osl-3.0.php
10
 *
11
 * PHP version 5
12
 *
13
 * @author    Bernhard Wick <[email protected]>
14
 * @copyright 2015 TechDivision GmbH - <[email protected]>
15
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
16
 * @link      https://github.com/appserver-io/appserver
17
 * @link      http://www.appserver.io
18
 */
19
20
namespace AppserverIo\Appserver\Core;
21
22
use AppserverIo\Doppelgaenger\Generator;
23
use Psr\Log\LogLevel;
24
25
/**
26
 * Simple thread for parallel creation of contract-enabled structure definitions.
27
 *
28
 * @author    Bernhard Wick <[email protected]>
29
 * @copyright 2015 TechDivision GmbH - <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/appserver-io/appserver
32
 * @link      http://www.appserver.io
33
 */
34
class GeneratorThread extends \Thread
35
{
36
37
    /**
38
     * The default format for log messages.
39
     *
40
     * @var string
41
     */
42
    const LOG_FORMAT = '[%s] - %s (%s): %s [%s]';
43
44
    /**
45
     * Generator instance to use for creation
46
     *
47
     * @var \AppserverIo\Doppelgaenger\Generator $generator
48
     */
49
    protected $generator;
50
51
    /**
52
     * Array of structures we will be creating
53
     *
54
     * @var array<\AppserverIo\Doppelgaenger\Entities\Definitions\Structure> $structures
55
     */
56
    protected $structures;
57
58
    /**
59
     * Default constructor
60
     *
61
     * @param \AppserverIo\Doppelgaenger\Generator $generator  Our Doppelgaenger generator instance
62
     * @param array                                $structures List of structures to generate
63
     */
64
    public function __construct(Generator $generator, array $structures)
65
    {
66
        $this->generator = $generator;
67
        $this->structures = $structures;
68
    }
69
70
    /**
71
     * Run method
72
     *
73
     * @return void
74
     */
75
    public function run()
76
    {
77
78
        // register a shutdown function
79
        register_shutdown_function(array($this, 'shutdown'));
80
81
        // register the default autoloader
82
        require SERVER_AUTOLOADER;
83
84
        try {
85
            // iterate over all structures and generate them
86
            foreach ($this->structures as $structure) {
87
                $this->generator->create($structure);
88
            }
89
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
90
        } catch (\Exception $e) {
91
            $this->log(LogLevel::ERROR, $e->__toString());
92
        }
93
    }
94
95
    /**
96
     * The shutdown method implementation.
97
     *
98
     *@return void
99
     */
100
    public function shutdown()
101
    {
102
103
        // check if there was a fatal error caused shutdown
104
        if ($lastError = error_get_last()) {
105
            // initialize error type and message
106
            $type = 0;
107
            $message = '';
108
            // extract the last error values
109
            extract($lastError);
110
            // query whether we've a fatal/user error
111
            if ($type === E_ERROR || $type === E_USER_ERROR) {
112
                $this->log(LogLevel::ERROR, $message);
113
            }
114
        }
115
    }
116
117
    /**
118
     * Returns the default format for log messages.
119
     *
120
     * @return string The default log message format
121
     */
122
    public function getDefaultLogFormat()
123
    {
124
        return GeneratorThread::LOG_FORMAT;
125
    }
126
127
    /**
128
     * This is a very basic method to log some stuff by using the error_log() method of PHP.
129
     *
130
     * @param mixed  $level   The log level to use
131
     * @param string $message The message we want to log
132
     * @param array  $context The context we of the message
133
     *
134
     * @return void
135
     */
136
    public function log($level, $message, array $context = array())
137
    {
138
        error_log(sprintf($this->getDefaultLogFormat(), date('Y-m-d H:i:s'), gethostname(), $level, $message, json_encode($context)));
139
    }
140
}
141