Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
anis
anis-server
Commits
b1e08e9c
Commit
b1e08e9c
authored
Apr 02, 2020
by
François Agneray
Browse files
Using fetch instead of fetchAll (SearchAction)
parent
712acc60
Pipeline
#2262
passed with stages
in 3 minutes and 37 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
conf-dev/dev-php.ini
View file @
b1e08e9c
display_errors
=
1
error_reporting
=
E_ALL
date.timezone
=
Europe/Paris
short_open_tag
=
Off
date.timezone
=
Europe/Paris
safe_mode
=
Off
expose_php
=
Off
memory_limit
=
512M
upload_max_filesize
=
16M
post_max_size
=
16M
display_errors
=
On
display_startup_errors
=
On
disable_functions
=
pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signasignal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
max_execution_time
=
30
max_input_time
=
60
[xdebug]
zend_extension
=
/usr/local/lib/php/extensions/no-debug-non-zts-20180731/xdebug.so
...
...
src/Action/SearchAction.php
View file @
b1e08e9c
...
...
@@ -18,6 +18,7 @@ use Slim\Exception\HttpBadRequestException;
use
Slim\Exception\HttpNotFoundException
;
use
Doctrine\ORM\EntityManagerInterface
;
use
Doctrine\DBAL\Query\QueryBuilder
;
use
Doctrine\DBAL\Driver\Statement
;
use
Doctrine\DBAL\Query\Expression\CompositeExpression
;
use
App\Utils\DBALConnectionFactory
;
use
App\Utils\Operator\OperatorFactory
;
...
...
@@ -255,23 +256,21 @@ final class SearchAction extends AbstractAction
array
$attributes
,
string
$format
):
Response
{
// First of all
retrieve results from the database
// First of all
execute the SQL query
$stmt
=
$queryBuilder
->
execute
();
$rows
=
$stmt
->
fetchAll
();
// Build and write the payload according to the format
// The return the response
switch
(
$format
)
{
case
'json'
:
$payload
=
json_encode
(
$this
->
decodeNestedJson
(
$
rows
,
$attributes
),
JSON_UNESCAPED_SLASHES
);
$payload
=
json_encode
(
$this
->
decodeNestedJson
(
$
stmt
,
$attributes
),
JSON_UNESCAPED_SLASHES
);
$response
->
getBody
()
->
write
(
$payload
);
return
$response
;
case
'csv'
:
$payload
=
$this
->
transformArrayToCsv
(
$
rows
,
$attributes
,
','
);
$payload
=
$this
->
transformArrayToCsv
(
$
stmt
,
$attributes
,
','
);
$response
->
getBody
()
->
write
(
$payload
);
return
$response
->
withHeader
(
'Content-Type'
,
'text/csv'
);
case
'ascii'
:
$payload
=
$this
->
transformArrayToCsv
(
$
rows
,
$attributes
,
' '
);
$payload
=
$this
->
transformArrayToCsv
(
$
stmt
,
$attributes
,
' '
);
$response
->
getBody
()
->
write
(
$payload
);
return
$response
->
withHeader
(
'Content-Type'
,
'text/plain'
);
default
:
...
...
@@ -279,17 +278,19 @@ final class SearchAction extends AbstractAction
}
}
private
function
decodeNestedJson
(
array
$rows
,
array
$attributes
):
array
private
function
decodeNestedJson
(
Statement
$stmt
,
array
$attributes
):
array
{
$rows
=
array
();
$jsonAttributes
=
$this
->
getAttributesOfTypeJson
(
$attributes
);
foreach
(
$rows
as
&
$row
)
{
foreach
(
$row
as
$key
=>
&
$column
)
{
while
(
$row
=
$stmt
->
fetch
(
\
PDO
::
FETCH_ASSOC
)
)
{
foreach
(
$row
as
$key
=>
$column
)
{
if
(
array_search
(
$key
,
$jsonAttributes
))
{
if
(
!
is_null
(
$column
))
{
$row
[
$key
]
=
json_decode
(
$column
,
true
);
}
}
}
$rows
[]
=
$row
;
}
return
$rows
;
}
...
...
@@ -303,13 +304,13 @@ final class SearchAction extends AbstractAction
}));
}
private
function
transformArrayToCsv
(
array
$rows
,
array
$attributes
,
string
$delimiter
):
string
private
function
transformArrayToCsv
(
Statement
$stmt
,
array
$attributes
,
string
$delimiter
):
string
{
$attributesLabel
=
array_map
(
function
(
$attribute
)
{
return
$attribute
->
getLabel
();
},
$attributes
);
$csv
=
implode
(
$delimiter
,
$attributesLabel
)
.
PHP_EOL
;
foreach
(
$rows
as
$row
)
{
while
(
$row
=
$stmt
->
fetch
(
\
PDO
::
FETCH_ASSOC
)
)
{
$csv
.
=
implode
(
$delimiter
,
$row
)
.
PHP_EOL
;
}
return
$csv
;
...
...
src/Middleware/ContentTypeJsonMiddleware.php
View file @
b1e08e9c
...
...
@@ -18,7 +18,7 @@ use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use
Psr\Http\Server\MiddlewareInterface
;
/**
* Middleware to force content type to application/json
eror
* Middleware to force content type to application/json
*
* @author François Agneray <francois.agneray@lam.fr>
* @package App\Middleware
...
...
@@ -36,6 +36,10 @@ final class ContentTypeJsonMiddleware implements MiddlewareInterface
public
function
process
(
Request
$request
,
RequestHandler
$handler
):
Response
{
$response
=
$handler
->
handle
(
$request
);
return
$response
->
withHeader
(
'Content-Type'
,
'application/json'
);
if
(
$response
->
hasHeader
(
'Content-Type'
))
{
return
$response
;
}
else
{
return
$response
->
withHeader
(
'Content-Type'
,
'application/json'
);
}
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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