Continued:
[core.git] / framework / main / classes / factories / index / class_FileStackIndexFactory.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Factory\Stack\File;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\BaseFactory;
7 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
9 use Org\Mxchange\CoreFramework\Registry\Object\ObjectRegistry;
10
11 // Import SPL stuff
12 use \InvalidArgumentException;
13 use \SplFileInfo;
14
15 /**
16  * A factory class for file-based stack indexes
17  *
18  * @author              Roland Haeder <webmaster@ship-simu.org>
19  * @version             0.0.0
20  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 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 FileStackIndexFactory 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       $fileInfoInstance       An instance of a SplFileInfo class
52          * @return      $indexInstance  An instance of a IndexableStack class
53          * @throws      InvalidArgumentException        If a parameter is invalid
54          */
55         public static final function createFileStackIndexInstance (SplFileInfo $fileInfoInstance, string $type) {
56                 // If there is no handler?
57                 if (empty($type)) {
58                         // Throw IAE
59                         throw new InvalidArgumentException('Parameter "type" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
60                 } elseif (ObjectRegistry::getRegistry('factory')->instanceExists($type . '_index')) {
61                         // Get handler from registry
62                         $indexInstance = ObjectRegistry::getRegistry('factory')->getInstance($type . '_index');
63                 } else {
64                         // Get the handler instance
65                         $indexInstance = ObjectFactory::createObjectByConfiguredName($type . '_file_stack_index_class', [$fileInfoInstance]);
66
67                         // Add it to the registry
68                         ObjectRegistry::getRegistry('factory')->addInstance($type . '_index', $indexInstance);
69                 }
70
71                 // Return the instance
72                 return $indexInstance;
73         }
74
75 }