1 | <?php |
||
45 | abstract class AbstractRepository implements RepositoryInterface |
||
46 | { |
||
47 | /** |
||
48 | * The repository path |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $repositoryPath; |
||
53 | |||
54 | /** |
||
55 | * The mode used to create files when requested |
||
56 | * |
||
57 | * @var integer |
||
58 | */ |
||
59 | protected $fileCreationMode = 0644; |
||
60 | |||
61 | /** |
||
62 | * The mode used to create directories when requested |
||
63 | * |
||
64 | * @var integer |
||
65 | */ |
||
66 | protected $directoryCreationMode = 0755; |
||
67 | |||
68 | /** |
||
69 | * The author used when committing changes |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | protected $author; |
||
74 | |||
75 | /** |
||
76 | * Creates a new repository instance - use {@see open()} instead |
||
77 | * |
||
78 | * @param string $repositoryPath |
||
79 | */ |
||
80 | 193 | protected function __construct($repositoryPath) |
|
84 | |||
85 | /** |
||
86 | * Returns the full file system path to the repository |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | 189 | public function getRepositoryPath() |
|
94 | |||
95 | /** |
||
96 | * Returns the mode used to create files when requested |
||
97 | * |
||
98 | * @return integer |
||
99 | */ |
||
100 | 41 | public function getFileCreationMode() |
|
104 | |||
105 | /** |
||
106 | * Sets the mode used to create files when requested |
||
107 | * |
||
108 | * @param integer $fileCreationMode The mode, e.g. 644 |
||
109 | * @return RepositoryInterface |
||
110 | */ |
||
111 | public function setFileCreationMode($fileCreationMode) |
||
116 | |||
117 | /** |
||
118 | * Returns the mode used to create directories when requested |
||
119 | * |
||
120 | * @return integer |
||
121 | */ |
||
122 | 41 | public function getDirectoryCreationMode() |
|
126 | |||
127 | /** |
||
128 | * Sets the mode used to create directories when requested |
||
129 | * |
||
130 | * @param integer $directoryCreationMode The mode, e.g. 755 |
||
131 | * @return RepositoryInterface |
||
132 | */ |
||
133 | public function setDirectoryCreationMode($directoryCreationMode) |
||
138 | |||
139 | /** |
||
140 | * Returns the author used when committing changes |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | 100 | public function getAuthor() |
|
148 | |||
149 | /** |
||
150 | * Sets the author used when committing changes |
||
151 | * |
||
152 | * @param string $author The author used when committing changes |
||
153 | * @return RepositoryInterface |
||
154 | */ |
||
155 | public function setAuthor($author) |
||
160 | |||
161 | /** |
||
162 | * Resolves an absolute path into a path relative to the repository path |
||
163 | * |
||
164 | * @param string|array $path A file system path (or an array of paths) |
||
165 | * @return string|array Either a single path or an array of paths is returned |
||
166 | */ |
||
167 | 133 | public function resolveLocalPath($path) |
|
183 | |||
184 | /** |
||
185 | * Resolves a path relative to the repository into an absolute path |
||
186 | * |
||
187 | * @param string|array $path A local path (or an array of paths) |
||
188 | * @return string|array Either a single path or an array of paths is returned |
||
189 | */ |
||
190 | 159 | public function resolveFullPath($path) |
|
207 | |||
208 | /** |
||
209 | * Runs $function in a transactional scope committing all changes to the repository on success, |
||
210 | * but rolling back all changes in the event of an Exception being thrown in the closure |
||
211 | * |
||
212 | * The closure $function will be called with a {@see TQ\Vcs\Repository\Transaction} as its only argument |
||
213 | * |
||
214 | * @param \Closure $function The callback used inside the transaction |
||
215 | * @return Transaction |
||
216 | * @throws \Exception Rethrows every exception happening inside the transaction |
||
217 | */ |
||
218 | 8 | public function transactional(\Closure $function) |
|
245 | } |
||
246 | |||
247 |
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.