Skip to content

Commit c143792

Browse files
authored
Merge pull request #2 from brunourb/develop
Develop
2 parents 0337261 + e033a4c commit c143792

File tree

646 files changed

+155596
-8
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

646 files changed

+155596
-8
lines changed

LICENSE

100644100755
File mode changed.

README.md

100644100755
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,21 @@ forked by https://github.com/akrabat/slim3-skeleton
3838
* `app/routes.php`: All application routes are here
3939
* `app/src/Action/HomeAction.php`: Action class for the home page
4040
* `app/templates/home.twig`: Twig template file for the home page
41+
42+
https://stackoverflow.com/questions/2068344/how-do-i-get-a-youtube-video-thumbnail-from-the-youtube-api
43+
44+
## slime3 + Doctrine
45+
* http://blog.sub85.com/slim-3-with-doctrine-2.html
46+
47+
'<?php
48+
$page = 1; // Página Atual
49+
$numByPage = 5; // Número de registros por Página
50+
$Empresa = $em->createQuery("SELECT e FROM Empresa e ")
51+
->setFirstResult( ( $numByPage * ($page-1) ) )
52+
->setMaxResults( $numByPage );
53+
$Empresa = new \Doctrine\ORM\Tools\Pagination\Paginator($Empresa);'
54+
55+
## Generate Entities
56+
57+
./vendor/doctrine/orm/bin/doctrine orm:convert-mapping --namespace="App\\Entities\\" --force --from-database annotation ./app/src/Entities
58+
./vendor/doctrine/orm/bin/doctrine orm:generate-entities ./app/src/Entities/ --generate-annotations=true

app/dependencies.php

100644100755
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// Add extensions
1616
$view->addExtension(new Slim\Views\TwigExtension($c->get('router'), $c->get('request')->getUri()));
1717
$view->addExtension(new Twig_Extension_Debug());
18+
$view->addExtension(new \nochso\HtmlCompressTwig\Extension());
1819

1920
return $view;
2021
};
@@ -37,10 +38,35 @@
3738
return $logger;
3839
};
3940

41+
// Doctrine
42+
$container['em'] = function ($c) {
43+
$settings = $c->get('settings');
44+
$config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(
45+
$settings['doctrine']['meta']['entity_path'],
46+
$settings['doctrine']['meta']['auto_generate_proxies'],
47+
$settings['doctrine']['meta']['proxy_dir'],
48+
$settings['doctrine']['meta']['cache'],
49+
false
50+
);
51+
$em = \Doctrine\ORM\EntityManager::create($settings['doctrine']['connection'], $config);
52+
53+
$helpers = new Symfony\Component\Console\Helper\HelperSet(array(
54+
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
55+
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
56+
));
57+
58+
return $em;
59+
};
60+
61+
4062
// -----------------------------------------------------------------------------
4163
// Action factories
4264
// -----------------------------------------------------------------------------
4365

4466
$container[App\Action\HomeAction::class] = function ($c) {
4567
return new App\Action\HomeAction($c->get('view'), $c->get('logger'));
4668
};
69+
70+
$container[App\Action\CMSAction::class] = function ($c) {
71+
return new App\Action\CMSAction($c->get('view'), $c->get('logger'), $c->get('em'));
72+
};

app/middleware.php

100644100755
File mode changed.

app/routes.php

100644100755
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
<?php
22
// Routes
33

4-
$app->get('/', App\Action\HomeAction::class)
5-
->setName('homepage');
4+
//
5+
6+
$app->group('/painel', function() use ($app) {
7+
$app->get('',App\Action\CMSAction::class . ':page')->setName('homepage');
8+
$app->map(['GET', 'POST','DELETE','PUT'], '/menu', App\Action\CMSAction::class . ':page');
9+
$app->get('/page',App\Action\CMSAction::class . ':page');
10+
$app->get('/page-details',App\Action\CMSAction::class . ':page');
11+
$app->get('/img-video',App\Action\CMSAction::class . ':page');
12+
$app->get('/tag',App\Action\CMSAction::class . ':page');
13+
$app->get('/tag-values',App\Action\CMSAction::class . ':page');
14+
$app->get('/banner',App\Action\CMSAction::class . ':page');
15+
$app->get('/tariff',App\Action\CMSAction::class . ':page');
16+
$app->get('/hotel',App\Action\CMSAction::class . ':page');
17+
$app->get('/room',App\Action\CMSAction::class . ':page');
18+
$app->get('/room-values',App\Action\CMSAction::class . ':page');
19+
$app->post('/upload',App\Action\CMSAction::class . ':upload');
20+
21+
});
22+
23+
$app->get('/', App\Action\HomeAction::class)->setName('homepage');

app/settings.php

100644100755
Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
'settings' => [
44
// Slim Settings
55
'determineRouteBeforeAppMiddleware' => false,
6-
'displayErrorDetails' => false,
6+
'displayErrorDetails' => true,
77

88
// View settings
99
'view' => [
10-
'template_path' => __DIR__ . '/templates',
10+
'template_path' => array(
11+
__DIR__ . '/templates/admin-cms/default',
12+
__DIR__ . '/templates/admin-cms/partials',
13+
__DIR__ . '/templates/admin-cms/partials/head',
14+
__DIR__ . '/templates/admin-cms/partials/script'),
1115
'twig' => [
1216
'cache' => __DIR__ . '/../cache/twig',
1317
'debug' => true,
@@ -20,5 +24,24 @@
2024
'name' => 'app',
2125
'path' => __DIR__ . '/../log/app.log',
2226
],
27+
'doctrine' => [
28+
'meta' => [
29+
'entity_path' => ['app/src/Entity/'],
30+
'auto_generate_proxies' => true,
31+
'proxy_dir' => __DIR__.'/../cache/proxies',
32+
'cache' => null,
33+
],
34+
'connection' => [
35+
'driver' => 'pdo_mysql',
36+
'host' => 'localhost',
37+
'dbname' => 'php-cms-admin',
38+
'user' => 'root',
39+
'password' => 'root',
40+
'charset' => 'utf8',
41+
'driverOptions' => array(
42+
1002 => 'SET NAMES utf8'
43+
)
44+
]
45+
]
2346
],
2447
];

app/src/Action/CMSAction.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
namespace App\Action;
3+
4+
use App\Resource\GenericResource;
5+
use function Couchbase\defaultDecoder;
6+
use Doctrine\ORM\EntityManager;
7+
use Slim\Views\Twig;
8+
use Psr\Log\LoggerInterface;
9+
use Slim\Http\Request;
10+
use Slim\Http\Response;
11+
12+
final class CMSAction extends DefaultAction
13+
{
14+
private $view;
15+
private $logger;
16+
private $genericResource;
17+
18+
/**
19+
* CMSAction constructor.
20+
* @param Twig $view
21+
* @param LoggerInterface $logger
22+
* @param EntityManager $entityManager
23+
*/
24+
public function __construct(Twig $view, LoggerInterface $logger,EntityManager $entityManager) {
25+
$this->view = $view;
26+
$this->logger = $logger;
27+
$this->genericResource = new GenericResource($entityManager);
28+
}
29+
30+
/**
31+
* @param Request $request
32+
* @param Response $response
33+
* @param $args
34+
* @return Response
35+
*/
36+
public function __invoke(Request $request, Response $response, $args) {
37+
$this->logger->info("Home page action dispatched");
38+
$this->view->render($response, 'home.twig');
39+
return $response;
40+
}
41+
42+
/**
43+
* @param Request $request
44+
* @param Response $response
45+
* @param $args
46+
* @return Response
47+
*/
48+
public function page(Request $request, Response $response, $args) {
49+
$this->logger->info("Page action dispatched");
50+
51+
$page = $request->getUri()->getPath() == '/painel' ? explode("/", $request->getUri()->getPath())[1] : explode("/", $request->getUri()->getPath())[2];
52+
53+
$data = $this->resource($page,$request,$response,$args);
54+
55+
if($request->getParam('id') && !$request->isPost()){
56+
return $response->withStatus(200)
57+
->withHeader('Content-Type', 'application/json')
58+
->write(json_encode($data));
59+
}else{
60+
61+
$this->view->render($response, $page.'.twig', array("partials" => $page,"data" =>$data));
62+
63+
return $response;
64+
}
65+
}
66+
67+
/**
68+
* @param Request $request
69+
* @param Response $response
70+
* @param $args
71+
* @return mixed|string
72+
*/
73+
public function resource($page, Request $request, Response $response, $args){
74+
return $this->genericResource->resourse($page,$request,$response,$args);
75+
}
76+
77+
/**
78+
* @param Request $request
79+
* @param Response $response
80+
* @return Response|static
81+
*/
82+
public function upload(Request $request, Response $response) {
83+
84+
$errors = [];
85+
86+
$files = $request->getUploadedFiles();
87+
88+
foreach ($files as $data) {
89+
try {
90+
if (empty($data->file)) {
91+
throw new Exception('Expected a newfile');
92+
}
93+
94+
if ($data->getError() === UPLOAD_ERR_OK) {
95+
$uploadFileName = $data->getClientFilename();
96+
$token = md5(uniqid(rand(), true));
97+
$data->moveTo("/tmp/$token.".pathinfo($data->getClientFilename(), PATHINFO_EXTENSION));
98+
}
99+
} catch (\Exception $e) {
100+
array_push($errors, 'Failed to upload '. $data->getClientFilename());
101+
}
102+
}
103+
return $response->withJson(['errors' => $errors], 200, JSON_PRETTY_PRINT);
104+
105+
106+
107+
return $response;
108+
}
109+
}

app/src/Action/DefaultAction.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
namespace App\Action;
3+
4+
use App\Util\Access;
5+
use Slim\Http\Request;
6+
7+
class DefaultAction {
8+
9+
public function getPage(Request $request){
10+
return $request->getUri()->getPath() == '/painel' ? explode("/", $request->getUri()->getPath())[1] : explode("/", $request->getUri()->getPath())[2];
11+
}
12+
13+
public function isAuthorized(Request $request){
14+
if($request->getHeader('Authorization')!=null && count($request->getHeader('Authorization')[0])){
15+
$autorization = $request->getHeader('Authorization')[0];
16+
return $autorization == Access::$AUTHORIZATION;
17+
}
18+
else{
19+
return false;
20+
}
21+
}
22+
23+
public function object_to_array($data) {
24+
if ((! is_array($data)) and (! is_object($data))) return 'xxx'; //$data;
25+
26+
$result = array();
27+
28+
$data = (array) $data;
29+
foreach ($data as $key => $value) {
30+
if (is_object($value)) $value = (array) $value;
31+
if (is_array($value))
32+
$result[$key] = self::object_to_array($value);
33+
else
34+
$result[$key] = $value;
35+
}
36+
37+
return $result;
38+
}
39+
40+
}

app/src/Action/HomeAction.php

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __invoke(Request $request, Response $response, $args)
2121
{
2222
$this->logger->info("Home page action dispatched");
2323

24-
$this->view->render($response, 'home.twig');
24+
$this->view->render($response, 'home-1.twig');
2525
return $response;
2626
}
2727
}

app/src/Entity/AbstractEntity.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Created by IntelliJ IDEA.
4+
* User: bruno
5+
* Date: 04/03/18
6+
* Time: 21:35
7+
*/
8+
9+
namespace App\Entity;
10+
11+
12+
abstract class AbstractEntity {
13+
14+
public function getAvoidedFields() {
15+
return array ();
16+
}
17+
18+
public function toArray() {
19+
$temp = ( array ) $this;
20+
21+
$array = array ();
22+
23+
foreach ( $temp as $k => $v ) {
24+
$k = preg_match ( '/^\x00(?:.*?)\x00(.+)/', $k, $matches ) ? $matches [1] : $k;
25+
if (in_array ( $k, $this->getAvoidedFields () )) {
26+
$array [$k] = "";
27+
} else {
28+
29+
// if it is an object recursive call
30+
if (is_object ( $v ) && $v instanceof AbstractEntity) {
31+
$array [$k] = $v->toArray();
32+
}
33+
// if its an array pass por each item
34+
if (is_array ( $v )) {
35+
36+
foreach ( $v as $key => $value ) {
37+
if (is_object ( $value ) && $value instanceof AbstractEntity) {
38+
$arrayReturn [$key] = $value->toArray();
39+
} else {
40+
$arrayReturn [$key] = $value;
41+
}
42+
}
43+
$array [$k] = $arrayReturn;
44+
}
45+
// if it is not a array and a object return it
46+
if (! is_object ( $v ) && !is_array ( $v )) {
47+
$array [$k] = $v;
48+
}
49+
}
50+
}
51+
52+
return $array;
53+
}
54+
}

0 commit comments

Comments
 (0)