Completed
Push — master ( 5d29b3...11effe )
by Nazar
04:11
created

Output::set_stream()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @package   Composer
4
 * @category  modules
5
 * @author    Nazar Mokrynskyi <[email protected]>
6
 * @copyright Copyright (c) 2015-2016, Nazar Mokrynskyi
7
 * @license   MIT License, see license.txt
8
 */
9
namespace cs\modules\Composer;
10
use
11
	cs\Event,
12
	Symfony\Component\Console\Formatter\OutputFormatterInterface,
13
	Symfony\Component\Console\Output\Output as Symfony_output;
14
15
/**
16
 * Provides next events:
17
 *  Composer/update_progress
18
 *  [
19
 *   'message' => $message,     //Current message targeted for output
20
 *   'buffer'  => $this->buffer //Total output
21
 *  ]
22
 */
23
class Output extends Symfony_output {
24
	/**
25
	 * @var string
26
	 */
27
	protected $buffer = '';
28
	/**
29
	 * @var resource
30
	 */
31
	protected $stream;
32
	function __construct ($verbosity = self::VERBOSITY_NORMAL, $decorated = false, OutputFormatterInterface $formatter = null) {
33
		$this->stream = fopen(STORAGE.'/Composer/last_execution.log', 'w');
34
		parent::__construct($verbosity, $decorated, $formatter);
35
	}
36
	/**
37
	 * @return string
38
	 */
39
	function get_buffer () {
40
		return $this->buffer;
41
	}
42
	/**
43
	 * @param string $message
44
	 * @param bool   $newline
45
	 */
46
	protected function doWrite ($message, $newline) {
47
		if ($newline) {
48
			$message .= "\n";
49
		}
50
		fwrite($this->stream, $message);
51
		$this->buffer .= $message;
52
		Event::instance()->fire(
53
			'Composer/update_progress',
54
			[
55
				'message' => $message,
56
				'buffer'  => $this->buffer
57
			]
58
		);
59
	}
60
	function __destruct () {
61
		fclose($this->stream);
62
	}
63
}
64