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