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