Skip to content

Commit 83e45a9

Browse files
committed
added REST api for basic models
1 parent 71485bd commit 83e45a9

30 files changed

+850
-20
lines changed

app/base/abstracts/AdminPage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function __construct(ContainerInterface $container)
5858
*
5959
* @return array
6060
*/
61-
public function getRouteVerbs()
61+
public static function getRouteVerbs()
6262
{
6363
return ['GET','POST'];
6464
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* SiteBase
4+
* PHP Version 7.0
5+
*
6+
* @category CMS / Framework
7+
* @package Degami\Sitebase
8+
* @author Mirko De Grandis <degami@github.com>
9+
* @license MIT https://opensource.org/licenses/mit-license.php
10+
* @link https://github.com/degami/sitebase
11+
*/
12+
namespace App\Base\Abstracts;
13+
14+
use \Psr\Container\ContainerInterface;
15+
use \App\App;
16+
use \App\Site\Routing\RouteInfo;
17+
use \Symfony\Component\HttpFoundation\Response;
18+
use \Symfony\Component\HttpFoundation\JsonResponse;
19+
use \App\Base\Traits\AdminTrait;
20+
use \Exception;
21+
22+
/**
23+
* Base for rest endopoints
24+
*/
25+
abstract class AdminRestPage extends BaseRestPage
26+
{
27+
28+
use AdminTrait;
29+
30+
31+
/**
32+
* before render hook
33+
*
34+
* @return Response|self
35+
*/
36+
protected function beforeRender()
37+
{
38+
if (!$this->checkCredentials() || !$this->checkPermission($this->getAccessPermission())) {
39+
return $this->getUtils()->errorPage(403);
40+
}
41+
42+
return parent::beforeRender();
43+
}
44+
45+
/**
46+
* gets access permission name
47+
*
48+
* @return string
49+
*/
50+
abstract protected function getAccessPermission();
51+
}

app/base/abstracts/BaseRestPage.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use \Psr\Container\ContainerInterface;
1515
use \App\App;
1616
use \App\Site\Routing\RouteInfo;
17+
use \App\Site\Models\RequestLog;
1718
use \Symfony\Component\HttpFoundation\Response;
1819
use \Symfony\Component\HttpFoundation\JsonResponse;
1920
use \Exception;
@@ -39,7 +40,7 @@ public function __construct(ContainerInterface $container)
3940
*
4041
* @return array
4142
*/
42-
public function getRouteVerbs()
43+
public static function getRouteVerbs()
4344
{
4445
return ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT'];
4546
}
@@ -56,7 +57,9 @@ protected function beforeRender()
5657
return $return;
5758
}
5859

59-
if ($this->getRequest()->getContentType() != 'application/json') {
60+
if ($this->getRequest()->headers->get('Content-Type') != 'application/json' &&
61+
$this->getRequest()->getContentType() != 'json'
62+
) {
6063
return $this->getUtils()->errorPage(403);
6164
}
6265

@@ -97,14 +100,23 @@ protected function loadObject($id)
97100
*/
98101
public function process(RouteInfo $route_info = null, $route_data = [])
99102
{
100-
$return = parent::process($route_info, $route_data);
101-
102103
if (!empty($data = json_decode($this->getRequest()->getContent(), true))) {
103104
if (isset($data['id'])) {
104105
unset($data['id']);
105106
}
106107
}
107108

109+
try {
110+
$log = $this->getContainer()->make(RequestLog::class);
111+
$log->fillWithRequest($this->getRequest(), $this);
112+
$log->persist();
113+
} catch (Exception $e) {
114+
$this->getUtils()->logException($e, "Can't write RequestLog");
115+
if ($this->getEnv('DEBUG')) {
116+
return $this->getUtils()->errorException($e);
117+
}
118+
}
119+
108120
$object = $this->getContainer()->call([$this->getObjectClass(), 'new']);
109121
if (in_array($this->getVerb(), ['GET', 'PUT', 'DELETE'])) {
110122
$object = $this->loadObject($route_data['id']);
@@ -125,6 +137,16 @@ public function process(RouteInfo $route_info = null, $route_data = [])
125137
break;
126138
case 'GET':
127139
// Read
140+
141+
if ($object->id == null) {
142+
return $this
143+
->getResponse()
144+
->prepare($this->getRequest())
145+
->setData(array_map(function ($object) {
146+
return $object->getData();
147+
}, $this->getContainer()->call([$this->getObjectClass(), 'all'])));
148+
}
149+
128150
return $this
129151
->getResponse()
130152
->prepare($this->getRequest())

app/base/exceptions/InvalidValueException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
namespace App\Base\Exceptions;
1313

14-
use Degami\Basics\Exceptions\BasicException;
14+
use \Degami\Basics\Exceptions\BasicException;
1515

1616
/**
1717
* "Invalid value" exception

app/base/exceptions/OfflineException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
namespace App\Base\Exceptions;
1313

14-
use Degami\Basics\Exceptions\BasicException;
14+
use \Degami\Basics\Exceptions\BasicException;
1515

1616
/**
1717
* "Site offline" exception

app/base/exceptions/PermissionDeniedException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
namespace App\Base\Exceptions;
1313

14-
use Degami\Basics\Exceptions\BasicException;
14+
use \Degami\Basics\Exceptions\BasicException;
1515

1616
/**
1717
* "Permission denied" exception

app/base/traits/ContainerAwareTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use \Psr\Container\ContainerInterface;
1515
use \App\Base\Abstracts\ContainerAwareObject;
16-
use \App\Base\Exceptions\BasicException;
16+
use \Degami\Basics\Exceptions\BasicException;
1717

1818
/**
1919
* Container Aware Object Trait
@@ -47,7 +47,7 @@ protected function getService($service_key)
4747
if ($this->getContainer()->has($service_key)) {
4848
return $this->getContainer()->get($service_key);
4949
} else {
50-
throw new BasicException("{$service_key} is not registered", 1);
50+
throw new BasicException("{$service_key} is not registered ", 1);
5151
}
5252
}
5353

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* SiteBase
4+
* PHP Version 7.0
5+
*
6+
* @category CMS / Framework
7+
* @package Degami\Sitebase
8+
* @author Mirko De Grandis <degami@github.com>
9+
* @license MIT https://opensource.org/licenses/mit-license.php
10+
* @link https://github.com/degami/sitebase
11+
*/
12+
namespace App\Site\Controllers\Admin\Crud;
13+
14+
use \Psr\Container\ContainerInterface;
15+
use \App\Base\Abstracts\AdminRestPage;
16+
use \App\Site\Models\Block as BlockModel;
17+
use \App\Site\Routing\RouteInfo;
18+
19+
/**
20+
* Blocks REST endpoint
21+
*/
22+
class Blocks extends AdminRestPage
23+
{
24+
/**
25+
* return route path
26+
*
27+
* @return string
28+
*/
29+
public static function getRoutePath()
30+
{
31+
return 'crud/blocks[/{id:\d+}]';
32+
}
33+
34+
/**
35+
* {@inheritdocs}
36+
*
37+
* @return string
38+
*/
39+
protected function getAccessPermission()
40+
{
41+
return 'administer_blocks';
42+
}
43+
44+
45+
/**
46+
* {@inheritdocs}
47+
*
48+
* @return string
49+
*/
50+
public static function getObjectClass()
51+
{
52+
return BlockModel::class;
53+
}
54+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* SiteBase
4+
* PHP Version 7.0
5+
*
6+
* @category CMS / Framework
7+
* @package Degami\Sitebase
8+
* @author Mirko De Grandis <degami@github.com>
9+
* @license MIT https://opensource.org/licenses/mit-license.php
10+
* @link https://github.com/degami/sitebase
11+
*/
12+
namespace App\Site\Controllers\Admin\Crud;
13+
14+
use \Psr\Container\ContainerInterface;
15+
use \App\Base\Abstracts\AdminRestPage;
16+
use \App\Site\Models\Configuration as ConfigurationModel;
17+
use \App\Site\Routing\RouteInfo;
18+
19+
/**
20+
* Configs REST endpoint
21+
*/
22+
class Configs extends AdminRestPage
23+
{
24+
/**
25+
* return route path
26+
*
27+
* @return string
28+
*/
29+
public static function getRoutePath()
30+
{
31+
return 'crud/configs[/{id:\d+}]';
32+
}
33+
34+
/**
35+
* {@inheritdocs}
36+
*
37+
* @return string
38+
*/
39+
protected function getAccessPermission()
40+
{
41+
return 'administer_site';
42+
}
43+
44+
45+
/**
46+
* {@inheritdocs}
47+
*
48+
* @return string
49+
*/
50+
public static function getObjectClass()
51+
{
52+
return ConfigurationModel::class;
53+
}
54+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* SiteBase
4+
* PHP Version 7.0
5+
*
6+
* @category CMS / Framework
7+
* @package Degami\Sitebase
8+
* @author Mirko De Grandis <degami@github.com>
9+
* @license MIT https://opensource.org/licenses/mit-license.php
10+
* @link https://github.com/degami/sitebase
11+
*/
12+
namespace App\Site\Controllers\Admin\Crud;
13+
14+
use \Psr\Container\ContainerInterface;
15+
use \App\Base\Abstracts\AdminRestPage;
16+
use \App\Site\Models\Language as LanguageModel;
17+
use \App\Site\Routing\RouteInfo;
18+
19+
/**
20+
* Languages REST endpoint
21+
*/
22+
class Languages extends AdminRestPage
23+
{
24+
/**
25+
* return route path
26+
*
27+
* @return string
28+
*/
29+
public static function getRoutePath()
30+
{
31+
return 'crud/languages[/{id:\d+}]';
32+
}
33+
34+
/**
35+
* {@inheritdocs}
36+
*
37+
* @return string
38+
*/
39+
protected function getAccessPermission()
40+
{
41+
return 'administer_languages';
42+
}
43+
44+
45+
/**
46+
* {@inheritdocs}
47+
*
48+
* @return string
49+
*/
50+
public static function getObjectClass()
51+
{
52+
return LanguageModel::class;
53+
}
54+
}

0 commit comments

Comments
 (0)