Skip to content

Commit 492f0e4

Browse files
committed
initial commit
1 parent 7e4852d commit 492f0e4

File tree

65 files changed

+4121
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+4121
-0
lines changed

Command/IndexEntitiesCommand.php

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
3+
namespace Mdiyakov\DoctrineSolrBundle\Command;
4+
5+
use Doctrine\ORM\EntityManager;
6+
use Doctrine\ORM\EntityRepository;
7+
use Symfony\Component\Console\Input\InputArgument;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
use Symfony\Component\Console\Command\Command;
11+
use Mdiyakov\DoctrineSolrBundle\Config\Config;
12+
use Mdiyakov\DoctrineSolrBundle\Manager\IndexProcessManager;
13+
14+
class IndexEntitiesCommand extends Command
15+
{
16+
17+
const BUNCH_COUNT = 100;
18+
19+
/**
20+
* @var EntityManager
21+
*/
22+
private $em;
23+
24+
/**
25+
* @var Config
26+
*/
27+
private $config;
28+
29+
/**
30+
* @var array
31+
*/
32+
private $possibleEntityTypes;
33+
34+
/**
35+
* @var IndexProcessManager
36+
*/
37+
private $indexProcessManager;
38+
39+
/**
40+
* @var OutputInterface
41+
*/
42+
private $output;
43+
44+
/**
45+
* @param Config $config
46+
* @param IndexProcessManager $indexProcessManager
47+
* @param EntityManager $em
48+
*/
49+
public function __construct(Config $config, IndexProcessManager $indexProcessManager, EntityManager $em)
50+
{
51+
$this->config = $config;
52+
$this->indexProcessManager = $indexProcessManager;
53+
$this->possibleEntityTypes = array_keys($this->getAssocEntitiesClasses());
54+
$this->em = $em;
55+
56+
parent::__construct();
57+
}
58+
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
protected function configure()
64+
{
65+
$this
66+
->setName('mdiyakov:doctrine-solr:index')
67+
->addArgument(
68+
'entity-type',
69+
InputArgument::OPTIONAL,
70+
'Specify type of entity to be indexed. Possible values are "all", "' . join('","', $this->possibleEntityTypes),
71+
'all'
72+
)
73+
->addArgument(
74+
'id',
75+
InputArgument::OPTIONAL,
76+
'Specify id of entity to be indexed. Value must be integer. Also entity-type must be specify'
77+
)
78+
;
79+
}
80+
81+
/**
82+
* {@inheritdoc}
83+
*/
84+
protected function execute(InputInterface $input, OutputInterface $output)
85+
{
86+
$this->output = $output;
87+
$entityType = $input->getArgument('entity-type');
88+
$entityId = (int) $input->getArgument('id');
89+
90+
if ($entityType == 'all') {
91+
foreach ($this->config->getIndexedClasses() as $entityClass) {
92+
$this->indexEntityClass($entityClass);
93+
}
94+
} else {
95+
$entitiesClasses = $this->getAssocEntitiesClasses();
96+
if (!array_key_exists($entityType, $entitiesClasses )) {
97+
throw new \Exception('There is no such possible entity-type. Check help section for possible values');
98+
}
99+
$entityClass = $entitiesClasses[$entityType];
100+
$this->indexEntityClass($entityClass, $entityId);
101+
}
102+
}
103+
104+
/**
105+
* @param string $entityClass
106+
* @param int|null $id
107+
* @throws \Exception
108+
*/
109+
private function indexEntityClass($entityClass, $id = null)
110+
{
111+
$repository = $this->em->getRepository($entityClass);
112+
if ($id) {
113+
$entity = $repository->find($id);
114+
if (!$entity) {
115+
throw new \Exception(
116+
sprintf('There is no %s with id %s in database', $entityClass, $id)
117+
);
118+
}
119+
$this->processEntity($entity);
120+
} else {
121+
$this->processRepository($repository);
122+
}
123+
}
124+
125+
/**
126+
* @param EntityRepository $repository
127+
*/
128+
private function processRepository(EntityRepository $repository)
129+
{
130+
$offset = 0;
131+
while ($entities = $repository->findBy([],['id'=> 'asc'], self::BUNCH_COUNT, $offset)) {
132+
foreach ($entities as $entity) {
133+
$this->processEntity($entity);
134+
}
135+
$this->em->clear($repository->getClassName());
136+
$offset += self::BUNCH_COUNT;
137+
}
138+
}
139+
140+
/**
141+
* @param $entity
142+
*/
143+
private function processEntity($entity)
144+
{
145+
$result = $this->indexProcessManager->reindex($entity);
146+
$status = $result->isSuccess() ? 'successfully' : 'failed. Error ' . $result->getError();
147+
$message = sprintf('Indexing of %s with id %s is %s',
148+
get_class($entity),
149+
$entity->getId(),
150+
$status
151+
);
152+
$this->output->writeln($message);
153+
}
154+
155+
/**
156+
* @return string[]
157+
*/
158+
private function getAssocEntitiesClasses()
159+
{
160+
$entitiesConfigs = $this->config->getIndexedEntities();
161+
162+
$result = [];
163+
foreach ($entitiesConfigs as $entityKey => $entityConfig) {
164+
$result[$entityKey] = $entityConfig['class'];
165+
}
166+
167+
return $result;
168+
}
169+
}

