Skip to content
GitLab
Menu
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
e2d4e872
Commit
e2d4e872
authored
Aug 27, 2019
by
François Agneray
Browse files
Ajout des routes de gestion des instances
parent
7441415a
Changes
5
Hide whitespace changes
Inline
Side-by-side
src/Action/Instance/InstanceAction.php
0 → 100644
View file @
e2d4e872
<?php
declare
(
strict_types
=
1
);
/*
* This file is part of ANIS SERVER API.
*
* (c) François Agneray <francois.agneray@lam.fr>
* (c) Chrystel Moreau <chrystel.moreau@lam.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
App\Action\Instance
;
use
Psr\Log\LoggerInterface
;
use
Doctrine\ORM\EntityManagerInterface
;
use
Psr\Http\Message\ServerRequestInterface
as
Request
;
use
Psr\Http\Message\ResponseInterface
as
Response
;
use
App\Utils\ActionTrait
;
use
App\Entity\Admin\Instance
;
final
class
InstanceAction
{
use
ActionTrait
;
private
$logger
;
private
$em
;
public
function
__construct
(
LoggerInterface
$logger
,
EntityManagerInterface
$em
)
{
$this
->
logger
=
$logger
;
$this
->
em
=
$em
;
}
public
function
__invoke
(
Request
$request
,
Response
$response
,
array
$args
):
Response
{
$this
->
logger
->
info
(
'Instance action dispatched'
);
if
(
$request
->
isOptions
())
{
return
$response
->
withHeader
(
'Access-Control-Allow-Methods'
,
'GET, PUT, DELETE, OPTIONS'
);
}
$instance
=
$this
->
em
->
find
(
'App\Entity\Admin\Instance'
,
$args
[
'name'
]);
if
(
is_null
(
$instance
))
{
return
$this
->
dispatchHttpError
(
$response
,
'Invalid request'
,
'Instance with name '
.
$args
[
'name'
]
.
' is not found'
)
->
withStatus
(
404
);
}
if
(
$request
->
isGet
())
{
$newResponse
=
$response
->
withJson
(
$select
);
}
if
(
$request
->
isPut
())
{
$parsedBody
=
$request
->
getParsedBody
();
// Vérification des champs vides
foreach
(
array
(
'name'
,
'label'
,
'path_proxy'
,
'dev_mode'
,
'driver'
,
'path'
,
'host'
,
'port'
,
'dbname'
,
'login'
,
'password'
)
as
$a
)
{
if
(
$this
->
isEmptyField
(
$a
,
$parsedBody
))
{
return
$this
->
dispatchHttpError
(
$response
,
'Invalid request'
,
'Param '
.
$a
.
' needed to add a new instance'
);
}
}
$this
->
editInstance
(
$instance
,
$parsedBody
);
$newResponse
=
$response
->
withJson
(
$instance
);
}
if
(
$request
->
isDelete
())
{
$name
=
$instance
->
getName
();
$this
->
em
->
remove
(
$instance
);
$this
->
em
->
flush
();
$newResponse
=
$response
->
withJson
(
array
(
'message'
=>
'Instance with name '
.
$name
.
' is removed!'
));
}
return
$newResponse
;
}
private
function
editInstance
(
Instance
$instance
,
array
$parsedBody
):
void
{
$instance
->
setName
(
$parsedBody
[
'name'
]);
$instance
->
setLabel
(
$parsedBody
[
'label'
]);
$instance
->
setPathProxy
(
$parsedBody
[
'path_proxy'
]);
$instance
->
setDevMode
(
$parsedBody
[
'dev_mode'
]);
$instance
->
setDriver
(
$parsedBody
[
'driver'
]);
$instance
->
setPath
(
$parsedBody
[
'path'
]);
$instance
->
setHost
(
$parsedBody
[
'host'
]);
$instance
->
setPort
(
$parsedBody
[
'port'
]);
$instance
->
setDbName
(
$parsedBody
[
'dbname'
]);
$instance
->
setLogin
(
$parsedBody
[
'login'
]);
$instance
->
setPassword
(
$parsedBody
[
'password'
]);
$this
->
em
->
flush
();
}
}
src/Action/Instance/InstanceListAction.php
0 → 100644
View file @
e2d4e872
<?php
declare
(
strict_types
=
1
);
/*
* This file is part of ANIS SERVER API.
*
* (c) François Agneray <francois.agneray@lam.fr>
* (c) Chrystel Moreau <chrystel.moreau@lam.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
App\Action\Instance
;
use
Psr\Log\LoggerInterface
;
use
Doctrine\ORM\EntityManagerInterface
;
use
Psr\Http\Message\ServerRequestInterface
as
Request
;
use
Psr\Http\Message\ResponseInterface
as
Response
;
use
App\Utils\ActionTrait
;
use
App\Entity\Admin\Instance
;
final
class
InstanceListAction
{
use
ActionTrait
;
private
$logger
;
private
$em
;
public
function
__construct
(
LoggerInterface
$logger
,
EntityManagerInterface
$em
)
{
$this
->
logger
=
$logger
;
$this
->
em
=
$em
;
}
public
function
__invoke
(
Request
$request
,
Response
$response
,
array
$args
):
Response
{
$this
->
logger
->
info
(
'Instance list action dispatched'
);
if
(
$request
->
isOptions
())
{
return
$response
->
withHeader
(
'Access-Control-Allow-Methods'
,
'GET, POST, OPTIONS'
);
}
if
(
$request
->
isGet
())
{
$instances
=
$this
->
em
->
getRepository
(
'App\Entity\Admin\Instance'
)
->
findAll
();
$newResponse
=
$response
->
withJson
(
$instances
);
}
if
(
$request
->
isPost
())
{
$parsedBody
=
$request
->
getParsedBody
();
// Vérification des champs vides
foreach
(
array
(
'name'
,
'label'
,
'path_proxy'
,
'dev_mode'
,
'driver'
,
'path'
,
'host'
,
'port'
,
'dbname'
,
'login'
,
'password'
)
as
$a
)
{
if
(
$this
->
isEmptyField
(
$a
,
$parsedBody
))
{
return
$this
->
dispatchHttpError
(
$response
,
'Invalid request'
,
'Param '
.
$a
.
' needed to add a new instance'
);
}
}
$instance
=
$this
->
postInstance
(
$parsedBody
);
$newResponse
=
$response
->
withJson
(
$instance
,
201
);
}
return
$newResponse
;
}
private
function
postInstance
(
array
$parsedBody
):
Instance
{
$instance
=
new
Instance
(
$parsedBody
[
'name'
]);
$instance
->
setLabel
(
$parsedBody
[
'label'
]);
$instance
->
setPathProxy
(
$parsedBody
[
'path_proxy'
]);
$instance
->
setDevMode
(
$parsedBody
[
'dev_mode'
]);
$instance
->
setDriver
(
$parsedBody
[
'driver'
]);
$instance
->
setPath
(
$parsedBody
[
'path'
]);
$instance
->
setHost
(
$parsedBody
[
'host'
]);
$instance
->
setPort
(
$parsedBody
[
'port'
]);
$instance
->
setDbName
(
$parsedBody
[
'dbname'
]);
$instance
->
setLogin
(
$parsedBody
[
'login'
]);
$instance
->
setPassword
(
$parsedBody
[
'password'
]);
$this
->
em
->
persist
(
$instance
);
$this
->
em
->
flush
();
return
$instance
;
}
}
src/Entity/Admin/Instance.php
View file @
e2d4e872
...
...
@@ -326,11 +326,11 @@ class Instance implements \JsonSerializable
'dev_mode'
=>
$this
->
getDevMode
(),
'driver'
=>
$this
->
getDriver
(),
'path'
=>
$this
->
getPath
(),
'
db
host'
=>
$this
->
getHost
(),
'
db
port'
=>
$this
->
getPort
(),
'host'
=>
$this
->
getHost
(),
'port'
=>
$this
->
getPort
(),
'dbname'
=>
$this
->
getDbName
(),
'
db
login'
=>
$this
->
getLogin
(),
'
db
password'
=>
$this
->
getPassword
()
'login'
=>
$this
->
getLogin
(),
'password'
=>
$this
->
getPassword
()
];
}
}
src/Utils/MetaEntityManagerFactory.php
View file @
e2d4e872
...
...
@@ -10,7 +10,7 @@
*/
namespace
App\Utils
;
use
Doctrine\ORM\EntityManager
;
use
Doctrine\ORM\EntityManager
Interface
;
use
App\Entity\Admin\Instance
;
...
...
@@ -25,14 +25,14 @@ class MetaEntityManagerFactory
/**
* Doctrine Entity Manager to the anis admin database
*
* @var EntityManager
* @var EntityManager
Interface
*/
private
$adminEntityManager
;
/**
* Create the classe with the adminEntityManager object
*/
public
function
__construct
(
EntityManager
$adminEntityManager
)
public
function
__construct
(
EntityManager
Interface
$adminEntityManager
)
{
$this
->
adminEntityManager
=
$adminEntityManager
;
}
...
...
@@ -43,7 +43,7 @@ class MetaEntityManagerFactory
* @param string $instanceName Instance name necessary to find the correct metamodel database
* @return EntityManager
*/
public
function
getMetaEntityManager
(
string
$instanceName
):
EntityManager
public
function
getMetaEntityManager
(
string
$instanceName
):
EntityManager
Interface
{
// Get instance information from anis admin database
$instance
=
$this
->
adminEntityManager
->
find
(
'App\Entity\Admin\Instance'
,
$instanceName
);
...
...
src/routes.php
View file @
e2d4e872
...
...
@@ -28,6 +28,9 @@ $app->group('/settings', function (App $app) {
$app
->
map
([
'OPTIONS'
,
'GET'
,
'PUT'
,
'DELETE'
],
'/option/{id}'
,
App\Action\Settings\OptionAction
::
class
);
});
$app
->
map
([
'OPTIONS'
,
'GET'
,
'POST'
],
'/instance'
,
App\Action\Instance\InstanceListAction
::
class
);
$app
->
map
([
'OPTIONS'
,
'GET'
,
'PUT'
,
'DELETE'
],
'/instance/{name}'
,
App\Action\Instance\InstanceAction
::
class
);
$app
->
group
(
'/{instance}'
,
function
(
App
$app
)
{
$app
->
group
(
'/metadata'
,
function
(
App
$app
)
{
$app
->
map
([
'OPTIONS'
,
'GET'
,
'POST'
],
'/database'
,
App\Action\Meta\DatabaseListAction
::
class
);
...
...
Write
Preview
Supports
Markdown
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