Skip to content

Commit 0e768ae

Browse files
author
Simonas Šerlinskas
committed
Merge branch '5.0'
# Conflicts: # Tests/app/fixture/TestBundle/Entity/Product.php
2 parents 6efea18 + 48acbd2 commit 0e768ae

File tree

22 files changed

+493
-72
lines changed

22 files changed

+493
-72
lines changed

.travis.yml

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,33 @@
1-
sudo: true
1+
sudo: false
22
language: php
33
php:
44
- 5.6
55
- 7.0
66
- 7.1
7-
- hhvm
7+
- 7.2
88
env:
99
global:
10-
- JAVA_HOME="/usr/lib/jvm/java-8-oracle/jre"
11-
- ES_URL="https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.1.2.zip"
10+
- ES_VERSION=5.6.8 ES_DOWNLOAD_URL=https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${ES_VERSION}.tar.gz
1211
matrix:
1312
- SYMFONY="~2.8"
1413
- SYMFONY="~3.0"
1514
- SYMFONY="~3.1"
1615
- SYMFONY="~3.2"
17-
matrix:
18-
allow_failures:
19-
- php: hhvm
20-
addons:
21-
apt:
22-
packages:
23-
- oracle-java8-installer
16+
- SYMFONY="~3.3"
17+
- SYMFONY="~3.4"
18+
- SYMFONY="~3.5"
19+
- SYMFONY="~3.6"
2420
install:
25-
# Container based PHP image ues PHP 5.6.5, once it will be upgraded sudo will be not necessary
26-
- sudo apt-get install -y oracle-java8-set-default
27-
- curl -L -o elasticsearch.zip $ES_URL
28-
- unzip elasticsearch.zip
29-
- ./elasticsearch-*/bin/elasticsearch -d
21+
- wget ${ES_DOWNLOAD_URL}
22+
- tar -xzf elasticsearch-${ES_VERSION}.tar.gz
23+
- ./elasticsearch-${ES_VERSION}/bin/elasticsearch -d
3024
before_script:
3125
- composer require --no-update symfony/symfony:${SYMFONY}
3226
- composer config -g github-oauth.github.com $GITHUB_COMPOSER_AUTH
3327
- composer install --no-interaction --prefer-dist
3428
script:
35-
- vendor/bin/phpunit --coverage-clover=coveralls.clover
29+
- wget -q --waitretry=1 --retry-connrefused -T 10 -O - http://127.0.0.1:9200
30+
- vendor/bin/phpunit --coverage-clover=coverage.clover
3631
- vendor/bin/phpcs -p --standard=PSR2 --ignore=vendor/,Tests/app/ ./
3732
after_script:
38-
- travis_retry php vendor/bin/coveralls
33+
- travis_retry php vendor/bin/coveralls

Command/DocumentGenerateCommand.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class DocumentGenerateCommand extends AbstractManagerAwareCommand
3232
*/
3333
private $propertyAnnotations;
3434

35+
/**
36+
* @var string[]
37+
*/
38+
private $propertyVisibilities;
39+
3540
/**
3641
* {@inheritdoc}
3742
*/
@@ -165,6 +170,19 @@ protected function interact(InputInterface $input, OutputInterface $output)
165170
continue;
166171
}
167172

173+
$this->propertyVisibilities = ['private', 'protected', 'public'];
174+
$output->writeln($this->getOptionsLabel($this->propertyVisibilities, 'Available visibilities'));
175+
$property['visibility'] = $this->questionHelper->ask(
176+
$input,
177+
$output,
178+
$this->getQuestion(
179+
'Property visibility',
180+
'private',
181+
[$this, 'validatePropertyVisibility'],
182+
$this->propertyVisibilities
183+
)
184+
);
185+
168186
$output->writeln($this->getOptionsLabel($this->propertyAnnotations, 'Available annotations'));
169187
$property['annotation'] = $this->questionHelper->ask(
170188
$input,
@@ -210,7 +228,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
210228
$output,
211229
$this->getQuestion(
212230
'Property type',
213-
'string',
231+
'text',
214232
[$this, 'validatePropertyType'],
215233
$this->getPropertyTypes()
216234
)
@@ -515,6 +533,26 @@ public function validatePropertyAnnotation($annotation)
515533
return $annotation;
516534
}
517535

536+
/**
537+
* Validates property visibility
538+
*
539+
* @param string $visibility
540+
*
541+
* @return string
542+
* @throws \InvalidArgumentException When the visibility is not found in the list of allowed ones.
543+
*/
544+
public function validatePropertyVisibility($visibility)
545+
{
546+
if (!in_array($visibility, $this->propertyVisibilities)) {
547+
throw $this->getException(
548+
'The property visibility isn\'t valid ("%s" given, expecting one of following: %s)',
549+
[$visibility, implode(', ', $this->propertyVisibilities)]
550+
);
551+
}
552+
553+
return $visibility;
554+
}
555+
518556
/**
519557
* Returns formatted question
520558
*

Generator/DocumentGenerator.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace ONGR\ElasticsearchBundle\Generator;
1313

14+
use Doctrine\Common\Inflector\Inflector;
15+
1416
/**
1517
* Document Generator
1618
*/
@@ -33,6 +35,20 @@ class DocumentGenerator
3335
public function get<methodName>()
3436
{
3537
<spaces>return $this-><fieldName>;
38+
}';
39+
40+
/**
41+
* @var string
42+
*/
43+
private $isMethodTemplate =
44+
'/**
45+
* Returns <fieldName>
46+
*
47+
* @return string
48+
*/
49+
public function is<methodName>()
50+
{
51+
<spaces>return $this-><fieldName>;
3652
}';
3753

3854
/**
@@ -43,10 +59,14 @@ public function get<methodName>()
4359
* Sets <fieldName>
4460
*
4561
* @param string $<fieldName>
62+
*
63+
* @return self
4664
*/
4765
public function set<methodName>($<fieldName>)
4866
{
4967
<spaces>$this-><fieldName> = $<fieldName>;
68+
69+
<spaces>return $this;
5070
}';
5171

5272
/**
@@ -122,7 +142,7 @@ private function generateDocumentProperties(array $metadata)
122142

123143
foreach ($metadata['properties'] as $property) {
124144
$lines[] = $this->generatePropertyDocBlock($property);
125-
$lines[] = $this->spaces . 'private $' . $property['field_name'] . ";\n";
145+
$lines[] = $this->spaces . $property['visibility'] . ' $' . $property['field_name'] . ";\n";
126146
}
127147

128148
return implode("\n", $lines);
@@ -140,7 +160,14 @@ private function generateDocumentMethods(array $metadata)
140160
$lines = [];
141161

142162
foreach ($metadata['properties'] as $property) {
163+
if (isset($property['visibility']) && $property['visibility'] === 'public') {
164+
continue;
165+
}
143166
$lines[] = $this->generateDocumentMethod($property, $this->setMethodTemplate) . "\n";
167+
if (isset($property['property_type']) && $property['property_type'] === 'boolean') {
168+
$lines[] = $this->generateDocumentMethod($property, $this->isMethodTemplate) . "\n";
169+
}
170+
144171
$lines[] = $this->generateDocumentMethod($property, $this->getMethodTemplate) . "\n";
145172
}
146173

@@ -249,8 +276,8 @@ private function generateDocumentDocBlock(array $metadata)
249276
[
250277
$this->getClassName($metadata),
251278
ucfirst($metadata['annotation']),
252-
$metadata['type'] != lcfirst($this->getClassName($metadata))
253-
? sprintf('type="%s"', $metadata['type']) : '',
279+
$metadata['annotation'] != 'object' ? ($metadata['type'] != lcfirst($this->getClassName($metadata))
280+
? sprintf('type="%s"', $metadata['type']) : '') : '',
254281
],
255282
'/**
256283
* <className>

Mapping/DocumentFinder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function getBundleDocumentClasses($bundle, $documentsDirectory = null)
135135

136136
$bundleReflection = new \ReflectionClass($this->getBundleClass($bundle));
137137

138-
$documentsDirectory = DIRECTORY_SEPARATOR . $documentsDirectory . DIRECTORY_SEPARATOR;
138+
$documentsDirectory = DIRECTORY_SEPARATOR . str_replace('\\', '/', $documentsDirectory) . DIRECTORY_SEPARATOR;
139139
$directory = dirname($bundleReflection->getFileName()) . $documentsDirectory;
140140

141141
if (!is_dir($directory)) {

Mapping/DocumentParser.php

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ class DocumentParser
6565
*/
6666
private $properties = [];
6767

