1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JhFlexiTimeTest\Util; |
4
|
|
|
|
5
|
|
|
use Zend\Mvc\Service\ServiceManagerConfig; |
6
|
|
|
use Zend\ServiceManager\ServiceManager; |
7
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
8
|
|
|
|
9
|
|
|
use Doctrine\Common\DataFixtures\Purger\ORMPurger as FixturePurger; |
10
|
|
|
use Doctrine\Common\DataFixtures\Executor\ORMExecutor as FixtureExecutor; |
11
|
|
|
|
12
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
13
|
|
|
use Doctrine\DBAL\Types\Type; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Base test case to be used when a new service manager instance is required |
17
|
|
|
* |
18
|
|
|
* @license MIT |
19
|
|
|
* @link https://github.com/zf-fr/ZfrRest |
20
|
|
|
* @author Marco Pivetta <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
abstract class ServiceManagerFactory |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private static $config = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @static |
31
|
|
|
* @param array $config |
32
|
|
|
*/ |
33
|
|
|
public static function setApplicationConfig(array $config) |
34
|
|
|
{ |
35
|
|
|
static::$config = $config; |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @static |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public static function getApplicationConfig() |
43
|
|
|
{ |
44
|
|
|
return static::$config; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array|null $config |
49
|
|
|
* @return ServiceManager |
50
|
|
|
*/ |
51
|
|
|
public static function getServiceManager(array $config = null) |
52
|
|
|
{ |
53
|
|
|
$config = $config ?: static::getApplicationConfig(); |
54
|
|
|
|
55
|
|
|
$serviceManager = new ServiceManager( |
56
|
|
|
new ServiceManagerConfig( |
57
|
|
|
isset($config['service_manager']) ? $config['service_manager'] : [] |
58
|
|
|
) |
59
|
|
|
); |
60
|
|
|
$serviceManager->setService('ApplicationConfig', $config); |
61
|
|
|
|
62
|
|
|
/* @var $moduleManager \Zend\ModuleManager\ModuleManagerInterface */ |
63
|
|
|
$moduleManager = $serviceManager->get('ModuleManager'); |
64
|
|
|
|
65
|
|
|
$moduleManager->loadModules(); |
66
|
|
|
|
67
|
|
|
// @todo move to own factory class/add to merged configuration? Create a test module? |
68
|
|
|
$serviceManager->setFactory( |
69
|
|
|
'Doctrine\Common\DataFixtures\Executor\AbstractExecutor', |
70
|
|
|
function (ServiceLocatorInterface $sl) { |
71
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
72
|
|
|
$em = $sl->get('Doctrine\ORM\EntityManager'); |
73
|
|
|
$schemaTool = new SchemaTool($em); |
74
|
|
|
|
75
|
|
|
Type::overrideType('date', 'JhFlexiTime\DBAL\Types\DateType'); |
76
|
|
|
Type::overrideType('time', 'JhFlexiTime\DBAL\Types\TimeType'); |
77
|
|
|
|
78
|
|
|
$schemaTool->dropDatabase(); |
79
|
|
|
$schemaTool->createSchema($em->getMetadataFactory()->getAllMetadata()); |
80
|
|
|
return new FixtureExecutor($em, new FixturePurger($em)); |
81
|
|
|
} |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
return $serviceManager; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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: