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
|
|
|
|
22
|
|
|
class SyncCommand extends ContainerAwareCommand { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @var EntityToDocumentMapperInterface |
27
|
|
|
*/ |
28
|
|
|
protected $entityToDocumentMapper; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @var SearchService |
33
|
|
|
*/ |
34
|
|
|
protected $searchService; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* Cache for the default upload path of this platform |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected static $defaultUploadPath = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
* |
48
|
|
|
* @see \Symfony\Component\Console\Command\Command::configure() |
49
|
|
|
*/ |
50
|
|
|
protected function configure() { |
51
|
|
|
/* @formatter:off */ |
52
|
|
|
$this |
53
|
|
|
->setName('stinger:search:sync') |
54
|
|
|
->addArgument('entity', InputArgument::REQUIRED, 'The entity you want to index') |
55
|
|
|
->addOption('source', null, InputArgument::OPTIONAL, 'specify a source from where to load entities [relational, mongodb] (unsupported!)', 'relational') |
56
|
|
|
->setDescription('Index all entities'); |
57
|
|
|
/* @formatter:on */ |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
* |
64
|
|
|
* @see \Symfony\Component\Console\Command\Command::execute() |
65
|
|
|
*/ |
66
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
67
|
|
|
// Detect upload path |
68
|
|
|
if(!self::$defaultUploadPath) { |
69
|
|
|
$root = $this->getContainer()->get('kernel')->getRootDir(); |
70
|
|
|
self::$defaultUploadPath = $root . '/../web/uploads'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Get the entity argument |
74
|
|
|
$entity = $input->getArgument('entity'); |
75
|
|
|
|
76
|
|
|
if($entity == 'all') { |
|
|
|
|
77
|
|
|
// $indexHandler = $this->getIndexHandler(); |
|
|
|
|
78
|
|
|
// $entities = $indexHandler->getSearchableEntities(); |
|
|
|
|
79
|
|
|
// foreach($entities as $bundle => $searchableEntities) { |
|
|
|
|
80
|
|
|
// $output->writeln(sprintf('<comment>Indexing entities for bundle <%s></comment>', $bundle)); |
|
|
|
|
81
|
|
|
// foreach($searchableEntities as $entity => $entityLabel) { |
|
|
|
|
82
|
|
|
// $output->writeln(sprintf('<comment>Indexing entity <%s></comment>', $entityLabel)); |
|
|
|
|
83
|
|
|
// $this->indexEntity($input, $output, $entity); |
|
|
|
|
84
|
|
|
// } |
85
|
|
|
// } |
86
|
|
|
} else { |
87
|
|
|
$this->indexEntity($input, $output, $entity); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function indexEntity(InputInterface $input, OutputInterface $output, $entity) { |
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* |
94
|
|
|
* @var EntityManager $entityManager |
95
|
|
|
*/ |
96
|
|
|
$entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); |
97
|
|
|
$repository = null; |
98
|
|
|
try { |
99
|
|
|
// Get repository for the given entity type |
100
|
|
|
$repository = $entityManager->getRepository($entity); |
101
|
|
|
} catch(\Exception $e) { |
102
|
|
|
$output->writeln(sprintf('<error>No repository found for "%s", check your input</error>', $entity)); |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Get all entities |
107
|
|
|
$entities = $repository->findAll(); |
108
|
|
|
if(count($entities) == 0) { |
109
|
|
|
$output->writeln('<comment>No entities found for indexing</comment>'); |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$entitiesIndexed = 0; |
114
|
|
|
|
115
|
|
|
// Index each entity seperate |
116
|
|
|
foreach($entities as $entity) { |
117
|
|
|
if($this->getEntityToDocumentMapper()->isIndexable($entity)){ |
118
|
|
|
$document = $this->getEntityToDocumentMapper()->createDocument($entityManager, $entity); |
119
|
|
|
$this->getSearchService($entityManager)->saveDocument($document); |
|
|
|
|
120
|
|
|
$entitiesIndexed++; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
$output->writeln('<comment>Indexed ' . $entitiesIndexed . ' entities</comment>'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* |
128
|
|
|
* @return EntityToDocumentMapperInterface |
129
|
|
|
*/ |
130
|
|
|
protected function getEntityToDocumentMapper() { |
131
|
|
|
if(!$this->entityToDocumentMapper) { |
132
|
|
|
$this->entityToDocumentMapper = $this->getContainer()->get(EntityToDocumentMapperInterface::SERVICE_ID); |
133
|
|
|
} |
134
|
|
|
return $this->entityToDocumentMapper; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* |
139
|
|
|
* @return SearchService |
140
|
|
|
*/ |
141
|
|
|
protected function getSearchService(ObjectManager $manager) { |
142
|
|
|
if(!$this->searchService) { |
143
|
|
|
$this->searchService = $this->getContainer()->get(SearchService::SERVICE_ID); |
144
|
|
|
} |
145
|
|
|
$this->searchService->setObjectManager($manager); |
146
|
|
|
return $this->searchService; |
147
|
|
|
} |
148
|
|
|
} |
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.