68+
/**
69+
* @var array Local cache for documents
70+
*/
71+
private $documents = [];
72+
6873
/**
6974
* @param Reader $reader Used for reading annotations.
7075
* @param DocumentFinder $finder Used for resolving namespaces.
@@ -81,40 +86,50 @@ public function __construct(Reader $reader, DocumentFinder $finder)
8186
*
8287
* @param \ReflectionClass $class
8388
*
84-
* @return array
89+
* @return array|null
8590
* @throws MissingDocumentAnnotationException
8691
*/
8792
public function parse(\ReflectionClass $class)
8893
{
89-
/** @var Document $document */
90-
$document = $this->reader->getClassAnnotation($class, self::DOCUMENT_ANNOTATION);
91-
92-
if ($document === null) {
93-
throw new MissingDocumentAnnotationException(
94-
sprintf(
95-
'"%s" class cannot be parsed as document because @Document annotation is missing.',
96-
$class->getName()
97-
)
98-
);
94+
$className = $class->getName();
95+
96+
if ($class->isTrait()) {
97+
return false;
9998
}
10099

101-
$fields = [];
102-
103-
return [
104-
'type' => $document->type ?: Caser::snake($class->getShortName()),
105-
'properties' => $this->getProperties($class),
106-
'fields' => array_filter(
107-
array_merge(
108-
$document->dump(),
109-
$fields
110-
)
111-
),
112-
'aliases' => $this->getAliases($class, $fields),
113-
'analyzers' => $this->getAnalyzers($class),
114-
'objects' => $this->getObjects(),
115-
'namespace' => $class->getName(),
116-
'class' => $class->getShortName(),
117-
];
100+
if (!isset($this->documents[$className])) {
101+
/** @var Document $document */
102+
$document = $this->reader->getClassAnnotation($class, self::DOCUMENT_ANNOTATION);
103+
104+
if ($document === null) {
105+
throw new MissingDocumentAnnotationException(
106+
sprintf(
107+
'"%s" class cannot be parsed as document because @Document annotation is missing.',
108+
$class->getName()
109+
)
110+
);
111+
}
112+
113+
$fields = [];
114+
$aliases = $this->getAliases($class, $fields);
115+
116+
$this->documents[$className] = [
117+
'type' => $document->type ?: Caser::snake($class->getShortName()),
118+
'properties' => $this->getProperties($class),
119+
'fields' => array_filter(
120+
array_merge(
121+
$document->dump(),
122+
$fields
123+
)
124+
),
125+
'aliases' => $aliases,
126+
'analyzers' => $this->getAnalyzers($class),
127+
'objects' => $this->getObjects(),
128+
'namespace' => $class->getName(),
129+
'class' => $class->getShortName(),
130+
];
131+
}
132+
return $this->documents[$className];
118133
}
119134

120135
/**
@@ -236,7 +251,6 @@ private function getObjects()
236251
private function getAliases(\ReflectionClass $reflectionClass, array &$metaFields = null)
237252
{
238253
$reflectionName = $reflectionClass->getName();
239-
$directory = $this->guessDirName($reflectionClass);
240254

241255
// We skip cache in case $metaFields is given. This should not affect performance
242256
// because for each document this method is called only once. For objects it might
@@ -251,6 +265,8 @@ private function getAliases(\ReflectionClass $reflectionClass, array &$metaField
251265
$properties = $this->getDocumentPropertiesReflection($reflectionClass);
252266

253267
foreach ($properties as $name => $property) {
268+
$directory = $this->guessDirName($property->getDeclaringClass());
269+
254270
$type = $this->getPropertyAnnotationData($property);
255271
$type = $type !== null ? $type : $this->getEmbeddedAnnotationData($property);
256272
$type = $type !== null ? $type : $this->getHashMapAnnotationData($property);
@@ -456,9 +472,10 @@ private function getDocumentPropertiesReflection(\ReflectionClass $reflectionCla
456472
private function getAnalyzers(\ReflectionClass $reflectionClass)
457473
{
458474
$analyzers = [];
459-
$directory = $this->guessDirName($reflectionClass);
460475

461476
foreach ($this->getDocumentPropertiesReflection($reflectionClass) as $name => $property) {
477+
$directory = $this->guessDirName($property->getDeclaringClass());
478+
462479
$type = $this->getPropertyAnnotationData($property);
463480
$type = $type !== null ? $type : $this->getEmbeddedAnnotationData($property);
464481

@@ -504,10 +521,11 @@ private function getAnalyzers(\ReflectionClass $reflectionClass)
504521
private function getProperties(\ReflectionClass $reflectionClass, $properties = [], $flag = false)
505522
{
506523
$mapping = [];
507-
$directory = $this->guessDirName($reflectionClass);
508524

509525
/** @var \ReflectionProperty $property */
510526
foreach ($this->getDocumentPropertiesReflection($reflectionClass) as $name => $property) {
527+
$directory = $this->guessDirName($property->getDeclaringClass());
528+
511529
$type = $this->getPropertyAnnotationData($property);
512530
$type = $type !== null ? $type : $this->getEmbeddedAnnotationData($property);
513531
$type = $type !== null ? $type : $this->getHashMapAnnotationData($property);

Mapping/MetadataCollector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,15 @@ public function getBundleMapping($name, $config = [])
141141
foreach ($documents as $document) {
142142
$documentReflection = new \ReflectionClass(
143143
$bundleNamespace .
144-
'\\' . $documentDir .
144+
'\\' . str_replace('/', '\\', $documentDir) .
145145
'\\' . $document
146146
);
147147

148148
try {
149149
$documentMapping = $this->getDocumentReflectionMapping($documentReflection);
150+
if (!$documentMapping) {
151+
continue;
152+
}
150153
} catch (MissingDocumentAnnotationException $exception) {
151154
// Not a document, just ignore
152155
continue;

Profiler/ElasticsearchProfiler.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ public function collect(Request $request, Response $response, \Exception $except
7575
}
7676
}
7777

78+
/**
79+
* {@inheritdoc}
80+
*/
81+
public function reset()
82+
{
83+
$this->queries = [];
84+
$this->count = 0;
85+
$this->time = 0;
86+
}
87+
7888
/**
7989
* Returns total time queries took.
8090
*

Resources/config/services.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
parameters:
22
es.logging.path: "%kernel.logs_dir%/elasticsearch_%kernel.environment%.log"
3-
es.profiler.template: ONGRElasticsearchBundle:Profiler:profiler.html.twig
43

54
services:
65
ONGR\ElasticsearchBundle\Command\:
@@ -57,7 +56,7 @@ services:
5756
- [setManagers, ["%es.managers%"]]
5857
- [addLogger, ["@es.tracer"]]
5958
tags:
60-
- {name: data_collector, template: "%es.profiler.template%", id: ongr.profiler}
59+
- {name: data_collector, template: "@ONGRElasticsearch/Profiler/profiler.html.twig", id: ongr.profiler}
6160

6261
es.result_converter:
6362
class: ONGR\ElasticsearchBundle\Result\Converter

0 commit comments

Comments
 (0)