1
|
|
|
<?php |
2
|
|
|
namespace Naneau\FileGen; |
3
|
|
|
|
4
|
|
|
use Naneau\FileGen\Parameterized; |
5
|
|
|
|
6
|
|
|
use Naneau\FileGen\AccessRights; |
7
|
|
|
|
8
|
|
|
use Naneau\FileGen\File\Contents as ContentGenerator; |
9
|
|
|
use Naneau\FileGen\File\Contents\StringBased as StringContents; |
10
|
|
|
|
11
|
|
|
use \InvalidArgumentException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A file |
15
|
|
|
*/ |
16
|
|
|
class File extends AccessRights |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Contents of the file |
20
|
|
|
* |
21
|
|
|
* @var ContentGenerator |
22
|
|
|
**/ |
23
|
|
|
private $contentGenerator; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor |
27
|
|
|
* |
28
|
|
|
* @param string $name |
29
|
|
|
* @param ContentGenerator|string $contents |
30
|
|
|
* @param int $mode mode in octal 0XXX |
31
|
|
|
* @return void |
|
|
|
|
32
|
|
|
**/ |
33
|
|
|
public function __construct($name, $contents = '', $mode = null) |
34
|
|
|
{ |
35
|
|
|
parent::__construct($name, $mode); |
36
|
|
|
|
37
|
|
|
$this->setContentGenerator($contents); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the contents as a string |
42
|
|
|
* |
43
|
|
|
* @param array[string]string $parameters |
|
|
|
|
44
|
|
|
* @return string |
45
|
|
|
**/ |
46
|
|
|
public function getContents(array $parameters = array()) |
47
|
|
|
{ |
48
|
|
|
// Merge incoming parameters with that of the content generator if the |
49
|
|
|
// content generator is parameterized |
50
|
|
|
if ($this->getContentGenerator() instanceof Parameterized) { |
51
|
|
|
$this->getContentGenerator()->setParameters(array_merge( |
52
|
|
|
$this->getContentGenerator()->getParameters(), |
53
|
|
|
$parameters |
54
|
|
|
)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->getContentGenerator()->getContents(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the content generator |
62
|
|
|
* |
63
|
|
|
* @return ContentGenerator |
64
|
|
|
*/ |
65
|
|
|
public function getContentGenerator() |
66
|
|
|
{ |
67
|
|
|
return $this->contentGenerator; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set the content generator |
72
|
|
|
* |
73
|
|
|
* @param ContentGenerator $contentGenerator |
74
|
|
|
* @return File |
75
|
|
|
*/ |
76
|
|
|
public function setContentGenerator($contentGenerator) |
77
|
|
|
{ |
78
|
|
|
if (is_string($contentGenerator)) { |
79
|
|
|
$this->contentGenerator = new StringContents($contentGenerator); |
80
|
|
|
} elseif ($contentGenerator instanceof ContentGenerator) { |
81
|
|
|
$this->contentGenerator = $contentGenerator; |
82
|
|
|
} else { |
83
|
|
|
throw new InvalidArgumentException( |
84
|
|
|
'Content generator needs to be string or instance of Naneau\FileGen\File\Contents' |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.