-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathOutputWriter.php
More file actions
100 lines (88 loc) · 2.45 KB
/
Copy pathOutputWriter.php
File metadata and controls
100 lines (88 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
declare(strict_types=1);
/*
* This file is part of Class Preloader.
*
* (c) Graham Campbell <hello@gjcampbell.co.uk>
* (c) Michael Dowling <mtdowling@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ClassPreloader;
use ClassPreloader\Exception\IOException;
use ClassPreloader\File\FileUtils;
/**
* This is the output writer.
*/
final class OutputWriter
{
/**
* Open an output file, ensuring its directory exists.
*
* @param string $filePath
*
* @throws \ClassPreloader\Exception\IOException
*
* @return resource
*/
public static function openOutputFile(string $filePath)
{
$dirPath = dirname($filePath);
if (!FileUtils::ensureDirectoryExists($dirPath)) {
throw new IOException("Unable to create directory $dirPath.");
}
$handle = FileUtils::openFileForWriting($filePath);
if ($handle === false) {
throw new IOException("Unable to open $filePath for writing.");
}
return $handle;
}
/**
* Write an opening PHP tag to the given handle.
*
* @param resource $handle
* @param bool $strictTypes
*
* @throws \ClassPreloader\Exception\IOException
*
* @return void
*/
public static function writeOpeningTag($handle, bool $strictTypes)
{
if (!FileUtils::writeString($handle, $strictTypes ? "<?php declare(strict_types=1);\n" : "<?php\n")) {
throw new IOException('Unable to write opening tag to the output file.');
}
}
/**
* Write the given file content to the given handle.
*
* @param resource $handle
* @param string $fileContent
*
* @throws \ClassPreloader\Exception\IOException
*
* @return void
*/
public static function writeFileContent($handle, string $fileContent)
{
if (!FileUtils::writeString($handle, $fileContent)) {
throw new IOException('Unable to write file content to the output file.');
}
}
/**
* Close the given handle.
*
* @param resource $handle
*
* @throws \ClassPreloader\Exception\IOException
*
* @return void
*/
public static function closeHandle($handle)
{
if (!FileUtils::closeHandle($handle)) {
throw new IOException('Unable to close the output file.');
}
}
}