]> git.mxchange.org Git - core.git/blob - framework/main/classes/factories/stacks/class_FileStackFactory.php
Continued:
[core.git] / framework / main / classes / factories / stacks / class_FileStackFactory.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Factory\Stack;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Factory\BaseFactory;
8 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
9 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
10 use Org\Mxchange\CoreFramework\Registry\Object\ObjectRegistry;
11
12 // Import SPL stuff
13 use \InvalidArgumentException;
14 use \SplFileInfo;
15
16 /**
17  * A factory class for file-based stacks
18  *
19  * @author              Roland Haeder <webmaster@ship-simu.org>
20  * @version             0.0.0
21  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
22  * @license             GNU GPL 3.0 or any newer version
23  * @link                http://www.ship-simu.org
24  *
25  * This program is free software: you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation, either version 3 of the License, or
28  * (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program. If not, see <http://www.gnu.org/licenses/>.
37  */
38 class FileStackFactory extends BaseFactory {
39         /**
40          * Protected constructor
41          *
42          * @return      void
43          */
44         private function __construct () {
45                 // Call parent constructor
46                 parent::__construct(__CLASS__);
47         }
48
49         /**
50          * Returns a singleton (registry-based) StackableFile instance
51          *
52          * @param       $prefix                         Class prefix
53          * @param       $stackName                      Name of the stack
54          * @return      $stackInstance          An instance of a StackableFile class
55          */
56         public static final function createFileStackInstance (string $prefix, string $stackName) {
57                 // Validate parameter
58                 //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-FACTORY: prefix=%s,stackName=%s - CALLED!', $prefix, $stackName));
59                 if (empty($prefix)) {
60                         // Throw IAE
61                         throw new InvalidArgumentException('Parameter "prefix" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
62                 } elseif (empty($stackName)) {
63                         // Throw it again
64                         throw new InvalidArgumentException('Paramter "stackName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
65                 }
66
67                 // Construct file stack name
68                 $fileInfoInstance = new SplFileInfo(sprintf('%s%s/%s.%s',
69                         FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('root_base_path'),
70                         FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('base_file_stacks_path'),
71                         $stackName,
72                         FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('file_stacks_extension')
73                 ));
74
75                 // If there is no handler?
76                 if (ObjectRegistry::getRegistry('factory')->instanceExists($stackName . '_stack')) {
77                         // Get handler from registry
78                         $stackInstance = ObjectRegistry::getRegistry('factory')->getInstance($stackName . '_stack');
79                 } else {
80                         // Get the handler instance
81                         $stackInstance = ObjectFactory::createObjectByConfiguredName($prefix . '_' . $stackName . '_stack_class', array($fileInfoInstance, $prefix . '_' . $stackName));
82
83                         // Add it to the registry
84                         ObjectRegistry::getRegistry('factory')->addInstance($stackName . '_stack', $stackInstance);
85                 }
86
87                 // Return the instance
88                 //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-FACTORY: stackInstance=%s - EXIT!', $stackInstance->__toString()));
89                 return $stackInstance;
90         }
91
92 }