|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Slim3 Doctrine integration (https://github.com/juliangut/slim-doctrine) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/juliangut/slim-doctrine for the canonical source repository |
|
6
|
|
|
* |
|
7
|
|
|
* @license https://raw.githubusercontent.com/juliangut/slim-doctrine/master/LICENSE |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Jgut\Slim\Doctrine; |
|
11
|
|
|
|
|
12
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
13
|
|
|
use Doctrine\ODM\MongoDB\Tools\Console\Helper\DocumentManagerHelper; |
|
14
|
|
|
use Doctrine\ORM\EntityManager; |
|
15
|
|
|
use Doctrine\ORM\Tools\Console\ConsoleRunner; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Doctrine CLI application builder. |
|
19
|
|
|
*/ |
|
20
|
|
|
class CLIApplicationBuilder |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Create a Doctrine CLI application. |
|
24
|
|
|
* |
|
25
|
|
|
* @param array|\Doctrine\ORM\EntityManager $entityManager |
|
26
|
|
|
* @param array|\Doctrine\ODM\MongoDB\DocumentManager $documentManager |
|
|
|
|
|
|
27
|
|
|
* |
|
28
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
29
|
|
|
* @throws \Doctrine\ORM\ORMException |
|
30
|
|
|
* @throws \InvalidArgumentException |
|
31
|
|
|
* @throws \LogicException |
|
32
|
|
|
* @throws \RuntimeException |
|
33
|
|
|
* |
|
34
|
|
|
* @return \Symfony\Component\Console\Application |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function build($entityManager, $documentManager = null) |
|
37
|
|
|
{ |
|
38
|
|
|
$entityManager = self::getEntityManager($entityManager); |
|
39
|
|
|
|
|
40
|
|
|
$helperSet = ConsoleRunner::createHelperSet($entityManager); |
|
41
|
|
|
$application = ConsoleRunner::createApplication($helperSet); |
|
42
|
|
|
|
|
43
|
|
|
if ($documentManager !== null) { |
|
44
|
|
|
$documentManager = self::getDocumentManager($documentManager); |
|
45
|
|
|
|
|
46
|
|
|
$helperSet->set(new DocumentManagerHelper($documentManager), 'dm'); |
|
47
|
|
|
|
|
48
|
|
|
$application->addCommands( |
|
49
|
|
|
[ |
|
50
|
|
|
new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateDocumentsCommand(), |
|
51
|
|
|
new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateHydratorsCommand(), |
|
52
|
|
|
new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateProxiesCommand(), |
|
53
|
|
|
new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateRepositoriesCommand(), |
|
54
|
|
|
new \Doctrine\ODM\MongoDB\Tools\Console\Command\QueryCommand(), |
|
55
|
|
|
new \Doctrine\ODM\MongoDB\Tools\Console\Command\ClearCache\MetadataCommand(), |
|
56
|
|
|
new \Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\CreateCommand(), |
|
57
|
|
|
new \Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\DropCommand(), |
|
58
|
|
|
new \Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\UpdateCommand(), |
|
59
|
|
|
] |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $application; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Retrieve entity manager. |
|
68
|
|
|
* |
|
69
|
|
|
* @param array|\Doctrine\ORM\EntityManager $entityManager |
|
70
|
|
|
* |
|
71
|
|
|
* @throws \InvalidArgumentException |
|
72
|
|
|
* |
|
73
|
|
|
* @return \Doctrine\ORM\EntityManager |
|
74
|
|
|
*/ |
|
75
|
|
|
protected static function getEntityManager($entityManager) |
|
76
|
|
|
{ |
|
77
|
|
|
if (is_array($entityManager)) { |
|
78
|
|
|
$entityManager = EntityManagerBuilder::build($entityManager); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (!$entityManager instanceof EntityManager) { |
|
82
|
|
|
throw new \InvalidArgumentException('Invalid Entity Manager provided'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $entityManager; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Retrieve document manager. |
|
90
|
|
|
* |
|
91
|
|
|
* @param array|\Doctrine\ODM\MongoDB\DocumentManager $documentManager |
|
92
|
|
|
* |
|
93
|
|
|
* @throws \InvalidArgumentException |
|
94
|
|
|
* |
|
95
|
|
|
* @return \Doctrine\ODM\MongoDB\DocumentManager |
|
96
|
|
|
*/ |
|
97
|
|
|
protected static function getDocumentManager($documentManager) |
|
98
|
|
|
{ |
|
99
|
|
|
if (is_array($documentManager)) { |
|
100
|
|
|
$documentManager = DocumentManagerBuilder::build($documentManager); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if (!$documentManager instanceof DocumentManager) { |
|
104
|
|
|
throw new \InvalidArgumentException('Invalid Document Manager provided'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $documentManager; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
arrayand suggests a stricter type likearray<String>.Most often this is a case of a parameter that can be null in addition to its declared types.