Skip to content

Commit d10f77a

Browse files
committed
update rest templates
1 parent dda50bd commit d10f77a

9 files changed

+547
-5
lines changed

src/Ubiquity/scaffolding/creators/BaseControllerCreator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ protected function getNamespaceStr(){
5454
return 'namespace '. \trim($this->controllerNS,'\\').';';
5555
}
5656

57-
protected function getRouteAnnotation($path) {
57+
protected function getRouteAnnotation($path, $automated=true, $inherited=true) {
5858
return CacheManager::getAnnotationsEngineInstance()->getAnnotation($this, 'route', [
5959
'path' => $path,
60-
'automated' => true,
61-
'inherited' => true
60+
'automated' => $automated,
61+
'inherited' => $inherited
6262
])->asAnnotation();
6363
}
6464

src/Ubiquity/scaffolding/creators/RestControllerCreator.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Ubiquity\controllers\rest\RestServer;
88
use Ubiquity\utils\base\UFileSystem;
99
use Ubiquity\cache\CacheManager;
10+
use Ubiquity\controllers\rest\RestBaseController;
1011

1112
/**
1213
* Creates a Rest controller.
@@ -45,7 +46,11 @@ public function create(ScaffoldController $scaffoldController, $reInit = null) {
4546

4647
$filename = $restControllersDir . \DS . $controllerName . ".php";
4748
if (! \file_exists ( $filename )) {
48-
$templateDir = $scaffoldController->getTemplateDir ();
49+
$attrFolder='';
50+
if(\class_exists('\\Ubiquity\\attributes\\AttributesEngine')){
51+
$attrFolder='attributes/';
52+
}
53+
$templateDir = $scaffoldController->getTemplateDir ().$attrFolder;
4954
$namespace = '';
5055
if ($controllerNS != null){
5156
$namespace = "namespace " . $controllerNS . ";";
@@ -54,7 +59,8 @@ public function create(ScaffoldController $scaffoldController, $reInit = null) {
5459
$routeAnnot='';
5560
if ($this->routePath != null) {
5661
$routePath=$this->routePath;
57-
$routeAnnot=$this->getRouteAnnotation($this->routePath);
62+
$automatedAndInherited=$this->baseClass===RestBaseController::class;
63+
$routeAnnot=$this->getRouteAnnotation($this->routePath,$automatedAndInherited,$automatedAndInherited);
5864
}
5965
$variables = [ '%route%' => $routeAnnot,'%controllerName%' => $controllerName,'%namespace%' => $namespace,'%routePath%' => $routePath ,'%baseClass%'=>$this->baseClass];
6066
$this->addVariablesForReplacement ( $variables );
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
%namespace%
3+
4+
%uses%
5+
use Ubiquity\controllers\rest\api\jsonapi\JsonApiResponseFormatter;
6+
use Ubiquity\controllers\rest\ResponseFormatter;
7+
use Ubiquity\attributes\items\router\Delete;
8+
use Ubiquity\attributes\items\router\Get;
9+
use Ubiquity\attributes\items\router\Options;
10+
use Ubiquity\attributes\items\router\Post;
11+
use Ubiquity\attributes\items\router\Put;
12+
13+
%restAnnot%
14+
%route%
15+
class %controllerName% extends %baseClass% {
16+
protected function getResponseFormatter(): ResponseFormatter {
17+
return new JsonApiResponseFormatter('%routePath%');
18+
}
19+
20+
/**
21+
* Returns all the instances from the model $resource.
22+
* Query parameters:
23+
* - **include**: A string of associated members to load, comma separated (e.g. users,groups,organization...), or a boolean: true for all members, false for none (default: true).
24+
* - **filter**: The filter to apply to the query (where part of an SQL query) (default: 1=1).
25+
* - **page[number]**: The page to display (in this case, the page size is set to 1).
26+
* - **page[size]**: The page size (count of instance per page) (default: 1).
27+
*
28+
* @route("{resource}/","methods"=>["get"],"priority"=>0)
29+
*/
30+
#[Get('{resource}',priority: 0)]
31+
public function all($resource){
32+
$this->getAll_($resource);
33+
}
34+
35+
/**
36+
* Returns an instance of $resource, by primary key $id.
37+
*
38+
* @param string $resource The resource (model) to use
39+
* @param string $id The primary key value(s), if the primary key is composite, use a comma to separate the values (e.g. 1,115,AB)
40+
*
41+
* @route("{resource}/{id}/","methods"=>["get"],"priority"=>1000)
42+
*/
43+
#[Get('{resource}/{id}', priority: 1000)]
44+
public function one($resource,$id){
45+
$this->getOne_($resource,$id);
46+
}
47+
48+
/**
49+
* Deletes an existing instance of $resource.
50+
*
51+
* @param string $resource The resource (model) to use
52+
* @param string $ids The primary key value(s), if the primary key is composite, use a comma to separate the values (e.g. 1,115,AB)
53+
*
54+
* @route("{resource}/{id}/","methods"=>["delete"],"priority"=>0)
55+
* @authorization
56+
*/
57+
#[Delete('{resource}/{id}')]
58+
public function delete($resource,...$id){
59+
$this->delete_($resource,...$id);
60+
}
61+
62+
/**
63+
* Route for CORS
64+
*
65+
* @route("{resource}","methods"=>["options"],"priority"=>3000)
66+
*/
67+
#[Options('{resource}',priority: 3000)]
68+
public function options(...$resource) {
69+
}
70+
71+
/**
72+
* Inserts a new instance of $resource.
73+
* Data attributes are send in request body (in JSON format)
74+
*
75+
* @param string $resource The resource (model) to use
76+
* @route("{resource}/","methods"=>["post"],"priority"=>0)
77+
* @authorization
78+
*/
79+
#[Post('{resource}',priority: 0)]
80+
public function add($resource) {
81+
parent::add_($resource);
82+
}
83+
84+
/**
85+
* Updates an existing instance of $resource.
86+
* Data attributes are send in data[attributes] request body (in JSON format)
87+
*
88+
* @param string $resource The resource (model) to use
89+
*
90+
* @route("{resource}/{id}","methods"=>["patch"],"priority"=>0)
91+
* @authorization
92+
*/
93+
#[Put('{resource}/{id}',priority: 0)]
94+
public function update($resource,...$id ){
95+
parent::update_($resource,...$id);
96+
}
97+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
%namespace%
3+
4+
%uses%
5+
6+
%restAnnot%
7+
%route%
8+
class %controllerName% extends %baseClass% {
9+
10+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
%namespace%
3+
4+
use Ubiquity\attributes\items\router\Delete;
5+
use Ubiquity\attributes\items\router\Get;
6+
use Ubiquity\attributes\items\router\Options;
7+
use Ubiquity\attributes\items\router\Post;
8+
use Ubiquity\attributes\items\router\Put;
9+
%uses%
10+
11+
%restAnnot%
12+
%route%
13+
class %controllerName% extends %baseClass% {
14+
15+
/**
16+
* Returns all the instances from the model $resource.
17+
* Query parameters:
18+
* - **include**: A string of associated members to load, comma separated (e.g. users,groups,organization...), or a boolean: true for all members, false for none (default: true).
19+
* - **filter**: The filter to apply to the query (where part of an SQL query) (default: 1=1).
20+
* - **page[number]**: The page to display (in this case, the page size is set to 1).
21+
* - **page[size]**: The page size (count of instance per page) (default: 1).
22+
*
23+
* @route("{resource}/","methods"=>["get"],"priority"=>0)
24+
*/
25+
#[Get('{resource}',priority: 0)]
26+
public function all($resource){
27+
$this->getAll_($resource);
28+
}
29+
30+
/**
31+
* Returns an instance of $resource, by primary key $id.
32+
*
33+
* @param string $resource The resource (model) to use
34+
* @param string $id The primary key value(s), if the primary key is composite, use a comma to separate the values (e.g. 1,115,AB)
35+
*
36+
* @route("{resource}/{id}/","methods"=>["get"],"priority"=>1000)
37+
*/
38+
#[Get('{resource}/{id}', priority: 1000)]
39+
public function one($resource,$id){
40+
$this->getOne_($resource,$id);
41+
}
42+
43+
/**
44+
* Deletes an existing instance of $resource.
45+
*
46+
* @param string $resource The resource (model) to use
47+
* @param string $ids The primary key value(s), if the primary key is composite, use a comma to separate the values (e.g. 1,115,AB)
48+
*
49+
* @route("{resource}/{id}/","methods"=>["delete"],"priority"=>0)
50+
* @authorization
51+
*/
52+
#[Delete('{resource}/{id}')]
53+
public function delete($resource,...$id){
54+
$this->delete_($resource,...$id);
55+
}
56+
57+
/**
58+
* Route for CORS
59+
*
60+
* @route("{resource}","methods"=>["options"],"priority"=>3000)
61+
*/
62+
#[Options('{resource}',priority: 3000)]
63+
public function options(...$resource) {
64+
}
65+
66+
/**
67+
* Inserts a new instance of $resource.
68+
* Data attributes are send in request body (in JSON format)
69+
*
70+
* @param string $resource The resource (model) to use
71+
* @route("{resource}/","methods"=>["post"],"priority"=>0)
72+
* @authorization
73+
*/
74+
#[Post('{resource}',priority: 0)]
75+
public function add($resource) {
76+
parent::add_($resource);
77+
}
78+
79+
/**
80+
* Updates an existing instance of $resource.
81+
* Data attributes are send in request body (in JSON format)
82+
*
83+
* @param string $resource The resource (model) to use
84+
*
85+
* @route("{resource}/{id}","methods"=>["patch"],"priority"=>0)
86+
* @authorization
87+
*/
88+
#[Put('{resource}/{id}',priority: 0)]
89+
public function update($resource,...$id ){
90+
parent::update_($resource,...$id);
91+
}
92+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
%namespace%
3+
4+
use Ubiquity\attributes\items\router\Delete;
5+
use Ubiquity\attributes\items\router\Get;
6+
use Ubiquity\attributes\items\router\Options;
7+
use Ubiquity\attributes\items\router\Post;
8+
use Ubiquity\attributes\items\router\Put;
9+
%uses%
10+
11+
%restAnnot%
12+
%route%
13+
class %controllerName% extends %baseClass% {
14+
15+
/**
16+
* Returns all links for this controller.
17+
*
18+
* @route("/links","methods"=>["get"],"priority"=>3000)
19+
*/
20+
#[Route('/links', priority: 3000)]
21+
public function index() {
22+
parent::index ();
23+
}
24+
25+
/**
26+
* Returns a list of objects from the server
27+
*
28+
* @param string $condition the sql Where part
29+
* @param boolean|string $included if true, loads associate members with associations, if string, example : client.*,commands
30+
* @param boolean $useCache
31+
* @route("methods"=>["get"])
32+
*/
33+
#[Get('{condition}/{included}/{useCache}', priority: -1)]
34+
public function all($condition = "1=1", $included = false, $useCache = false) {
35+
$this->_get ( $condition, $included, $useCache );
36+
}
37+
38+
/**
39+
* Get the first object corresponding to the $keyValues
40+
*
41+
* @param string $keyValues primary key(s) value(s) or condition
42+
* @param boolean|string $included if true, loads associate members with associations, if string, example : client.*,commands
43+
* @param boolean $useCache if true then response is cached
44+
* @route("methods"=>["get"])
45+
*/
46+
#[Get('{keyValues}/{included}/{useCache}', priority: -1)]
47+
public function one($keyValues, $included = false, $useCache = false) {
48+
$this->_getOne ( $keyValues, $included, $useCache );
49+
}
50+
51+
/**
52+
* Update an instance of $model selected by the primary key $keyValues
53+
* Require members values in $_POST array
54+
* Requires an authorization with access token
55+
*
56+
* @param array $keyValues
57+
* @authorization
58+
* @route("methods"=>["put"])
59+
*/
60+
#[Put('{keyValues}')]
61+
public function update(...$keyValues) {
62+
$this->_update ( ...$keyValues );
63+
}
64+
65+
/**
66+
* Insert a new instance of $model
67+
* Require members values in $_POST array
68+
* Requires an authorization with access token
69+
*
70+
* @authorization
71+
* @route("methods"=>["post"])
72+
*/
73+
#[Post('/')]
74+
public function add() {
75+
$this->_add ();
76+
}
77+
78+
/**
79+
* Delete the instance of $model selected by the primary key $keyValues
80+
* Requires an authorization with access token
81+
*
82+
* @param array $keyValues
83+
* @route("methods"=>["delete"],"priority"=>30)
84+
* @authorization
85+
*/
86+
#[Delete('{keyValues}')]
87+
public function delete(...$keyValues) {
88+
$this->_delete ( ...$keyValues );
89+
}
90+
91+
/**
92+
* Route for CORS
93+
*
94+
* @route("{resource}","methods"=>["options"],"priority"=>3000)
95+
*/
96+
#[Options('{resource}',priority: 3000)]
97+
public function options(...$resource) {}
98+
}

0 commit comments

Comments
 (0)