Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
rekall
laravel-scout-solr
Commits
2a03c938
Commit
2a03c938
authored
May 16, 2018
by
Bert Balcaen
Browse files
Fixing coding standards.
parent
850bf812
Changes
7
Hide whitespace changes
Inline
Side-by-side
phpcs.xml
0 → 100644
View file @
2a03c938
<?xml version="1.0"?>
<ruleset
name=
"PHP_CodeSniffer"
>
<description>
The coding standard for our project.
</description>
<file>
.
</file>
<exclude-pattern>
vendor
</exclude-pattern>
<arg
value=
"np"
/>
<rule
ref=
"PSR2"
>
<exclude
name=
"Generic.Files.LineLength"
/>
<exclude
name=
"Generic.ControlStructures.InlineControlStructure.NotAllowed"
/>
</rule>
</ruleset>
src/ScoutServiceProvider.php
View file @
2a03c938
...
...
@@ -3,10 +3,12 @@
use
Illuminate\Support\ServiceProvider
;
use
Laravel\Scout\EngineManager
;
class
ScoutServiceProvider
extends
ServiceProvider
{
public
function
boot
(){
class
ScoutServiceProvider
extends
ServiceProvider
{
public
function
boot
()
{
resolve
(
EngineManager
::
class
)
->
extend
(
'solr'
,
function
()
{
return
new
SolrEngine
();
});
}
}
\ No newline at end of file
}
src/Searchable.php
View file @
2a03c938
...
...
@@ -2,7 +2,8 @@
use
Laravel\Scout\Searchable
as
ScoutSearchable
;
trait
Searchable
{
trait
Searchable
{
use
ScoutSearchable
;
/**
...
...
@@ -10,7 +11,8 @@ trait Searchable{
*
* @return array
*/
public
function
getScoutMap
(){
public
function
getScoutMap
()
{
return
array_combine
(
$this
->
attributes
,
$this
->
attributes
);
}
...
...
@@ -19,17 +21,18 @@ trait Searchable{
*
* @return array
*/
public
function
toSearchableArray
(){
public
function
toSearchableArray
()
{
$attributes
=
$this
->
toArray
();
$map
=
$this
->
getScoutMap
();
foreach
(
$attributes
as
$attribute
=>
&
$value
)
{
if
(
$attribute
==
'id'
)
if
(
$attribute
==
'id'
)
$value
=
$this
->
getTable
()
.
"-
$value
"
;
if
(
$attribute
==
$map
[
$attribute
])
if
(
$attribute
==
$map
[
$attribute
])
continue
;
$attributes
[
$map
[
$attribute
]]
=
$value
;
unset
(
$attributes
[
$attribute
]);
}
return
$attributes
;
}
}
\ No newline at end of file
}
src/SolrEngine.php
View file @
2a03c938
...
...
@@ -4,7 +4,8 @@ use Illuminate\Database\Eloquent\Collection;
use
Laravel\Scout\Builder
;
use
Laravel\Scout\Engines\Engine
;
class
SolrEngine
extends
Engine
{
class
SolrEngine
extends
Engine
{
/**
* Update the given model in the index.
...
...
@@ -13,9 +14,10 @@ class SolrEngine extends Engine{
* @throws \Exception In case of update command failure.
* @return void
*/
public
function
update
(
$models
){
public
function
update
(
$models
)
{
$update
=
\
Solr
::
createUpdate
();
$models
->
each
(
function
(
$model
)
use
(
&
$update
){
$models
->
each
(
function
(
$model
)
use
(
&
$update
)
{
$document
=
$update
->
createDocument
(
$model
->
toSearchableArray
());
$update
->
addDocument
(
$document
);
});
...
...
@@ -29,9 +31,10 @@ class SolrEngine extends Engine{
* @throws \Exception In case of delete command failure.
* @return void
*/
public
function
delete
(
$models
){
public
function
delete
(
$models
)
{
$update
=
\
Solr
::
createUpdate
();
$models
->
each
(
function
(
$model
)
use
(
&
$update
){
$models
->
each
(
function
(
$model
)
use
(
&
$update
)
{
$update
->
addDeleteById
(
$model
->
getKey
());
});
$this
->
executeStatement
(
$update
);
...
...
@@ -43,10 +46,11 @@ class SolrEngine extends Engine{
* @throws \Exception In case of command failure.
* @param $statement \Solarium\QueryType\Update\Query\Query
*/
private
function
executeStatement
(
&
$statement
){
private
function
executeStatement
(
&
$statement
)
{
$statement
->
addCommit
();
$response
=
\
Solr
::
update
(
$statement
);
if
(
$response
->
getStatus
()
!=
0
)
if
(
$response
->
getStatus
()
!=
0
)
throw
new
\
Exception
(
"Update command failed
\n\n
"
.
json_encode
(
$response
->
getData
()));
}
...
...
@@ -56,7 +60,8 @@ class SolrEngine extends Engine{
* @param \Laravel\Scout\Builder $builder
* @return mixed
*/
public
function
search
(
Builder
$builder
){
public
function
search
(
Builder
$builder
)
{
$query
=
\
Solr
::
createSelect
();
return
$this
->
executeQuery
(
$query
,
$builder
);
}
...
...
@@ -69,7 +74,8 @@ class SolrEngine extends Engine{
* @param int $page
* @return mixed
*/
public
function
paginate
(
Builder
$builder
,
$perPage
,
$page
){
public
function
paginate
(
Builder
$builder
,
$perPage
,
$page
)
{
$query
=
\
Solr
::
createSelect
();
$offset
=
(
$page
-
1
)
*
$perPage
;
return
$this
->
executeQuery
(
$query
,
$builder
,
$offset
,
$perPage
);
...
...
@@ -84,12 +90,13 @@ class SolrEngine extends Engine{
* @param int $limit
* @return \Solarium\QueryType\Select\Result\Result
*/
private
function
executeQuery
(
&
$query
,
&
$builder
,
$offset
=
0
,
$limit
=
null
){
private
function
executeQuery
(
&
$query
,
&
$builder
,
$offset
=
0
,
$limit
=
null
)
{
$conditions
=
(
!
empty
(
$builder
->
query
))
?
[
$builder
->
query
]
:
[];
foreach
(
$builder
->
wheres
as
$key
=>
&
$value
)
foreach
(
$builder
->
wheres
as
$key
=>
&
$value
)
$conditions
[]
=
"
$key
:
\"
$value
\"
"
;
$query
->
setQuery
(
implode
(
' '
,
$conditions
));
if
(
!
is_null
(
$limit
))
if
(
!
is_null
(
$limit
))
$query
->
setStart
(
$offset
)
->
setRows
(
$limit
);
return
\
Solr
::
select
(
$query
);
}
...
...
@@ -100,9 +107,10 @@ class SolrEngine extends Engine{
* @param \Solarium\QueryType\Select\Result\Result $results
* @return \Illuminate\Support\Collection
*/
public
function
mapIds
(
$results
){
public
function
mapIds
(
$results
)
{
$ids
=
[];
foreach
(
$results
as
$document
)
foreach
(
$results
as
$document
)
$ids
[]
=
$document
->
id
;
return
collect
(
$ids
);
}
...
...
@@ -114,13 +122,14 @@ class SolrEngine extends Engine{
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Collection
*/
public
function
map
(
$results
,
$model
){
public
function
map
(
$results
,
$model
)
{
$map
=
array_flip
(
$model
->
getScoutMap
());
$models
=
[];
$modelClass
=
get_class
(
$model
);
foreach
(
$results
as
$document
){
foreach
(
$results
as
$document
)
{
$attributes
=
[];
foreach
(
$document
as
$field
=>
$value
)
foreach
(
$document
as
$field
=>
$value
)
$attributes
[
$map
[
$field
]]
=
(
$map
[
$field
]
!=
'id'
)
?
$value
:
str_replace
(
$model
->
getTable
()
.
'-'
,
''
,
$value
);
$models
[]
=
new
$modelClass
(
$attributes
);
}
...
...
@@ -133,7 +142,8 @@ class SolrEngine extends Engine{
* @param \Solarium\QueryType\Select\Result\Result $results
* @return int
*/
public
function
getTotalCount
(
$results
){
public
function
getTotalCount
(
$results
)
{
return
$results
->
getNumFound
();
}
}
\ No newline at end of file
}
src/SolrFacade.php
View file @
2a03c938
...
...
@@ -3,8 +3,10 @@
use
Illuminate\Support\Facades\Facade
;
use
Solarium\Client
;
class
SolrFacade
extends
Facade
{
protected
static
function
getFacadeAccessor
(){
class
SolrFacade
extends
Facade
{
protected
static
function
getFacadeAccessor
()
{
return
Client
::
class
;
}
}
\ No newline at end of file
}
src/SolrServiceProvider.php
View file @
2a03c938
...
...
@@ -3,22 +3,26 @@
use
Illuminate\Support\ServiceProvider
;
use
Solarium\Client
;
class
SolrServiceProvider
extends
ServiceProvider
{
class
SolrServiceProvider
extends
ServiceProvider
{
protected
$defer
=
true
;
public
function
boot
(){
public
function
boot
()
{
$this
->
publishes
([
__DIR__
.
'/solr-config.php'
=>
config_path
(
'solr.php'
)
]);
}
public
function
register
(){
$this
->
app
->
singleton
(
Client
::
class
,
function
(){
public
function
register
()
{
$this
->
app
->
singleton
(
Client
::
class
,
function
()
{
return
new
Client
(
config
(
'solr'
));
});
}
public
function
provides
(){
public
function
provides
()
{
return
[
Client
::
class
];
}
}
\ No newline at end of file
}
src/solr-config.php
View file @
2a03c938
...
...
@@ -7,4 +7,4 @@ return [
'path'
=>
'/solr/'
,
]
]
];
\ No newline at end of file
];
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment