Completed
Push — cleanup-service ( 73e309...ffbb72 )
by Romain
02:47
created

FormObjectFactory::injectConfigurationFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/*
3
 * 2017 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\Form;
15
16
use Romm\Formz\Configuration\Configuration;
17
use Romm\Formz\Configuration\ConfigurationFactory;
18
use Romm\Formz\Core\Core;
19
use Romm\Formz\Exceptions\ClassNotFoundException;
20
use Romm\Formz\Service\TypoScriptService;
21
use TYPO3\CMS\Core\SingletonInterface;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Extbase\Reflection\ReflectionService;
24
25
/**
26
 * Factory class which will manage instances of `FormObject`.
27
 */
28
class FormObjectFactory implements SingletonInterface
29
{
30
    const IGNORE_PROPERTY = 'formz-ignore';
31
32
    /**
33
     * @var ConfigurationFactory
34
     */
35
    protected $configurationFactory;
36
37
    /**
38
     * @var TypoScriptService
39
     */
40
    protected $typoScriptService;
41
42
    /**
43
     * @var FormObject[]
44
     */
45
    protected $instances = [];
46
47
    /**
48
     * @var array
49
     */
50
    private static $ignoredProperties = ['validationData', 'uid', 'pid', '_localizedUid', '_languageUid', '_versionedUid'];
51
52
    /**
53
     * Will create an instance of `FormObject` based on a class which implements
54
     * the interface `FormInterface`.
55
     *
56
     * @param string $className
57
     * @param string $name
58
     * @return FormObject
59
     * @throws \Exception
60
     */
61
    public function getInstanceFromClassName($className, $name)
62
    {
63
        if (false === class_exists($className)
64
            || false === in_array(FormInterface::class, class_implements($className))
65
        ) {
66
            throw new ClassNotFoundException(
67
                'Invalid class name given: "' . $className . '"; the class must be an instance of "' . FormInterface::class . '".',
68
                1467191011
69
            );
70
        }
71
72
        $cacheIdentifier = Core::get()->getCacheIdentifier('form-object-', $className . '-' . $name);
73
74
        if (false === isset($this->instances[$cacheIdentifier])) {
75
            $cacheInstance = Core::get()->getCacheInstance();
76
77
            if ($cacheInstance->has($cacheIdentifier)) {
78
                $instance = $cacheInstance->get($cacheIdentifier);
79
            } else {
80
                $instance = $this->createInstance($className, $name);
81
                $cacheInstance->set($cacheIdentifier, $instance);
82
            }
83
84
            /** @var Configuration $formzConfigurationObject */
85
            $formzConfigurationObject = $this->configurationFactory
86
                ->getFormzConfiguration()
87
                ->getObject(true);
88
89
            if (false === $formzConfigurationObject->hasForm($instance->getClassName(), $instance->getName())) {
90
                $formzConfigurationObject->addForm($instance);
91
            }
92
93
            $this->instances[$cacheIdentifier] = $instance;
94
        }
95
96
        return $this->instances[$cacheIdentifier];
97
    }
98
99
    /**
100
     * Creates and initializes a new `FormObject` instance.
101
     *
102
     * @param string $className
103
     * @param string $name
104
     * @return FormObject
105
     */
106
    protected function createInstance($className, $name)
107
    {
108
        /** @var FormObject $instance */
109
        $instance = Core::instantiate(FormObject::class, $className, $name);
110
111
        $this->insertObjectProperties($instance);
112
113
        $formConfiguration = $this->typoScriptService->getFormConfiguration($className);
114
        $instance->setConfigurationArray($formConfiguration);
115
116
        $instance->getHash();
117
118
        return $instance;
119
    }
120
121
    /**
122
     * Will insert all the accessible properties of the given instance.
123
     *
124
     * @param FormObject $instance
125
     */
126
    protected function insertObjectProperties(FormObject $instance)
127
    {
128
        $className = $instance->getClassName();
129
130
        /** @var ReflectionService $reflectionService */
131
        $reflectionService = GeneralUtility::makeInstance(ReflectionService::class);
132
        $reflectionProperties = $reflectionService->getClassPropertyNames($className);
133
134
        $classReflection = new \ReflectionClass($className);
135
        $publicProperties = $classReflection->getProperties(\ReflectionProperty::IS_PUBLIC);
136
137
        foreach ($reflectionProperties as $property) {
138
            if (false === in_array($property, self::$ignoredProperties)
139
                && false === $reflectionService->isPropertyTaggedWith($className, $property, self::IGNORE_PROPERTY)
140
                && ((true === in_array($property, $publicProperties))
141
                    || $reflectionService->hasMethod($className, 'get' . ucfirst($property))
142
                )
143
            ) {
144
                $instance->addProperty($property);
145
            }
146
        }
147
148
        unset($publicProperties);
149
    }
150
151
    /**
152
     * @param ConfigurationFactory $configurationFactory
153
     */
154
    public function injectConfigurationFactory(ConfigurationFactory $configurationFactory)
155
    {
156
        $this->configurationFactory = $configurationFactory;
157
    }
158
159
    /**
160
     * @param TypoScriptService $typoScriptService
161
     */
162
    public function injectTypoScriptService(TypoScriptService $typoScriptService)
163
    {
164
        $this->typoScriptService = $typoScriptService;
165
    }
166
}
167