Completed
Pull Request — master (#13)
by
unknown
03:10
created

AbstractAssetHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/*
3
 * 2016 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 Formz project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\AssetHandler;
15
16
use Romm\Formz\Configuration\Form\Form;
17
use Romm\Formz\Core\Core;
18
use Romm\Formz\Form\FormObject;
19
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
20
21
/**
22
 * Abstract class which must be inherited by an asset handler.
23
 *
24
 * An asset handler is a helper for getting useful information for a given
25
 * language.
26
 */
27
abstract class AbstractAssetHandler
28
{
29
30
    /**
31
     * @var AssetHandlerFactory
32
     */
33
    protected $assetHandlerFactory;
34
35
    /**
36
     * @var ObjectManagerInterface
37
     */
38
    protected $objectManager;
39
40
    /**
41
     * @var AbstractAssetHandler[]
42
     */
43
    protected static $instances = [];
44
45
    /**
46
     * Constructor, will set up variables.
47
     *
48
     * @param AssetHandlerFactory $assetHandlerFactory
49
     */
50
    public function __construct(AssetHandlerFactory $assetHandlerFactory)
51
    {
52
        $this->assetHandlerFactory = $assetHandlerFactory;
53
        $this->objectManager = Core::get()->getObjectManager();
54
    }
55
56
    /**
57
     * Use this function to instantiate a new instance of the class which calls
58
     * the function. The instance is then directly usable.
59
     *
60
     * Example:
61
     * `MyAssetHandler::with($assetHandlerFactory)->doSomeStuff();`
62
     *
63
     * @param AssetHandlerFactory $assetHandlerFactory
64
     * @return $this
65
     */
66
    public static function with(AssetHandlerFactory $assetHandlerFactory)
67
    {
68
        $hash = spl_object_hash($assetHandlerFactory);
69
        $className = get_called_class();
70
71
        if (false === isset(self::$instances[$hash])) {
72
            self::$instances[$hash] = [];
73
        }
74
75
        if (false === isset(self::$instances[$hash][$className])) {
76
            /** @noinspection PhpMethodParametersCountMismatchInspection */
77
            self::$instances[$hash][$className] = Core::get()->getObjectManager()
78
                ->get($className, $assetHandlerFactory);
79
        }
80
81
        return self::$instances[$hash][$className];
82
    }
83
84
    /**
85
     * Just an alias to get the form object faster.
86
     *
87
     * @return FormObject
88
     */
89
    public function getFormObject()
90
    {
91
        return $this->assetHandlerFactory->getFormObject();
92
    }
93
94
    /**
95
     * Just an alias to get the form configuration faster.
96
     *
97
     * @return Form
98
     */
99
    public function getFormConfiguration()
100
    {
101
        return $this->getFormObject()->getConfiguration();
102
    }
103
}
104