|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* AnimeDb package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Peter Gribanov <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
|
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace AnimeDb\Bundle\CatalogBundle\Console\Output; |
|
10
|
|
|
|
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
use Symfony\Component\Filesystem\Exception\IOException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Export output to file. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Peter Gribanov <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class Export extends Decorator |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* File handle. |
|
23
|
|
|
* |
|
24
|
|
|
* @var resource |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $handle; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Append to end file. |
|
30
|
|
|
* |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $append = true; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param OutputInterface $output |
|
37
|
|
|
* @param string $filename |
|
38
|
|
|
* @param bool $append |
|
39
|
|
|
*/ |
|
40
|
76 |
|
public function __construct(OutputInterface $output, $filename, $append = true) |
|
41
|
|
|
{ |
|
42
|
76 |
|
parent::__construct($output); |
|
43
|
76 |
|
$this->append = $append; |
|
44
|
|
|
|
|
45
|
76 |
|
$dir = pathinfo($filename, PATHINFO_DIRNAME); |
|
46
|
76 |
|
if (!is_dir($dir) && true !== @mkdir($dir, 0755, true)) { |
|
47
|
1 |
|
throw new IOException('Failed to create the export directory: '.$dir); |
|
48
|
|
|
} |
|
49
|
75 |
|
if (!($this->handle = @fopen($filename, 'w'))) { |
|
50
|
1 |
|
throw new IOException('Failed to open the export file: '.$filename); |
|
51
|
|
|
} |
|
52
|
74 |
|
} |
|
53
|
1 |
|
|
|
54
|
|
|
/** |
|
55
|
73 |
|
* @param array|string $messages |
|
56
|
|
|
* @param bool|false $newline |
|
57
|
|
|
* @param int $type |
|
58
|
|
|
*/ |
|
59
|
|
|
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->writeToFile($messages, $newline); |
|
62
|
49 |
|
parent::write($messages, $newline, $type); |
|
63
|
|
|
} |
|
64
|
49 |
|
|
|
65
|
49 |
|
/** |
|
66
|
49 |
|
* @param array|string $messages |
|
67
|
|
|
* @param int $type |
|
68
|
|
|
*/ |
|
69
|
|
|
public function writeln($messages, $type = self::OUTPUT_NORMAL) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->writeToFile($messages, true); |
|
72
|
24 |
|
parent::writeln($messages, $type); |
|
73
|
|
|
} |
|
74
|
24 |
|
|
|
75
|
24 |
|
/** |
|
76
|
24 |
|
* @param string|array $messages |
|
77
|
|
|
* @param bool $newline |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function writeToFile($messages, $newline) |
|
80
|
|
|
{ |
|
81
|
|
|
$messages = (array) $messages; |
|
82
|
73 |
|
foreach ($messages as $key => $message) { |
|
83
|
|
|
$messages[$key] = strip_tags($message).($newline ? PHP_EOL : ''); |
|
84
|
73 |
|
} |
|
85
|
73 |
|
|
|
86
|
73 |
|
// reset file |
|
87
|
|
|
if (!$this->append) { |
|
88
|
|
|
rewind($this->handle); |
|
89
|
|
|
ftruncate($this->handle, 0); |
|
90
|
73 |
|
} |
|
91
|
37 |
|
|
|
92
|
37 |
|
fwrite($this->handle, implode('', $messages)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
73 |
|
public function unlock() |
|
96
|
73 |
|
{ |
|
97
|
|
|
if (is_resource($this->handle)) { |
|
98
|
73 |
|
fclose($this->handle); |
|
99
|
|
|
} |
|
100
|
73 |
|
} |
|
101
|
73 |
|
|
|
102
|
73 |
|
public function __destruct() |
|
103
|
|
|
{ |
|
104
|
73 |
|
$this->unlock(); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|