|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Stinger Entity Search package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Oliver Kotte <[email protected]> |
|
7
|
|
|
* (c) Florian Meyer <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace StingerSoft\EntitySearchBundle\Command; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
18
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
19
|
|
|
use StingerSoft\EntitySearchBundle\Services\Mapping\EntityToDocumentMapperInterface; |
|
20
|
|
|
use StingerSoft\EntitySearchBundle\Services\SearchService; |
|
21
|
|
|
use Doctrine\ORM\EntityManager; |
|
22
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
23
|
|
|
|
|
24
|
|
|
class SyncCommand extends ContainerAwareCommand { |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* |
|
28
|
|
|
* @var EntityToDocumentMapperInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $entityToDocumentMapper; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* |
|
34
|
|
|
* @var SearchService |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $searchService; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* |
|
40
|
|
|
* Cache for the default upload path of this platform |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected static $defaultUploadPath = null; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
* |
|
50
|
|
|
* @see \Symfony\Component\Console\Command\Command::configure() |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function configure() { |
|
53
|
|
|
/* @formatter:off */ |
|
54
|
|
|
$this |
|
55
|
|
|
->setName('stinger:search:sync') |
|
56
|
|
|
->addArgument('entity', InputArgument::REQUIRED, 'The entity you want to index') |
|
57
|
|
|
->addOption('source', null, InputArgument::OPTIONAL, 'specify a source from where to load entities [relational, mongodb] (unsupported!)', 'relational') |
|
58
|
|
|
->setDescription('Index all entities'); |
|
59
|
|
|
/* @formatter:on */ |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
* |
|
66
|
|
|
* @see \Symfony\Component\Console\Command\Command::execute() |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
|
69
|
|
|
// Detect upload path |
|
70
|
|
|
if(!self::$defaultUploadPath) { |
|
71
|
|
|
$root = $this->getContainer()->get('kernel')->getRootDir(); |
|
72
|
|
|
self::$defaultUploadPath = $root . '/../web/uploads'; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// Get the entity argument |
|
76
|
|
|
$entity = $input->getArgument('entity'); |
|
77
|
|
|
|
|
78
|
|
|
if($entity == 'all') { |
|
79
|
|
|
/** |
|
80
|
|
|
* @var EntityManager $entityManager |
|
81
|
|
|
*/ |
|
82
|
|
|
$entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); |
|
83
|
|
|
|
|
84
|
|
|
$meta = $entityManager->getMetadataFactory()->getAllMetadata(); |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @var EntityToDocumentMapperInterface $mapper |
|
88
|
|
|
*/ |
|
89
|
|
|
$mapper = $this->getContainer()->get(EntityToDocumentMapperInterface::SERVICE_ID); |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @var ClassMetadata $m |
|
93
|
|
|
*/ |
|
94
|
|
|
foreach($meta as $m) { |
|
95
|
|
|
|
|
96
|
|
|
if($m->getReflectionClass()->isAbstract() || $m->getReflectionClass()->isInterface()) { |
|
97
|
|
|
continue; |
|
98
|
|
|
} |
|
99
|
|
|
if(!$mapper->isClassIndexable($m->getReflectionClass()->getName())) { |
|
100
|
|
|
continue; |
|
101
|
|
|
} |
|
102
|
|
|
$this->indexEntity($input, $output, $m->getReflectionClass()->getName()); |
|
103
|
|
|
} |
|
104
|
|
|
} else { |
|
105
|
|
|
$this->indexEntity($input, $output, $entity); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
protected function indexEntity(InputInterface $input, OutputInterface $output, $entity) { |
|
|
|
|
|
|
110
|
|
|
$output->writeln(sprintf('<comment>Indexing entities of type "%s"</comment>', $entity)); |
|
111
|
|
|
/** |
|
112
|
|
|
* |
|
113
|
|
|
* @var EntityManager $entityManager |
|
114
|
|
|
*/ |
|
115
|
|
|
$entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); |
|
116
|
|
|
$repository = null; |
|
117
|
|
|
try { |
|
118
|
|
|
// Get repository for the given entity type |
|
119
|
|
|
$repository = $entityManager->getRepository($entity); |
|
120
|
|
|
} catch(\Exception $e) { |
|
121
|
|
|
$output->writeln(sprintf('<error>No repository found for "%s", check your input</error>', $entity)); |
|
122
|
|
|
return; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
// Get all entities |
|
126
|
|
|
$entities = $repository->findAll(); |
|
127
|
|
|
if(count($entities) == 0) { |
|
128
|
|
|
$output->writeln('<comment>No entities found for indexing</comment>'); |
|
129
|
|
|
return; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$entitiesIndexed = 0; |
|
133
|
|
|
|
|
134
|
|
|
// Index each entity seperate |
|
135
|
|
|
foreach($entities as $entity) { |
|
136
|
|
|
if($this->getEntityToDocumentMapper()->isIndexable($entity)) { |
|
137
|
|
|
$document = $this->getEntityToDocumentMapper()->createDocument($entityManager, $entity); |
|
138
|
|
|
if($document === false) continue; |
|
139
|
|
|
$this->getSearchService($entityManager)->saveDocument($document); |
|
|
|
|
|
|
140
|
|
|
$entitiesIndexed++; |
|
141
|
|
|
if($entitiesIndexed % 50 == 0) { |
|
142
|
|
|
$entityManager->flush(); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
$entityManager->flush(); |
|
148
|
|
|
$output->writeln('<comment>Indexed ' . $entitiesIndexed . ' entities</comment>'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* |
|
153
|
|
|
* @return EntityToDocumentMapperInterface |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function getEntityToDocumentMapper() { |
|
156
|
|
|
if(!$this->entityToDocumentMapper) { |
|
157
|
|
|
$this->entityToDocumentMapper = $this->getContainer()->get(EntityToDocumentMapperInterface::SERVICE_ID); |
|
158
|
|
|
} |
|
159
|
|
|
return $this->entityToDocumentMapper; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* |
|
164
|
|
|
* @return SearchService |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function getSearchService(ObjectManager $manager) { |
|
167
|
|
|
if(!$this->searchService) { |
|
168
|
|
|
$this->searchService = $this->getContainer()->get(SearchService::SERVICE_ID); |
|
169
|
|
|
} |
|
170
|
|
|
$this->searchService->setObjectManager($manager); |
|
171
|
|
|
return $this->searchService; |
|
172
|
|
|
} |
|
173
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.