-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create an AbstractFactory and AbstractObject
References #8, but does not close it, because refactoring needs to be done all around.
- Loading branch information
1 parent
f824e2c
commit bdd6f6a
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
/* | ||
* PULPIT | ||
* A sermon plugin for WordPress | ||
* | ||
* Copyright (c) 2017 Christoph Fischer, http://www.peregrinus.de | ||
* Author: Christoph Fischer, chris@toph.de | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace Peregrinus\Pulpit; | ||
|
||
class AbstractFactory | ||
{ | ||
|
||
/** | ||
* Get objects of all classes of this type | ||
* @return array Instances of each class | ||
*/ | ||
public static function getAll() | ||
{ | ||
$reflection = new \ReflectionClass(get_called_class()); | ||
$filePath = pathinfo($reflection->getFileName(), PATHINFO_DIRNAME); | ||
$rawClassPath = str_replace('\\', '/', get_called_class()); | ||
$classPath = str_replace('/', '\\', pathinfo($rawClassPath, PATHINFO_DIRNAME)); | ||
$singularName = str_replace('Factory', '', pathinfo($rawClassPath, PATHINFO_FILENAME)); | ||
|
||
foreach (glob($filePath.'/*'.$singularName.'.*') as $class) { | ||
$baseClass = pathinfo($class, PATHINFO_FILENAME); | ||
$class = $classPath.'\\' . $baseClass; | ||
if (substr($baseClass, 0, 8) !== 'Abstract') { | ||
$objects[] = new $class(); | ||
} | ||
} | ||
return $objects; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/* | ||
* PULPIT | ||
* A sermon plugin for WordPress | ||
* | ||
* Copyright (c) 2019 Christoph Fischer, http://www.peregrinus.de | ||
* Author: Christoph Fischer, chris@toph.de | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace Peregrinus\Pulpit; | ||
|
||
use Peregrinus\Pulpit\Utility\StringUtility; | ||
|
||
class AbstractObject | ||
{ | ||
|
||
/** | ||
* @return string this object's key | ||
*/ | ||
public function getKey(): string { | ||
$classPath = explode('\\', get_called_class()); | ||
return str_replace( | ||
substr($classPath[count($classPath)-2],0, -1), | ||
'', | ||
$classPath[count($classPath)-1] | ||
); | ||
} | ||
|
||
} |