From 673ac4c0260aff16a6d64ee5e3b650f49cf34147 Mon Sep 17 00:00:00 2001 From: Tyler Arbon Date: Sun, 27 Mar 2016 13:38:58 -0600 Subject: [PATCH 01/11] New namespace structure. Recorder component that can collect user_id, input data, request url, and request method. --- composer.json | 3 +- config/lern.php | 6 + ...0_add_user_data_and_url_to_lern_tables.php | 38 ++++++ .../Notifier.php | 4 +- src/Components/Recorder.php | 127 ++++++++++++++++++ .../MonologHandlerFactory.php | 2 +- src/LERN.php | 62 ++++++--- src/LERNServiceProvider.php | 1 + tests/LERNTest.php | 12 +- tests/MonologHandlerFactoryTest.php | 2 +- tests/NotifierTest.php | 4 +- tests/TestCase.php | 14 +- .../2016_03_17_000000_create_lern_tables.php | 42 ++++++ 13 files changed, 282 insertions(+), 35 deletions(-) create mode 100644 migrations/2016_03_27_000000_add_user_data_and_url_to_lern_tables.php rename src/{Notifications => Components}/Notifier.php (97%) mode change 100755 => 100644 create mode 100644 src/Components/Recorder.php rename src/{Notifications => Factories}/MonologHandlerFactory.php (99%) create mode 100644 tests/migrations/2016_03_17_000000_create_lern_tables.php diff --git a/composer.json b/composer.json index bb5e142..12364c7 100755 --- a/composer.json +++ b/composer.json @@ -37,7 +37,8 @@ }, "require-dev": { "orchestra/testbench": "^3.1.0", - "phpunit/phpunit": "^4.8 || ^5.0" + "phpunit/phpunit": "^4.8 || ^5.0", + "doctrine/dbal": "~2.3" }, "suggest": { } diff --git a/config/lern.php b/config/lern.php index 5b37597..e466adf 100755 --- a/config/lern.php +++ b/config/lern.php @@ -4,6 +4,12 @@ 'record'=>[ 'table'=>'vendor_tylercd100_lern_exceptions', + 'collect'=>[ + 'method'=>false,//When true it will collect GET, POST, DELETE, PUT, etc... + 'data'=>false,//When true it will collect Input data + 'user_id'=>false, + 'url'=>false, + ], ], 'notify'=>[ diff --git a/migrations/2016_03_27_000000_add_user_data_and_url_to_lern_tables.php b/migrations/2016_03_27_000000_add_user_data_and_url_to_lern_tables.php new file mode 100644 index 0000000..b5aaff6 --- /dev/null +++ b/migrations/2016_03_27_000000_add_user_data_and_url_to_lern_tables.php @@ -0,0 +1,38 @@ +integer('user_id')->nullable(); + $table->text('data')->nullable(); + $table->string('url')->nullable(); + $table->string('method')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table(config('lern.record.table'), function(Blueprint $table) { + $table->dropColumn('user_id'); + $table->dropColumn('data'); + $table->dropColumn('url'); + $table->dropColumn('method'); + }); + } + +} \ No newline at end of file diff --git a/src/Notifications/Notifier.php b/src/Components/Notifier.php old mode 100755 new mode 100644 similarity index 97% rename from src/Notifications/Notifier.php rename to src/Components/Notifier.php index 925240f..d02e1d5 --- a/src/Notifications/Notifier.php +++ b/src/Components/Notifier.php @@ -1,11 +1,11 @@ config = config('lern.record'); + } + + /** + * Records an Exception to the database + * @param Exception $e The exception you want to record + * @return Tylercd100\LERN\Models\ExceptionModel + */ + public function record(Exception $e) + { + $opts = [ + 'class' => get_class($e), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'code' => $e->getCode(), + 'message' => $e->getMessage(), + 'trace' => $e->getTraceAsString(), + ]; + + $opts['status_code'] = $this->getStatusCode($e); + $opts['user_id'] = $this->getUserId($e); + $opts['method'] = $this->getMethod($e); + $opts['data'] = $this->getData($e); + $opts['url'] = $this->getUrl($e); + + return ExceptionModel::create($opts); + } + + /** + * Checks the config to see if you can collect certain information + * @param string $type the config value you want to check + * @return boolean + */ + private function canCollect($type){ + if(!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])){ + return $this->config['collect'][$type] === true; + } + return false; + } + + /** + * Gets the ID of the User that is logged in + * @return integer|null The ID of the User or Null if not logged in + */ + protected function getUserId(){ + $user = Auth::user(); + if($this->canCollect('user_id') && is_object($user)) { + return $user->id; + } else { + return null; + } + } + + /** + * Gets the Method of the Request + * @return string|null Possible values are null or GET, POST, DELETE, PUT, etc... + */ + protected function getMethod(){ + $method = Request::method(); + if($this->canCollect('method') && !empty($method)) { + return $method; + } else { + return null; + } + } + + /** + * Gets the input data of the Request + * @return array|null The Input data or null + */ + protected function getData(){ + $data = Input::all(); + if($this->canCollect('data') && is_array($data)) { + return $data; + } else { + return null; + } + } + + /** + * Gets the URL of the Request + * @return string|null Returns a URL string or null + */ + protected function getUrl(){ + $url = Request::url(); + if($this->canCollect('url') && is_array($url)) { + return $url; + } else { + return null; + } + } + + /** + * Gets the status code of the Exception + * @param Exception $e The Exception to check + * @return string|integer The status code value + */ + protected function getStatusCode(Exception $e){ + if ($e instanceof HttpExceptionInterface) { + return $e->getStatusCode(); + } else { + return 0; + } + } +} \ No newline at end of file diff --git a/src/Notifications/MonologHandlerFactory.php b/src/Factories/MonologHandlerFactory.php similarity index 99% rename from src/Notifications/MonologHandlerFactory.php rename to src/Factories/MonologHandlerFactory.php index 6218e55..475ae63 100755 --- a/src/Notifications/MonologHandlerFactory.php +++ b/src/Factories/MonologHandlerFactory.php @@ -1,6 +1,6 @@ notifier = $notifier; + + if (empty($recorder)) { + $recorder = new Recorder(); + } + $this->recorder = $recorder; } /** @@ -43,25 +59,12 @@ public function handle(Exception $e) /** * Stores the exception in the database * @param Exception $e The exception to use - * @return ExceptionModel the recorded Eloquent Model + * @return Tylercd100\LERN\Models\ExceptionModel The recorded Exception as an Eloquent Model */ public function record(Exception $e) { $this->exception = $e; - $opts = [ - 'class' => get_class($e), - 'file' => $e->getFile(), - 'line' => $e->getLine(), - 'code' => $e->getCode(), - 'message' => $e->getMessage(), - 'trace' => $e->getTraceAsString(), - ]; - - if ($e instanceof HttpExceptionInterface) { - $opts['status_code'] = $e->getStatusCode(); - } - - return ExceptionModel::create($opts); + return $this->recorder->record($e); } /** @@ -104,6 +107,25 @@ public function setNotifier(Notifier $notifier) return $this; } + /** + * Get Recorder + * @return Recorder + */ + public function getRecorder() + { + return $this->recorder; + } + + /** + * Set Recorder + * @param Recorder $recorder A Recorder instance to use + */ + public function setRecorder(Recorder $recorder) + { + $this->recorder = $recorder; + return $this; + } + /** * Set a string or a closure to be called that will generate the message body for the notification * @param function|string $cb This closure function will be passed an Exception and must return a string diff --git a/src/LERNServiceProvider.php b/src/LERNServiceProvider.php index a937730..42e5984 100755 --- a/src/LERNServiceProvider.php +++ b/src/LERNServiceProvider.php @@ -18,6 +18,7 @@ public function boot() { $this->publishes([ __DIR__ . '/../migrations/2016_03_17_000000_create_lern_tables.php' => base_path('database/migrations/2016_03_17_000000_create_lern_tables.php'), + __DIR__ . '/../migrations/2016_03_27_000000_add_user_data_and_url_to_lern_tables.php' => base_path('database/migrations/2016_03_27_000000_add_user_data_and_url_to_lern_tables.php'), __DIR__ . '/../config/lern.php' => base_path('config/lern.php'), ]); } diff --git a/tests/LERNTest.php b/tests/LERNTest.php index a8fbeb2..c64d5af 100644 --- a/tests/LERNTest.php +++ b/tests/LERNTest.php @@ -3,8 +3,8 @@ namespace Tylercd100\LERN\Tests; use Tylercd100\LERN\LERN; -use Tylercd100\LERN\Notifications\MonologHandlerFactory; -use Tylercd100\LERN\Notifications\Notifier; +use Tylercd100\LERN\Factories\MonologHandlerFactory; +use Tylercd100\LERN\Components\Notifier; use Exception; class LERNTest extends TestCase @@ -42,7 +42,7 @@ public function testRecordReturnsCorrectInstance() { } public function testItCallsNotifierSendMethod() { - $mock = $this->getMock('Tylercd100\LERN\Notifications\Notifier', array('send')); + $mock = $this->getMock('Tylercd100\LERN\Components\Notifier', array('send')); $mock->expects($this->once()) ->method('send'); $lern = new LERN($mock); @@ -50,7 +50,7 @@ public function testItCallsNotifierSendMethod() { } public function testItCallsNotifierPushHandlerMethod() { - $mock = $this->getMock('Tylercd100\LERN\Notifications\Notifier', array('pushHandler')); + $mock = $this->getMock('Tylercd100\LERN\Components\Notifier', array('pushHandler')); $mock->expects($this->once()) ->method('pushHandler'); $lern = new LERN($mock); @@ -59,7 +59,7 @@ public function testItCallsNotifierPushHandlerMethod() { } public function testItCallsNotifierSetSubjectMethod() { - $mock = $this->getMock('Tylercd100\LERN\Notifications\Notifier', array('setSubject')); + $mock = $this->getMock('Tylercd100\LERN\Components\Notifier', array('setSubject')); $mock->expects($this->once()) ->method('setSubject'); @@ -69,7 +69,7 @@ public function testItCallsNotifierSetSubjectMethod() { } public function testItCallsNotifierSetMessageMethod() { - $mock = $this->getMock('Tylercd100\LERN\Notifications\Notifier', array('setMessage')); + $mock = $this->getMock('Tylercd100\LERN\Components\Notifier', array('setMessage')); $mock->expects($this->once()) ->method('setMessage'); diff --git a/tests/MonologHandlerFactoryTest.php b/tests/MonologHandlerFactoryTest.php index af567b1..2fdd2c6 100755 --- a/tests/MonologHandlerFactoryTest.php +++ b/tests/MonologHandlerFactoryTest.php @@ -3,7 +3,7 @@ namespace Tylercd100\LERN\Tests; use Exception; -use Tylercd100\LERN\Notifications\MonologHandlerFactory; +use Tylercd100\LERN\Factories\MonologHandlerFactory; class MonologHandlerFactoryTest extends TestCase { diff --git a/tests/NotifierTest.php b/tests/NotifierTest.php index cab319c..543f425 100755 --- a/tests/NotifierTest.php +++ b/tests/NotifierTest.php @@ -3,8 +3,8 @@ namespace Tylercd100\LERN\Tests; use Exception; -use Tylercd100\LERN\Notifications\Notifier; -use Tylercd100\LERN\Notifications\MonologHandlerFactory; +use Tylercd100\LERN\Components\Notifier; +use Tylercd100\LERN\Factories\MonologHandlerFactory; class NotifierTest extends TestCase { diff --git a/tests/TestCase.php b/tests/TestCase.php index 816f45d..cbee2d4 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,7 +3,7 @@ namespace Tylercd100\LERN\Tests; use Orchestra\Testbench\TestCase as Orchestra; -use Tylercd100\LERN\Notifications\MonologHandlerFactory; +use Tylercd100\LERN\Factories\MonologHandlerFactory; class TestCase extends Orchestra { @@ -72,7 +72,7 @@ public function tearDown() protected function migrate(){ $this->artisan('migrate', [ '--database' => 'testbench', - '--realpath' => realpath(__DIR__.'/../migrations'), + '--realpath' => realpath(__DIR__.'/migrations'), ]); } @@ -104,6 +104,16 @@ protected function getEnvironmentSetUp($app) 'prefix' => '', ]); + $app['config']->set('lern.record', [ + 'table'=>'vendor_tylercd100_lern_exceptions', + 'collect'=>[ + 'method'=>false,//When true it will collect GET, POST, DELETE, PUT, etc... + 'data'=>false,//When true it will collect Input data + 'user_id'=>false, + 'url'=>false, + ], + ]); + $app['config']->set('lern.notify', [ 'channel'=>'Tylercd100\LERN', 'includeExceptionStackTrace'=>true, diff --git a/tests/migrations/2016_03_17_000000_create_lern_tables.php b/tests/migrations/2016_03_17_000000_create_lern_tables.php new file mode 100644 index 0000000..7f0eb0e --- /dev/null +++ b/tests/migrations/2016_03_17_000000_create_lern_tables.php @@ -0,0 +1,42 @@ +increments('id')->unsigned(); + $table->string('class'); + $table->string('file'); + $table->integer('code'); + $table->integer('status_code')->default(0); + $table->integer('line'); + $table->text('message'); + $table->mediumText('trace'); + $table->timestamps(); + $table->integer('user_id')->nullable(); + $table->text('data')->nullable(); + $table->string('url')->nullable(); + $table->string('method')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop(config('lern.record.table')); + } + +} \ No newline at end of file From 436495c4b0206d5252c66ca088d2dd316c956ba4 Mon Sep 17 00:00:00 2001 From: Tyler Arbon Date: Sun, 27 Mar 2016 13:50:55 -0600 Subject: [PATCH 02/11] small docblock changes --- src/Components/Recorder.php | 2 +- src/LERN.php | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Components/Recorder.php b/src/Components/Recorder.php index 10df8b5..6322be0 100644 --- a/src/Components/Recorder.php +++ b/src/Components/Recorder.php @@ -12,7 +12,7 @@ class Recorder { /** - * @var array + * @var mixed */ protected $config = []; diff --git a/src/LERN.php b/src/LERN.php index 32b50b2..0bba9d6 100755 --- a/src/LERN.php +++ b/src/LERN.php @@ -18,18 +18,18 @@ class LERN private $exception; /** - * @var Tylercd100\LERN\Components\Notifier + * @var \Tylercd100\LERN\Components\Notifier */ private $notifier; /** - * @var Tylercd100\LERN\Components\Recorder + * @var \Tylercd100\LERN\Components\Recorder */ private $recorder; /** - * @param Notifier|null $notifier Notifier instance - * @param Recorder|null $recorder Recorder instance + * @param \Tylercd100\LERN\Components\Notifier|null $notifier Notifier instance + * @param \Tylercd100\LERN\Components\Recorder|null $recorder Recorder instance */ public function __construct(Notifier $notifier = null, Recorder $recorder = null) { @@ -59,7 +59,7 @@ public function handle(Exception $e) /** * Stores the exception in the database * @param Exception $e The exception to use - * @return Tylercd100\LERN\Models\ExceptionModel The recorded Exception as an Eloquent Model + * @return \Tylercd100\LERN\Models\ExceptionModel The recorded Exception as an Eloquent Model */ public function record(Exception $e) { @@ -81,7 +81,6 @@ public function notify(Exception $e) /** * Pushes on another Monolog Handler * @param HandlerInterface $handler The handler instance to add on - * @return Notifier Returns this */ public function pushHandler(HandlerInterface $handler) { $this->notifier->pushHandler($handler); @@ -90,7 +89,7 @@ public function pushHandler(HandlerInterface $handler) { /** * Get Notifier - * @return Notifier + * @return \Tylercd100\LERN\Components\Notifier */ public function getNotifier() { @@ -99,7 +98,7 @@ public function getNotifier() /** * Set Notifier - * @param Notifier $notifier A Notifier instance to use + * @param \Tylercd100\LERN\Components\Notifier $notifier A Notifier instance to use */ public function setNotifier(Notifier $notifier) { @@ -109,7 +108,7 @@ public function setNotifier(Notifier $notifier) /** * Get Recorder - * @return Recorder + * @return \Tylercd100\LERN\Components\Recorder */ public function getRecorder() { @@ -118,7 +117,7 @@ public function getRecorder() /** * Set Recorder - * @param Recorder $recorder A Recorder instance to use + * @param \Tylercd100\LERN\Components\Recorder $recorder A Recorder instance to use */ public function setRecorder(Recorder $recorder) { From c9ac5c10699111d9eab2534397c417084e766cd7 Mon Sep 17 00:00:00 2001 From: Scrutinizer Auto-Fixer Date: Sun, 27 Mar 2016 19:54:56 +0000 Subject: [PATCH 03/11] Scrutinizer Auto-Fixes This commit consists of patches automatically generated for this project on https://scrutinizer-ci.com --- config/lern.php | 4 ++-- src/Components/Recorder.php | 32 ++++++++++++++++---------------- src/LERN.php | 2 +- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/config/lern.php b/config/lern.php index e466adf..03280bc 100755 --- a/config/lern.php +++ b/config/lern.php @@ -5,8 +5,8 @@ 'record'=>[ 'table'=>'vendor_tylercd100_lern_exceptions', 'collect'=>[ - 'method'=>false,//When true it will collect GET, POST, DELETE, PUT, etc... - 'data'=>false,//When true it will collect Input data + 'method'=>false, //When true it will collect GET, POST, DELETE, PUT, etc... + 'data'=>false, //When true it will collect Input data 'user_id'=>false, 'url'=>false, ], diff --git a/src/Components/Recorder.php b/src/Components/Recorder.php index 6322be0..96998ce 100644 --- a/src/Components/Recorder.php +++ b/src/Components/Recorder.php @@ -3,11 +3,11 @@ namespace Tylercd100\LERN\Components; use Exception; -use Tylercd100\LERN\Models\ExceptionModel; -use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Facades\Request; use Illuminate\Support\Facades\Input; +use Illuminate\Support\Facades\Request; +use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; +use Tylercd100\LERN\Models\ExceptionModel; class Recorder { @@ -19,14 +19,14 @@ class Recorder { /** * The constructor */ - public function __construct(){ + public function __construct() { $this->config = config('lern.record'); } /** * Records an Exception to the database * @param Exception $e The exception you want to record - * @return Tylercd100\LERN\Models\ExceptionModel + * @return ExceptionModel */ public function record(Exception $e) { @@ -53,8 +53,8 @@ public function record(Exception $e) * @param string $type the config value you want to check * @return boolean */ - private function canCollect($type){ - if(!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])){ + private function canCollect($type) { + if (!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])) { return $this->config['collect'][$type] === true; } return false; @@ -64,9 +64,9 @@ private function canCollect($type){ * Gets the ID of the User that is logged in * @return integer|null The ID of the User or Null if not logged in */ - protected function getUserId(){ + protected function getUserId() { $user = Auth::user(); - if($this->canCollect('user_id') && is_object($user)) { + if ($this->canCollect('user_id') && is_object($user)) { return $user->id; } else { return null; @@ -77,9 +77,9 @@ protected function getUserId(){ * Gets the Method of the Request * @return string|null Possible values are null or GET, POST, DELETE, PUT, etc... */ - protected function getMethod(){ + protected function getMethod() { $method = Request::method(); - if($this->canCollect('method') && !empty($method)) { + if ($this->canCollect('method') && !empty($method)) { return $method; } else { return null; @@ -90,9 +90,9 @@ protected function getMethod(){ * Gets the input data of the Request * @return array|null The Input data or null */ - protected function getData(){ + protected function getData() { $data = Input::all(); - if($this->canCollect('data') && is_array($data)) { + if ($this->canCollect('data') && is_array($data)) { return $data; } else { return null; @@ -103,9 +103,9 @@ protected function getData(){ * Gets the URL of the Request * @return string|null Returns a URL string or null */ - protected function getUrl(){ + protected function getUrl() { $url = Request::url(); - if($this->canCollect('url') && is_array($url)) { + if ($this->canCollect('url') && is_array($url)) { return $url; } else { return null; @@ -117,7 +117,7 @@ protected function getUrl(){ * @param Exception $e The Exception to check * @return string|integer The status code value */ - protected function getStatusCode(Exception $e){ + protected function getStatusCode(Exception $e) { if ($e instanceof HttpExceptionInterface) { return $e->getStatusCode(); } else { diff --git a/src/LERN.php b/src/LERN.php index 0bba9d6..60c31a8 100755 --- a/src/LERN.php +++ b/src/LERN.php @@ -3,9 +3,9 @@ namespace Tylercd100\LERN; use Exception; +use Monolog\Handler\HandlerInterface; use Tylercd100\LERN\Components\Notifier; use Tylercd100\LERN\Components\Recorder; -use Monolog\Handler\HandlerInterface; /** * The master class From 0b31d113408d23fc2ff3e396f019c467008b4b00 Mon Sep 17 00:00:00 2001 From: Tyler Arbon Date: Sun, 27 Mar 2016 13:56:26 -0600 Subject: [PATCH 04/11] renamed test migrations --- src/Components/Recorder.php | 8 ++++---- tests/migrations/2016_03_17_000000_create_lern_tables.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Components/Recorder.php b/src/Components/Recorder.php index 6322be0..a48ea9f 100644 --- a/src/Components/Recorder.php +++ b/src/Components/Recorder.php @@ -40,10 +40,10 @@ public function record(Exception $e) ]; $opts['status_code'] = $this->getStatusCode($e); - $opts['user_id'] = $this->getUserId($e); - $opts['method'] = $this->getMethod($e); - $opts['data'] = $this->getData($e); - $opts['url'] = $this->getUrl($e); + $opts['user_id'] = $this->getUserId(); + $opts['method'] = $this->getMethod(); + $opts['data'] = $this->getData(); + $opts['url'] = $this->getUrl(); return ExceptionModel::create($opts); } diff --git a/tests/migrations/2016_03_17_000000_create_lern_tables.php b/tests/migrations/2016_03_17_000000_create_lern_tables.php index 7f0eb0e..7b4df85 100644 --- a/tests/migrations/2016_03_17_000000_create_lern_tables.php +++ b/tests/migrations/2016_03_17_000000_create_lern_tables.php @@ -3,7 +3,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; -class CreateLERNTables extends Migration { +class CreateLERNTablesForTests extends Migration { /** * Run the migrations. From 312ed0a4c1500fd82f20df7b7ddc82bcfb6d1b02 Mon Sep 17 00:00:00 2001 From: Tyler Arbon Date: Sun, 27 Mar 2016 13:58:01 -0600 Subject: [PATCH 05/11] small dockblock change --- src/LERN.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/LERN.php b/src/LERN.php index 60c31a8..12afc6d 100755 --- a/src/LERN.php +++ b/src/LERN.php @@ -99,6 +99,7 @@ public function getNotifier() /** * Set Notifier * @param \Tylercd100\LERN\Components\Notifier $notifier A Notifier instance to use + * @return \Tylercd100\LERN\LERN */ public function setNotifier(Notifier $notifier) { @@ -118,6 +119,7 @@ public function getRecorder() /** * Set Recorder * @param \Tylercd100\LERN\Components\Recorder $recorder A Recorder instance to use + * @return \Tylercd100\LERN\LERN */ public function setRecorder(Recorder $recorder) { From 46ca327cfb3f25ee4ddafd415a770620910928ef Mon Sep 17 00:00:00 2001 From: Tyler Arbon Date: Sun, 27 Mar 2016 15:07:22 -0600 Subject: [PATCH 06/11] Fixed Recorder. Added a test. Added attribute mutators on model. --- config/lern.php | 1 + src/Components/Recorder.php | 49 +++++++++++++++---- src/Models/ExceptionModel.php | 8 +++ tests/NotifierTest.php | 2 +- ...7_000000_create_lern_tables_for_tests.php} | 0 5 files changed, 50 insertions(+), 10 deletions(-) rename tests/migrations/{2016_03_17_000000_create_lern_tables.php => 2016_03_17_000000_create_lern_tables_for_tests.php} (100%) diff --git a/config/lern.php b/config/lern.php index 03280bc..7a64ab5 100755 --- a/config/lern.php +++ b/config/lern.php @@ -7,6 +7,7 @@ 'collect'=>[ 'method'=>false, //When true it will collect GET, POST, DELETE, PUT, etc... 'data'=>false, //When true it will collect Input data + 'status_code'=>true, 'user_id'=>false, 'url'=>false, ], diff --git a/src/Components/Recorder.php b/src/Components/Recorder.php index 0d899c3..e5ab032 100644 --- a/src/Components/Recorder.php +++ b/src/Components/Recorder.php @@ -39,11 +39,14 @@ public function record(Exception $e) 'trace' => $e->getTraceAsString(), ]; - $opts['status_code'] = $this->getStatusCode($e); - $opts['user_id'] = $this->getUserId(); - $opts['method'] = $this->getMethod(); - $opts['data'] = $this->getData(); - $opts['url'] = $this->getUrl(); + + $configDependant = ['user_id','status_code','method','data','url']; + + foreach ($configDependant as $key) { + if($this->canCollect($key)){ + $opts[$key] = $this->collect($key,$e); + } + } return ExceptionModel::create($opts); } @@ -60,13 +63,41 @@ private function canCollect($type) { return false; } + protected function collect($key,Exception $e = null){ + switch ($key) { + case 'user_id': + return $this->getUserId(); + break; + + case 'method': + return $this->getMethod(); + break; + + case 'status_code': + return $this->getStatusCode($e); + break; + + case 'url': + return $this->getUrl(); + break; + + case 'data': + return $this->getData(); + break; + + default: + throw new Exception("{$key} is not supported! Therefore it cannot be collected!"); + break; + } + } + /** * Gets the ID of the User that is logged in * @return integer|null The ID of the User or Null if not logged in */ protected function getUserId() { $user = Auth::user(); - if ($this->canCollect('user_id') && is_object($user)) { + if (is_object($user)) { return $user->id; } else { return null; @@ -79,7 +110,7 @@ protected function getUserId() { */ protected function getMethod() { $method = Request::method(); - if ($this->canCollect('method') && !empty($method)) { + if (!empty($method)) { return $method; } else { return null; @@ -92,7 +123,7 @@ protected function getMethod() { */ protected function getData() { $data = Input::all(); - if ($this->canCollect('data') && is_array($data)) { + if (is_array($data)) { return $data; } else { return null; @@ -105,7 +136,7 @@ protected function getData() { */ protected function getUrl() { $url = Request::url(); - if ($this->canCollect('url') && is_array($url)) { + if (is_string($url)) { return $url; } else { return null; diff --git a/src/Models/ExceptionModel.php b/src/Models/ExceptionModel.php index a8d4c03..73bc40c 100755 --- a/src/Models/ExceptionModel.php +++ b/src/Models/ExceptionModel.php @@ -13,4 +13,12 @@ public function __construct(array $attributes = []) $this->table = config('lern.record.table'); parent::__construct($attributes); } + + public function setDataAttribute($value){ + $this->attributes['data'] = json_encode($value); + } + + public function getDataAttribute($value){ + return json_decode($value); + } } \ No newline at end of file diff --git a/tests/NotifierTest.php b/tests/NotifierTest.php index 543f425..6640436 100755 --- a/tests/NotifierTest.php +++ b/tests/NotifierTest.php @@ -62,7 +62,7 @@ public function testNotifierReturnsTheCorrectSubjectWhenUsingClosure(){ $this->assertEquals($result,"This is a test"); } - public function itReturnsTheCorrectSubjectWhenUsingString(){ + public function testItReturnsTheCorrectSubjectWhenUsingString(){ $this->notifier->setSubject("This is a test"); $result = $this->notifier->getSubject(new Exception); $this->assertEquals($result,"This is a test"); diff --git a/tests/migrations/2016_03_17_000000_create_lern_tables.php b/tests/migrations/2016_03_17_000000_create_lern_tables_for_tests.php similarity index 100% rename from tests/migrations/2016_03_17_000000_create_lern_tables.php rename to tests/migrations/2016_03_17_000000_create_lern_tables_for_tests.php From c02ed6b617008b258812a124d827f37bbb4326ce Mon Sep 17 00:00:00 2001 From: Tyler Arbon Date: Sun, 27 Mar 2016 15:15:38 -0600 Subject: [PATCH 07/11] Altered tests. Removed unnecessary break statements --- src/Components/Recorder.php | 11 ----------- tests/TestCase.php | 9 +++++---- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/Components/Recorder.php b/src/Components/Recorder.php index e5ab032..09eda7f 100644 --- a/src/Components/Recorder.php +++ b/src/Components/Recorder.php @@ -67,27 +67,16 @@ protected function collect($key,Exception $e = null){ switch ($key) { case 'user_id': return $this->getUserId(); - break; - case 'method': return $this->getMethod(); - break; - case 'status_code': return $this->getStatusCode($e); - break; - case 'url': return $this->getUrl(); - break; - case 'data': return $this->getData(); - break; - default: throw new Exception("{$key} is not supported! Therefore it cannot be collected!"); - break; } } diff --git a/tests/TestCase.php b/tests/TestCase.php index cbee2d4..0c651b3 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -107,10 +107,11 @@ protected function getEnvironmentSetUp($app) $app['config']->set('lern.record', [ 'table'=>'vendor_tylercd100_lern_exceptions', 'collect'=>[ - 'method'=>false,//When true it will collect GET, POST, DELETE, PUT, etc... - 'data'=>false,//When true it will collect Input data - 'user_id'=>false, - 'url'=>false, + 'method'=>true,//When true it will collect GET, POST, DELETE, PUT, etc... + 'data'=>true,//When true it will collect Input data + 'status_code'=>true, + 'user_id'=>true, + 'url'=>true, ], ]); From 1687ecfe1920521f8248c1e8c92e4d3b8e885d7e Mon Sep 17 00:00:00 2001 From: Scrutinizer Auto-Fixer Date: Sun, 27 Mar 2016 21:21:23 +0000 Subject: [PATCH 08/11] Scrutinizer Auto-Fixes This commit consists of patches automatically generated for this project on https://scrutinizer-ci.com --- src/Components/Recorder.php | 9 ++++++--- src/Models/ExceptionModel.php | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Components/Recorder.php b/src/Components/Recorder.php index 09eda7f..a62305c 100644 --- a/src/Components/Recorder.php +++ b/src/Components/Recorder.php @@ -40,11 +40,11 @@ public function record(Exception $e) ]; - $configDependant = ['user_id','status_code','method','data','url']; + $configDependant = ['user_id', 'status_code', 'method', 'data', 'url']; foreach ($configDependant as $key) { - if($this->canCollect($key)){ - $opts[$key] = $this->collect($key,$e); + if ($this->canCollect($key)) { + $opts[$key] = $this->collect($key, $e); } } @@ -63,6 +63,9 @@ private function canCollect($type) { return false; } + /** + * @param string $key + */ protected function collect($key,Exception $e = null){ switch ($key) { case 'user_id': diff --git a/src/Models/ExceptionModel.php b/src/Models/ExceptionModel.php index 73bc40c..7167fb7 100755 --- a/src/Models/ExceptionModel.php +++ b/src/Models/ExceptionModel.php @@ -14,11 +14,11 @@ public function __construct(array $attributes = []) parent::__construct($attributes); } - public function setDataAttribute($value){ + public function setDataAttribute($value) { $this->attributes['data'] = json_encode($value); } - public function getDataAttribute($value){ + public function getDataAttribute($value) { return json_decode($value); } } \ No newline at end of file From e53b667b05e816ecc0aab832b1eb550198d0c82f Mon Sep 17 00:00:00 2001 From: Tyler Arbon Date: Sun, 27 Mar 2016 15:37:36 -0600 Subject: [PATCH 09/11] Update README.md --- README.md | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 74a3d3b..6ce0e7a 100755 --- a/README.md +++ b/README.md @@ -21,6 +21,14 @@ Currently supported notification channels via [Monolog](https://github.com/Selda - [Plivo](https://www.plivo.com/) an SMS messaging service. - [Twilio](https://www.twilio.com/) an SMS messaging service. +## Migrating from `2.x` to `3.x` +Version 3.x introduces the ability to collect more information from the error such as the user_id, url, method, and input data. In order to use 3.x you will need to copy over the new [config file](https://github.com/tylercd100/lern/blob/master/config/lern.php), the migration file and then migrate it. +```php +# This will only copy over the migration file. For the config file you can either include the --force flag (Which will overwrite it) or copy it manually from github +php artisan vendor:publish --provider="Tylercd100\LERN\LERNServiceProvider" +php artisan migrate +``` + ## Installation Install via [composer](https://getcomposer.org/) - In the terminal: @@ -32,12 +40,12 @@ Now add the following to the `providers` array in your `config/app.php` ```php Tylercd100\LERN\LERNServiceProvider::class ``` + and this to the `aliases` array in `config/app.php` ```php "LERN" => "Tylercd100\LERN\Facades\LERN", ``` - Then you will need to run these commands in the terminal in order to copy the config and migration files ```bash php artisan vendor:publish --provider="Tylercd100\LERN\LERNServiceProvider" @@ -71,7 +79,21 @@ You can call `LERN::record($exception);` to record an Exception to the database. To query any Exception that has been recorded you can use `ExceptionModel` which is an Eloquent Model ```php use Tylercd100\LERN\Model\ExceptionModel; -$mostRecentException = ExceptionModel::orderBy('created_at','DESC')->first() +$mostRecentException = ExceptionModel::orderBy('created_at','DESC')->first(); +``` + +To change what is recorded in to the database take a look at `config/lern.php` +```php +'record'=>[ + 'table'=>'vendor_tylercd100_lern_exceptions', + 'collect'=>[ + 'method'=>false, //When true it will collect GET, POST, DELETE, PUT, etc... + 'data'=>false, //When true it will collect Input data + 'status_code'=>true, + 'user_id'=>false, + 'url'=>false, + ], +], ``` ### Notifications From 7bf76f336a079d0c74d1a690cd2f1b65f93af114 Mon Sep 17 00:00:00 2001 From: Tyler Arbon Date: Sun, 27 Mar 2016 15:52:16 -0600 Subject: [PATCH 10/11] Update CHANGELOG.md --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d65a26..1ee42c0 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ All notable changes to `LERN` will be documented in this file. +### 3.0.0 +- When enabled in the config file you can now collect: + - user_id - The id of the currently logged in user. + - method - Then method of the request: GET, POST, DELETE, PUT, etc... + - url - The full URL of the request. + - data - The input data of the request, if any. + +__Reason for Major release: 3.0.0 introduces a new migration file and structure changes that could cause issues for 2.x users__ + ### 2.3.0 - Added support for [Twilio](https://www.twilio.com/) an SMS messaging service. From 11edb7263f9fa7ceb67f02cfb76a9e906c4fcefb Mon Sep 17 00:00:00 2001 From: Tyler Arbon Date: Sun, 27 Mar 2016 15:52:38 -0600 Subject: [PATCH 11/11] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ee42c0..7206415 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ All notable changes to `LERN` will be documented in this file. - url - The full URL of the request. - data - The input data of the request, if any. -__Reason for Major release: 3.0.0 introduces a new migration file and structure changes that could cause issues for 2.x users__ +*__Reason for Major release: 3.0.0 introduces a new migration file and structure changes that could cause issues for 2.x users__* ### 2.3.0 - Added support for [Twilio](https://www.twilio.com/) an SMS messaging service.