1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2016 François Kooman <[email protected]>. |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace fkooman\RemoteStorage\Config; |
20
|
|
|
|
21
|
|
|
use RuntimeException; |
22
|
|
|
use Symfony\Component\Yaml\Dumper; |
23
|
|
|
use Symfony\Component\Yaml\Yaml; |
24
|
|
|
|
25
|
|
|
class YamlFile implements ReaderInterface, WriterInterface |
26
|
|
|
{ |
27
|
|
|
/** @var array */ |
28
|
|
|
private $configFile; |
29
|
|
|
|
30
|
|
|
public function __construct($configFile) |
31
|
|
|
{ |
32
|
|
|
if (!is_array($configFile)) { |
33
|
|
|
$configFile = [$configFile]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->configFile = $configFile; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function readConfig() |
40
|
|
|
{ |
41
|
|
|
foreach ($this->configFile as $configFile) { |
42
|
|
|
$fileContent = @file_get_contents($configFile); |
43
|
|
|
if (false !== $fileContent) { |
44
|
|
|
return Yaml::parse($fileContent); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
throw new RuntimeException(sprintf('unable to read configuration file(s) "%s"', implode(',', $this->configFile))); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function writeConfig(array $config) |
52
|
|
|
{ |
53
|
|
|
$dumper = new Dumper(); |
54
|
|
|
$yamlStr = $dumper->dump($config, 3); |
55
|
|
|
foreach ($this->configFile as $configFile) { |
56
|
|
|
if (false !== @file_put_contents($configFile, $yamlStr)) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
throw new RuntimeException(sprintf('unable to write configuration file(s) "%s"', implode(',', $this->configFile))); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.