Config/Config.php

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
namespace Mdiyakov\DoctrineSolrBundle\Config;
4+
5+
use Mdiyakov\DoctrineSolrBundle\Schema\Schema;
6+
7+
class Config
8+
{
9+
10+
/**
11+
* @var \string[][]
12+
*/
13+
private $indexedEntities;
14+
15+
/**
16+
* @var Schema[]
17+
*/
18+
private $entitySchemaMap = [];
19+
20+
/**
21+
* @var string[]
22+
*/
23+
private $entitiesClasses = [];
24+
25+
/**
26+
* @var Schema[]
27+
*/
28+
private $schemes = [];
29+
30+
/**
31+
* @var array|\string[][]
32+
*/
33+
private $filters = [];
34+
35+
/**
36+
* @var array
37+
*/
38+
private $entitiesConfigMap = [];
39+
40+
/**
41+
* @param string[][] $indexedEntities
42+
* @param string[][] $schemes
43+
* @param string[][] $filters
44+
* @param string[] $solariumClients
45+
*/
46+
public function __construct($indexedEntities, $schemes, $filters, $solariumClients)
47+
{
48+
$configValidator = new ConfigValidator();
49+
foreach ($indexedEntities as $entityConfig) {
50+
$schemaName = $entityConfig['schema'];
51+
$configValidator->validate($entityConfig, $schemes, $filters, $solariumClients);
52+
53+
/** todo should be moved to some SchemaProvider class */
54+
if (!isset($this->schemes[$schemaName])) {
55+
$schema = new Schema(
56+
$schemaName,
57+
$schemes[$schemaName]['client'],
58+
$schemes[$schemaName]['document_unique_field'],
59+
$schemes[$schemaName]['fields'],
60+
$schemes[$schemaName]['config_entity_fields']
61+
);
62+
$this->schemes[$schemaName] = $schema;
63+
}
64+
65+
$this->entitySchemaMap[$entityConfig['class']] = $schema;
66+
$this->entitiesClasses[$entityConfig['class']] = true;
67+
$this->entitiesConfigMap[$entityConfig['class']] = $entityConfig;
68+
}
69+
70+
$this->filters = $filters;
71+
$this->indexedEntities = $indexedEntities;
72+
}
73+
74+
/**
75+
* @param string $class
76+
* @return Schema|null
77+
*/
78+
public function getSchemaByEntityClass($class)
79+
{
80+
if (array_key_exists($class, $this->entitySchemaMap)) {
81+
return $this->entitySchemaMap[$class];
82+
}
83+
84+
return null;
85+
}
86+
87+
/**
88+
* @param string $class
89+
* @return string|null
90+
*
91+
*/
92+
public function getClientByEntityClass($class)
93+
{
94+
return array_key_exists($class, $this->entityClientMap) ? $this->entityClientMap[$class] : null;
95+
}
96+
97+
/**
98+
* @param $class
99+
* @return null|\string[][]
100+
*/
101+
public function getEntityConfig($class)
102+
{
103+
return array_key_exists($class, $this->entitiesConfigMap) ? $this->entitiesConfigMap[$class] : null;
104+
}
105+
106+
/**
107+
* @return string[]
108+
*/
109+
public function getIndexedClasses()
110+
{
111+
return array_keys($this->entitiesClasses);
112+
}
113+
114+
/**
115+
* @return array
116+
*/
117+
public function getIndexedEntities()
118+
{
119+
return $this->indexedEntities;
120+
}
121+
122+
/**
123+
* @return array|\string[][]
124+
*/
125+
public function getFilters()
126+
{
127+
return $this->filters;
128+
}
129+
}

0 commit comments

Comments
 (0)