Skip to content

Commit

Permalink
Create an AbstractFactory and AbstractObject
Browse files Browse the repository at this point in the history
References #8, but does not close it, because refactoring needs to be done all around.
  • Loading branch information
potofcoffee committed Jan 17, 2019
1 parent f824e2c commit bdd6f6a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Classes/AbstractFactory.php
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;
}

}
42 changes: 42 additions & 0 deletions Classes/AbstractObject.php
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]
);
}

}

0 comments on commit bdd6f6a

Please sign in to comment.