3 namespace Org\Mxchange\CoreFramework\Factory;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
9 use \InvalidArgumentException;
12 * A general (base) factory
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 abstract class BaseFactory extends BaseFrameworkSystem {
35 * Total objects generated
37 private static $total = 0;
40 * Counter of all objects
42 private static $objectCounters = [];
45 * Protected constructor
47 * @param $className Name of the real class (not BaseFactory)
50 protected function __construct (string $className) {
51 // Call parent constructor
52 parent::__construct($className);
58 * @param $fullClassName Name of the class we shall count
60 * @throws InvalidArgumentException If a parameter is not valid
62 protected static final function countObject (string $fullClassName) {
63 // Is the parameter valid?
64 if (empty($fullClassName)) {
65 // No empty class name
66 throw new InvalidArgumentException('fullClassName is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
69 // Count it up in total sum
72 // Do we have an entry?
73 if (!self::isClassCounted($fullClassName)) {
74 // No, then generate one
75 self::$objectCounters[$fullClassName] = 0;
79 //* NOISY-DEBUG: */ print __METHOD__.': className=' .$fullClassName . PHP_EOL;
80 self::$objectCounters[$fullClassName]++;
84 * Static getter for total object count
86 * @return $total Total amount of generated objects
88 public static final function getTotal () {
93 * Static getter for all object counters
95 * @return $objectCounters An array with all object counters
97 public static final function getAllCounters () {
98 return self::$objectCounters;
102 * Checks whether given full class name is already counted
104 * @param $fullClassName Full name of class
105 * @return $isCounted Whether given class name is counted
106 * @throws InvalidArgumentException If a parameter is not valid
108 public static final function isClassCounted (string $fullClassName) {
109 // Is the parameter valid?
110 if (empty($fullClassName)) {
111 // No empty class name
112 throw new InvalidArgumentException('fullClassName is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
115 // Return isset() result
116 return isset(self::$objectCounters[$fullClassName]);