Completed
Push — master ( 7e70c2...f499f2 )
by ANTHONIUS
03:01
created

LogTrait::setOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Yawik project.
5
 *
6
 *      (c) Anthonius Munthi
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Yawik\Composer;
13
14
use Composer\IO\IOInterface;
15
use Psr\Log\LoggerInterface;
16
use Psr\Log\LogLevel;
17
use Symfony\Component\Console\Output\ConsoleOutput;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Input\InputInterface;
20
21
trait LogTrait
22
{
23
24
    /**
25
     * @var OutputInterface
26
     */
27
    protected $output;
28
29
    /**
30
     * @var InputInterface
31
     */
32
    protected $input;
33
34
    /**
35
     * @var LoggerInterface
36
     */
37
    private $logger;
38
39
    public function setOutputFromComposerIO(IOInterface $output)
40
    {
41
        if (!is_null($output)) {
42
            $level   = OutputInterface::VERBOSITY_NORMAL;
43
44
            if ($output->isVeryVerbose() || $output->isDebug()) {
45
                $level = OutputInterface::VERBOSITY_VERY_VERBOSE;
46
            } elseif ($output->isVerbose()) {
47
                $level = OutputInterface::VERBOSITY_VERBOSE;
48
            }
49
50
            $this->getOutput()->setVerbosity($level);
51
            $this->getOutput()->setDecorated($output->isDecorated());
52
        }
53
    }
54
55
    /**
56
     * @param InputInterface $input
57
     * @return $this
58
     */
59
    public function setInput($input)
60
    {
61
        $this->input = $input;
62
        return $this;
63
    }
64
65
    /**
66
     * @return OutputInterface
67
     */
68
    public function getOutput()
69
    {
70
        if (is_null($this->output)) {
71
            $this->output = new ConsoleOutput();
72
        }
73
        return $this->output;
74
    }
75
76
    /**
77
     * @param OutputInterface $output
78
     * @return $this
79
     */
80
    public function setOutput($output)
81
    {
82
        $this->output = $output;
83
        return $this;
84
    }
85
86
87
    /**
88
     * Set a logger to use
89
     * @param LoggerInterface $logger
90
     * @return $this
91
     */
92
    public function setLogger(LoggerInterface $logger)
93
    {
94
        $this->logger = $logger;
95
96
        return $this;
97
    }
98
99
    /**
100
     * @param string $message
101
     */
102
    public function logDebug($message)
103
    {
104
        $this->doLog(LogLevel::DEBUG, $message);
105
    }
106
107
    /**
108
     * @param string $message
109
     */
110
    public function logError($message)
111
    {
112
        $this->doLog(LogLevel::ERROR, $message);
113
    }
114
115
    /**
116
     * @param string $message
117
     */
118
    public function log($message)
119
    {
120
        $this->doLog(LogLevel::INFO, $message);
121
    }
122
123
    public function isCli()
124
    {
125
        return php_sapi_name() === 'cli';
126
    }
127
128
    public function doLog($level, $message)
129
    {
130
        $message = str_replace(getcwd().DIRECTORY_SEPARATOR, '', $message);
131
        if (is_object($this->logger)) {
132
            $this->logger->log($level, $message);
133
        }
134
        if ($this->isCli()) {
135
            switch ($level) {
136
                case LogLevel::DEBUG:
137
                    $outputLevel = OutputInterface::VERBOSITY_VERY_VERBOSE;
138
                    break;
139
                case LogLevel::ERROR:
140
                    $message = '<error>'.$message.'</error>';
141
                    $outputLevel = OutputInterface::OUTPUT_NORMAL;
142
                    break;
143
                case LogLevel::INFO:
144
                default:
145
                    $outputLevel = OutputInterface::OUTPUT_NORMAL;
146
                    break;
147
            }
148
            $this->doWrite($message, $outputLevel);
149
        }
150
    }
151
152
    public function doWrite($message, $outputLevel = 0)
153
    {
154
        $message = sprintf(
155
            '<info>[yawik]</info> %s',
156
            $message
157
        );
158
        $this->output->writeln($message, $outputLevel);
159
    }
160
}
161