Completed
Push — master ( 5ae395...f2deb1 )
by
unknown
14:39
created

ObjectManager::getScope()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace TYPO3\CMS\Extbase\Object;
5
6
/*
7
 * This file is part of the TYPO3 CMS project.
8
 *
9
 * It is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU General Public License, either version 2
11
 * of the License, or any later version.
12
 *
13
 * For the full copyright and license information, please read the
14
 * LICENSE.txt file that was distributed with this source code.
15
 *
16
 * The TYPO3 project - inspiring people to share!
17
 */
18
19
use Psr\Container\ContainerInterface;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Extbase\Object\Container\Container as ExtbaseContainer;
22
23
/**
24
 * Implementation of the default Extbase Object Manager
25
 */
26
class ObjectManager implements ObjectManagerInterface
27
{
28
    /**
29
     * @var ContainerInterface
30
     */
31
    private $container;
32
33
    /**
34
     * @var ExtbaseContainer
35
     */
36
    protected $objectContainer;
37
38
    /**
39
     * Constructs a new Object Manager
40
     *
41
     * @param ContainerInterface $container
42
     * @param ExtbaseContainer $objectContainer
43
     */
44
    public function __construct(ContainerInterface $container, ExtbaseContainer $objectContainer)
45
    {
46
        $this->container = $container;
47
        $this->objectContainer = $objectContainer;
48
    }
49
50
    /**
51
     * Serialization (sleep) helper.
52
     *
53
     * Removes properties of this object from serialization.
54
     * This action is necessary, since there might be closures used
55
     * in the accordant content objects (e.g. in FLUIDTEMPLATE) which
56
     * cannot be serialized. It's fine to reset $this->contentObjects
57
     * since elements will be recreated and are just a local cache,
58
     * but not required for runtime logic and behaviour.
59
     *
60
     * @see http://forge.typo3.org/issues/36820
61
     * @return array Names of the properties to be serialized
62
     * @internal only to be used within Extbase, not part of TYPO3 Core API.
63
     */
64
    public function __sleep()
65
    {
66
        // Use get_objects_vars() instead of
67
        // a much more expensive Reflection:
68
        $properties = get_object_vars($this);
69
        unset($properties['objectContainer']);
70
        return array_keys($properties);
71
    }
72
73
    /**
74
     * Unserialization (wakeup) helper.
75
     *
76
     * Initializes the properties again that have been removed by
77
     * a call to the __sleep() method on serialization before.
78
     *
79
     * @see http://forge.typo3.org/issues/36820
80
     * @internal only to be used within Extbase, not part of TYPO3 Core API.
81
     */
82
    public function __wakeup()
83
    {
84
        $this->__construct(
85
            GeneralUtility::getContainer(),
86
            GeneralUtility::getContainer()->get(ExtbaseContainer::class)
87
        );
88
    }
89
90
    /**
91
     * Returns a fresh or existing instance of the object specified by $objectName.
92
     *
93
     * @param string $objectName The name of the object to return an instance of
94
     * @param array<int,mixed> $constructorArguments
95
     * @return object The object instance
96
     */
97
    public function get(string $objectName, ...$constructorArguments): object
98
    {
99
        if ($objectName === \DateTime::class) {
100
            return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($objectName, ...$constructorArguments);
101
        }
102
103
        if ($this->container->has($objectName)) {
104
            if ($constructorArguments === []) {
105
                $instance = $this->container->get($objectName);
106
                if (!is_object($instance)) {
107
                    throw new \TYPO3\CMS\Extbase\Object\Exception('Invalid object name "' . $objectName . '". The PSR-11 container entry resolves to a non object.', 1562357346);
108
                }
109
                return $instance;
110
            }
111
            trigger_error($objectName . ' is available in the PSR-11 container. That means you should not try to instanciate it using constructor arguments. Falling back to legacy extbase based injection.', E_USER_DEPRECATED);
112
        }
113
114
        return $this->objectContainer->getInstance($objectName, $constructorArguments);
115
    }
116
117
    /**
118
     * Create an instance of $className without calling its constructor
119
     *
120
     * @param string $className
121
     * @return object
122
     */
123
    public function getEmptyObject(string $className): object
124
    {
125
        return $this->objectContainer->getEmptyObject($className);
126
    }
127
}
128