1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace ZfrCorsTest\Util; |
21
|
|
|
|
22
|
|
|
use Zend\ServiceManager\ServiceManager; |
23
|
|
|
use Zend\Mvc\Service\ServiceManagerConfig; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Base test case to be used when a new service manager instance is required |
27
|
|
|
* |
28
|
|
|
* @license MIT |
29
|
|
|
* @link https://github.com/zf-fr/zfr-cors |
30
|
|
|
* @author Marco Pivetta <[email protected]> |
31
|
|
|
* @author Florent Blaison <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
abstract class ServiceManagerFactory |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private static $config = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @static |
42
|
|
|
* @param array $config |
43
|
|
|
*/ |
44
|
|
|
public static function setApplicationConfig(array $config) |
45
|
|
|
{ |
46
|
|
|
static::$config = $config; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @static |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public static function getApplicationConfig() |
54
|
|
|
{ |
55
|
|
|
return static::$config; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array|null $config |
60
|
|
|
* @return ServiceManager |
61
|
|
|
*/ |
62
|
|
|
public static function getServiceManager(array $config = null) |
63
|
|
|
{ |
64
|
|
|
$config = $config ?: static::getApplicationConfig(); |
65
|
|
|
$serviceManager = new ServiceManager(); |
66
|
|
|
$serviceManagerConfig = new ServiceManagerConfig( |
67
|
|
|
isset($config['service_manager']) ? $config['service_manager'] : [] |
68
|
|
|
); |
69
|
|
|
$serviceManagerConfig->configureServiceManager($serviceManager); |
70
|
|
|
|
71
|
|
|
$serviceManager->setService('ApplicationConfig', $config); |
72
|
|
|
|
73
|
|
|
/* @var $moduleManager \Zend\ModuleManager\ModuleManagerInterface */ |
74
|
|
|
$moduleManager = $serviceManager->get('ModuleManager'); |
75
|
|
|
|
76
|
|
|
$moduleManager->loadModules(); |
77
|
|
|
|
78
|
|
|
return $serviceManager; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: