From 037d03b829a01547468a249e3f704fe13feb2b22 Mon Sep 17 00:00:00 2001 From: douglas Date: Mon, 27 Jan 2020 15:13:18 -0300 Subject: [PATCH 1/2] =?UTF-8?q?Coment=C3=A1rios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/UserController.php | 2 +- app/Model/User.php | 2 +- app/traits/Read.php | 38 ++++++++++++++++++++++++------- src/Model.php | 11 ++------- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/app/Controller/UserController.php b/app/Controller/UserController.php index c8eb2ad..355397f 100755 --- a/app/Controller/UserController.php +++ b/app/Controller/UserController.php @@ -28,7 +28,7 @@ public function index(){ $this->view("home", [ - "users" => $this->users->all(), + "users" => $this->users->select()->get(), "title" => "Listando Usuários" ] ); diff --git a/app/Model/User.php b/app/Model/User.php index 5084dc1..e0ff83b 100755 --- a/app/Model/User.php +++ b/app/Model/User.php @@ -6,5 +6,5 @@ class User extends Model { - protected $table = "users"; + protected $table = "user"; } \ No newline at end of file diff --git a/app/traits/Read.php b/app/traits/Read.php index deba343..a3c064b 100644 --- a/app/traits/Read.php +++ b/app/traits/Read.php @@ -5,35 +5,57 @@ +use App\Model\User; + trait Read { - private $sql; - private $binds; - public function select($fields = "*"):object{ + /** + * Método responsável por + * @param string $fields + * @return User + */ + public function select($fields = "*"):User{ $this->sql = "SELECT {$fields} FROM {$this->table}"; - return $this; } - public function bindAndExecute(){ + /** + * Método responsável por Preparar as Instruções SQL e Fazer o Bind + * @return \PDOStatement + */ + public function bindAndExecute():\PDOStatement{ $select = $this->connection->prepare($this->sql); $select->execute($this->binds); - return $select; } + /** + * Método responsável por obter apenas o 1º Registro encontrado. + * @return object + */ public function first(){ $select = $this->bindAndExecute(); return $select->fetch(); } - public function get(){ + + /** + * Método responsável por obter todos os registros. + * @return array + */ + public function get():array{ $select = $this->bindAndExecute(); return $select->fetchAll(); } - public function where(){ + /** + * Melhorar este método para que possa aceitar a o operador AND, por exemplo: + * WHERE field =:field AND field2 =:field2 ... + * @return $this + * @throws \Exception + */ + public function where():User{ $num_args = func_num_args(); $args = func_get_args(); diff --git a/src/Model.php b/src/Model.php index 226b856..5b29261 100755 --- a/src/Model.php +++ b/src/Model.php @@ -16,19 +16,12 @@ class Model extends Connection protected $field; protected $value; + protected $sql; + protected $binds; use Create,Read,Update,Delete; - public function all(){ - - $sql = "SELECT * FROM {$this->table}"; - $stmt = $this->connection->prepare($sql); - $stmt->execute(); - - return $stmt->fetchAll(); - } - public function find($field,$value){ if((!isset($field) || empty($field))|| (!isset($value) || empty($value))){ From 46040ff78380a3125bdcfb7c97a551b098fbbae2 Mon Sep 17 00:00:00 2001 From: Douglas Date: Tue, 4 Feb 2020 22:06:57 -0200 Subject: [PATCH 2/2] Classe de Erros --- app/traits/Read.php | 2 - bootstrap.php | 12 +++- composer.json | 3 +- composer.lock | 159 +++++++++++++++++++++++++++++++++++++++++++- public/index.php | 13 ++-- src/Model.php | 2 + src/Whoops.php | 27 ++++++++ views/editar.html | 2 +- 8 files changed, 208 insertions(+), 12 deletions(-) create mode 100644 src/Whoops.php diff --git a/app/traits/Read.php b/app/traits/Read.php index deba343..ee253fd 100644 --- a/app/traits/Read.php +++ b/app/traits/Read.php @@ -7,8 +7,6 @@ trait Read { - private $sql; - private $binds; public function select($fields = "*"):object{ $this->sql = "SELECT {$fields} FROM {$this->table}"; diff --git a/bootstrap.php b/bootstrap.php index fb85d80..eaee09f 100755 --- a/bootstrap.php +++ b/bootstrap.php @@ -4,6 +4,8 @@ use Slim\App; use Dotenv\Dotenv; +use Core\Whoops; + $dotenv = Dotenv::createImmutable(__DIR__); $dotenv->load(); @@ -11,4 +13,12 @@ $config['displayErrorDetails'] = true; $config['addContentLengthHeader'] = false; -$app = new App(['settings' => $config]); \ No newline at end of file +$app = new App(['settings' => $config]); +$container = $app->getContainer(); + +$container['UserController'] = function ($container) { + $service = new \App\Controller\UserController; + return $service; +}; +$whoops = new Whoops(); +$whoops->run($container); diff --git a/composer.json b/composer.json index dfa4d94..af3db3d 100755 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ "require": { "slim/slim": "3.9", "twig/twig": "^3.0", - "vlucas/phpdotenv": "^4.1" + "vlucas/phpdotenv": "^4.1", + "dopesong/slim-whoops": "^2.3" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index cb23570..49a50c8 100755 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "daa512d956b6c70fc187e4d3b1be40eb", + "content-hash": "ea377acd1540415460dbab7f745576dc", "packages": [ { "name": "container-interop/container-interop", @@ -38,6 +38,116 @@ "abandoned": "psr/container", "time": "2017-02-14T19:40:03+00:00" }, + { + "name": "dopesong/slim-whoops", + "version": "2.3.0", + "source": { + "type": "git", + "url": "https://github.com/dopesong/Slim-Whoops.git", + "reference": "59d6f0a6e4368927a2f2a67e98e47f17962b3310" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/dopesong/Slim-Whoops/zipball/59d6f0a6e4368927a2f2a67e98e47f17962b3310", + "reference": "59d6f0a6e4368927a2f2a67e98e47f17962b3310", + "shasum": "" + }, + "require": { + "filp/whoops": "^2.0", + "php": ">=5.6.0" + }, + "require-dev": { + "psr/http-message": "^1.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Dopesong\\Slim\\Error\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Justinas Rakasis", + "email": "info@probit.lt", + "homepage": "http://probit.lt" + } + ], + "description": "Slim Framework 3 error handler built on top of the Whoops error handler", + "homepage": "https://github.com/dopesong/Slim-Whoops", + "keywords": [ + "error", + "framework", + "handler", + "slim", + "whoops" + ], + "time": "2018-05-17T17:11:52+00:00" + }, + { + "name": "filp/whoops", + "version": "2.7.1", + "source": { + "type": "git", + "url": "https://github.com/filp/whoops.git", + "reference": "fff6f1e4f36be0e0d0b84d66b413d9dcb0c49130" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/filp/whoops/zipball/fff6f1e4f36be0e0d0b84d66b413d9dcb0c49130", + "reference": "fff6f1e4f36be0e0d0b84d66b413d9dcb0c49130", + "shasum": "" + }, + "require": { + "php": "^5.5.9 || ^7.0", + "psr/log": "^1.0.1" + }, + "require-dev": { + "mockery/mockery": "^0.9 || ^1.0", + "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0", + "symfony/var-dumper": "^2.6 || ^3.0 || ^4.0 || ^5.0" + }, + "suggest": { + "symfony/var-dumper": "Pretty print complex values better with var-dumper available", + "whoops/soap": "Formats errors as SOAP responses" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.6-dev" + } + }, + "autoload": { + "psr-4": { + "Whoops\\": "src/Whoops/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Filipe Dobreira", + "homepage": "https://github.com/filp", + "role": "Developer" + } + ], + "description": "php error handling for cool kids", + "homepage": "https://filp.github.io/whoops/", + "keywords": [ + "error", + "exception", + "handling", + "library", + "throwable", + "whoops" + ], + "time": "2020-01-15T10:00:00+00:00" + }, { "name": "nikic/fast-route", "version": "v1.3.0", @@ -288,6 +398,53 @@ ], "time": "2016-08-06T14:39:51+00:00" }, + { + "name": "psr/log", + "version": "1.1.2", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "446d54b4cb6bf489fc9d75f55843658e6f25d801" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/446d54b4cb6bf489fc9d75f55843658e6f25d801", + "reference": "446d54b4cb6bf489fc9d75f55843658e6f25d801", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Log\\": "Psr/Log/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "homepage": "https://github.com/php-fig/log", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "time": "2019-11-01T11:05:21+00:00" + }, { "name": "slim/slim", "version": "3.9.0", diff --git a/public/index.php b/public/index.php index c870bd8..c6b4b17 100755 --- a/public/index.php +++ b/public/index.php @@ -2,11 +2,12 @@ require __DIR__ . "/../bootstrap.php"; -$app->get("/","App\Controller\UserController:index"); -$app->get("/users/create","App\Controller\UserController:create"); -$app->post("/users/store","App\Controller\UserController:store"); -$app->get("/users/edit/{id}","App\Controller\UserController:edit"); -$app->post("/users/update/{id}","App\Controller\UserController:update"); -$app->get("/users/delete/{id}","App\Controller\UserController:delete"); +$app->get("/","UserController:index"); +$app->get("/users/create","UserController:create"); +$app->post("/users/store","UserController:store"); +$app->get("/users/edit/{id}","UserController:edit"); +$app->post("/users/update/{id}","UserController:update"); +$app->get("/users/delete/{id}","UserController:delete"); + $app->run(); \ No newline at end of file diff --git a/src/Model.php b/src/Model.php index 226b856..a715e74 100755 --- a/src/Model.php +++ b/src/Model.php @@ -16,6 +16,8 @@ class Model extends Connection protected $field; protected $value; + protected $sql; + protected $binds; use Create,Read,Update,Delete; diff --git a/src/Whoops.php b/src/Whoops.php new file mode 100644 index 0000000..ed05896 --- /dev/null +++ b/src/Whoops.php @@ -0,0 +1,27 @@ +pushHandler(new \Whoops\Handler\PrettyPageHandler); + $this->whoops->register(); + } + + public function run($app){ + $this->slim($app); + $this->php(); + } + +} \ No newline at end of file diff --git a/views/editar.html b/views/editar.html index 3f2f350..1cae3e9 100644 --- a/views/editar.html +++ b/views/editar.html @@ -26,7 +26,7 @@

Usuários - Editar

{{ erros("telefone") }} - +