Completed
Push — master ( 405deb...3788b5 )
by
unknown
03:31
created

FormObjectFactory::insertObjectProperties()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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