From 93c869790610525a5667f4a6cea1ae17be47cdad Mon Sep 17 00:00:00 2001 From: Maksim Lanin Date: Fri, 13 Oct 2017 16:00:11 +0300 Subject: [PATCH] Add 5.5 support --- .editorconfig | 16 ++ .php_cs | 104 +++-------- .travis.yml | 17 +- README.md | 13 +- composer.json | 15 +- config/api-debugger.php | 24 +-- src/Collections/CacheCollection.php | 114 ++++++------ src/Collections/ProfilingCollection.php | 18 +- src/Collections/QueriesCollection.php | 8 +- src/Debugger.php | 78 ++++----- src/Events/StartProfiling.php | 28 +-- src/Events/StopProfiling.php | 28 +-- src/ServiceProvider.php | 48 ++--- src/Support/helpers.php | 34 ++-- tests/DebuggerTest.php | 224 ++++++++++++------------ tests/QueriesCollectionTest.php | 6 +- 16 files changed, 375 insertions(+), 400 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..1492202 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.yml] +indent_style = space +indent_size = 2 diff --git a/.php_cs b/.php_cs index 20c0f6a..a51da8c 100644 --- a/.php_cs +++ b/.php_cs @@ -1,82 +1,28 @@ true, + // addtional rules + 'array_syntax' => ['syntax' => 'short'], + 'no_multiline_whitespace_before_semicolons' => true, + 'no_short_echo_tag' => true, + 'no_unused_imports' => true, + 'not_operator_with_successor_space' => true, +]; +$excludes = [ + // add exclude project directory + 'vendor', ]; -$finder = Symfony\CS\Finder\DefaultFinder::create() - ->exclude('vendor') - ->exclude('storage') - ->in(__DIR__); - -return Config::create() - ->finder($finder) - ->fixers($fixers) - ->level(FixerInterface::NONE_LEVEL) - ->setUsingCache(true); \ No newline at end of file +return PhpCsFixer\Config::create() + ->setRules($rules) + ->setFinder( + PhpCsFixer\Finder::create() + ->exclude($excludes) + ->in(__DIR__) + ->notName('README.md') + ->notName('*.xml') + ->notName('*.yml') + ); diff --git a/.travis.yml b/.travis.yml index 273374e..bfb9c2c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,19 +3,26 @@ dist: trusty language: php php: + - 5.5 - 5.6 - 7.0 - - hhvm + - 7.1 sudo: false +cache: + directories: + - $HOME/.composer/cache + install: - travis_retry composer install --no-interaction --prefer-dist +before_script: + - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist + script: - - if [ "$TRAVIS_PHP_VERSION" != "5.6" ]; then vendor/bin/phpunit; fi - - if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then vendor/bin/phpunit --coverage-clover build/logs/clover.xml; fi + - vendor/bin/phpcs --standard=psr2 src/ + - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover after_script: - - if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi - - if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml; fi + - if [[ $TRAVIS_PHP_VERSION != 'hhvm' && $TRAVIS_PHP_VERSION != '7.0' ]]; then php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover; fi diff --git a/README.md b/README.md index ee3cc5a..a0c99b1 100644 --- a/README.md +++ b/README.md @@ -58,9 +58,8 @@ When you are developing JSON API sometimes you need to debug it, but if you will To get the latest version of Laravel Laravel-API-Debugger, simply add the following line to the require block of your `composer.json` file. -For Laravel 5.4 ``` -"lanin/laravel-api-debugger": "^0.3" +"lanin/laravel-api-debugger": "^3.0" ``` You'll then need to run `composer install` or `composer update` to download it and have the autoloader updated. @@ -85,7 +84,7 @@ Before extension will populate your answer it will try to distinguish if it is a Also please be careful with what you return. As if your answer will not be wrapped in any kind of `data` attribute (`pages` in the example above), frontend could be damaged because of waiting the particular set of attributes but it will return extra `debug` one. -So the best way to return your responses is like this +So the best way to return your responses is like this ```php $data = [ 'foo' => 'bar', @@ -104,7 +103,7 @@ For more info about better practices in JSON APIs you can find here http://jsona ## Debugging -Debugger's two main tasks are to dump variables and collect anny additional info about your request. +Debugger's two main tasks are to dump variables and collect anny additional info about your request. ### Var dump @@ -123,12 +122,12 @@ lad($foo, $bar); You can simultaneously dump as many vars as you want and they will appear in the answer. -**Note!** Of course it it not the best way do debug your production environment, but sometimes it is the only way. +**Note!** Of course it it not the best way do debug your production environment, but sometimes it is the only way. So be careful with this, because everyone will see your output, but at least debug will not break your clients. ### Collecting data -**Note!** By default Debugger will collect data ONLY when you set `APP_DEBUG=true`. +**Note!** By default Debugger will collect data ONLY when you set `APP_DEBUG=true`. So you don't have to worry that someone will see your system data on production. All available collections can be found in `api-debugger.php` config that you can publish and update as you wish. @@ -171,7 +170,7 @@ lad_pr_me(); ### Extending -You can easily add your own data collections to debug output. +You can easily add your own data collections to debug output. Just look at how it was done in the package itself and repeat for anything you want (for example HTTP requests). ## Contributing diff --git a/composer.json b/composer.json index 002d123..df9d3cc 100644 --- a/composer.json +++ b/composer.json @@ -12,11 +12,11 @@ ], "require": { "php": ">=5.6.0", - "illuminate/support": "~5.4" + "illuminate/support": "^5.4,<5.6" }, "require-dev": { - "phpunit/phpunit": "~5.7", - "orchestra/testbench": "~3.4", + "phpunit/phpunit": "~6.0", + "orchestra/testbench": "~3.5", "mockery/mockery": "0.9.*" }, "autoload": { @@ -33,5 +33,12 @@ } }, "minimum-stability": "dev", - "prefer-stable": true + "prefer-stable": true, + "extra": { + "laravel": { + "providers": [ + "Lanin\\Laravel\\ApiDebugger\\ServiceProvider" + ] + } + } } diff --git a/config/api-debugger.php b/config/api-debugger.php index c611b1c..959bde4 100644 --- a/config/api-debugger.php +++ b/config/api-debugger.php @@ -1,17 +1,17 @@ [ - // Database queries. - \Lanin\Laravel\ApiDebugger\Collections\QueriesCollection::class, + /** + * Specify what data to collect. + */ + 'collections' => [ + // Database queries. + \Lanin\Laravel\ApiDebugger\Collections\QueriesCollection::class, - // Show cache events. - \Lanin\Laravel\ApiDebugger\Collections\CacheCollection::class, + // Show cache events. + \Lanin\Laravel\ApiDebugger\Collections\CacheCollection::class, - // Profile custom events. - \Lanin\Laravel\ApiDebugger\Collections\ProfilingCollection::class, - ], -]; \ No newline at end of file + // Profile custom events. + \Lanin\Laravel\ApiDebugger\Collections\ProfilingCollection::class, + ], +]; diff --git a/src/Collections/CacheCollection.php b/src/Collections/CacheCollection.php index a9adca8..8050591 100644 --- a/src/Collections/CacheCollection.php +++ b/src/Collections/CacheCollection.php @@ -21,11 +21,11 @@ class CacheCollection implements Collection * @var array */ protected $events = [ - 'hit' => ['keys' => [], 'total' => 0], - 'miss' => ['keys' => [], 'total' => 0], - 'write' => ['keys' => [], 'total' => 0], - 'forget' => ['keys' => [], 'total' => 0], - ]; + 'hit' => ['keys' => [], 'total' => 0], + 'miss' => ['keys' => [], 'total' => 0], + 'write' => ['keys' => [], 'total' => 0], + 'forget' => ['keys' => [], 'total' => 0], + ]; /** * CacheCollection constructor. @@ -70,60 +70,60 @@ public function listen() $this->dispatcher->listen(KeyForgotten::class, [$this, 'forget']); } - /** - * Store hit. - * - * @param CacheHit $event - */ + /** + * Store hit. + * + * @param CacheHit $event + */ public function hit(CacheHit $event) - { - $this->store(__FUNCTION__, $event); - } - - /** - * Store miss. - * - * @param CacheMissed $event - */ - public function miss(CacheMissed $event) - { - $this->store(__FUNCTION__, $event); - } - - /** - * Store write. - * - * @param KeyWritten $event - */ - public function write(KeyWritten $event) - { - $this->store(__FUNCTION__, $event); - } - - /** - * Store forget. - * - * @param KeyForgotten $event - */ - public function forget(KeyForgotten $event) - { - $this->store(__FUNCTION__, $event); - } - - /** - * Store event. - * - * @param string $label - * @param CacheEvent $event - */ + { + $this->store(__FUNCTION__, $event); + } + + /** + * Store miss. + * + * @param CacheMissed $event + */ + public function miss(CacheMissed $event) + { + $this->store(__FUNCTION__, $event); + } + + /** + * Store write. + * + * @param KeyWritten $event + */ + public function write(KeyWritten $event) + { + $this->store(__FUNCTION__, $event); + } + + /** + * Store forget. + * + * @param KeyForgotten $event + */ + public function forget(KeyForgotten $event) + { + $this->store(__FUNCTION__, $event); + } + + /** + * Store event. + * + * @param string $label + * @param CacheEvent $event + */ protected function store($label, CacheEvent $event) - { - $tags = $event->tags; + { + $tags = $event->tags; - $this->events[$label]['keys'][] = !empty($tags) - ? ['tags' => $tags, 'key' => $event->key] - : $event->key; + $this->events[$label]['keys'][] = ! empty($tags) + ? ['tags' => $tags, 'key' => $event->key] + : $event->key; - $this->events[$label]['total']++; - } + $this->events[$label]['total']++; + } } diff --git a/src/Collections/ProfilingCollection.php b/src/Collections/ProfilingCollection.php index 2c6f813..5ba1551 100644 --- a/src/Collections/ProfilingCollection.php +++ b/src/Collections/ProfilingCollection.php @@ -62,16 +62,16 @@ public function items() public function listen() { $this->dispatcher->listen(StartProfiling::class, function (StartProfiling $event) { - $this->started[$event->name] = microtime(true); - }); + $this->started[$event->name] = microtime(true); + }); $this->dispatcher->listen(StopProfiling::class, function (StopProfiling $event) { - if (array_key_exists($event->name, $this->started)) { - $this->timers[] = [ - 'event' => $event->name, - 'time' => microtime(true) - $this->started[$event->name], - ]; - } - }); + if (array_key_exists($event->name, $this->started)) { + $this->timers[] = [ + 'event' => $event->name, + 'time' => microtime(true) - $this->started[$event->name], + ]; + } + }); } } diff --git a/src/Collections/QueriesCollection.php b/src/Collections/QueriesCollection.php index f8a693d..acd1011 100644 --- a/src/Collections/QueriesCollection.php +++ b/src/Collections/QueriesCollection.php @@ -47,10 +47,10 @@ public function name() */ public function items() { - return [ - 'total' => count($this->queries), - 'items' => $this->queries, - ]; + return [ + 'total' => count($this->queries), + 'items' => $this->queries, + ]; } /** diff --git a/src/Debugger.php b/src/Debugger.php index 11571c2..88d1a0c 100644 --- a/src/Debugger.php +++ b/src/Debugger.php @@ -22,12 +22,12 @@ class Debugger */ protected $storage; - /** - * @var Event - */ - protected $event; + /** + * @var Event + */ + protected $event; - /** + /** * Create a new Debugger service. * * @param Storage $storage @@ -61,42 +61,42 @@ public function dump() $this->storage->dump(func_get_args()); } - /** - * Start profiling event. - * - * @param string $name - * @return mixed - */ + /** + * Start profiling event. + * + * @param string $name + * @return mixed + */ public function startProfiling($name) - { - $this->event->dispatch(new StartProfiling($name)); - } - - /** - * Finish profiling event. - * - * @param string $name - */ + { + $this->event->dispatch(new StartProfiling($name)); + } + + /** + * Finish profiling event. + * + * @param string $name + */ public function stopProfiling($name) - { - $this->event->dispatch(new StopProfiling($name)); - } - - /** - * Profile action. - * - * @param string $name - * @param \Closure|null $action - * @return mixed - */ - public function profileMe($name, \Closure $action = null) - { - $this->startProfiling($name); - $return = $action(); - $this->stopProfiling($name); - - return $return; - } + { + $this->event->dispatch(new StopProfiling($name)); + } + + /** + * Profile action. + * + * @param string $name + * @param \Closure|null $action + * @return mixed + */ + public function profileMe($name, \Closure $action = null) + { + $this->startProfiling($name); + $return = $action(); + $this->stopProfiling($name); + + return $return; + } /** * Update final response. diff --git a/src/Events/StartProfiling.php b/src/Events/StartProfiling.php index b7ebc7d..73452cc 100644 --- a/src/Events/StartProfiling.php +++ b/src/Events/StartProfiling.php @@ -4,18 +4,18 @@ class StartProfiling { - /** - * @var string - */ - public $name; + /** + * @var string + */ + public $name; - /** - * StartProfiling constructor. - * - * @param string $name - */ - public function __construct($name) - { - $this->name = $name; - } -} \ No newline at end of file + /** + * StartProfiling constructor. + * + * @param string $name + */ + public function __construct($name) + { + $this->name = $name; + } +} diff --git a/src/Events/StopProfiling.php b/src/Events/StopProfiling.php index 848f25e..1faa4ab 100644 --- a/src/Events/StopProfiling.php +++ b/src/Events/StopProfiling.php @@ -4,18 +4,18 @@ class StopProfiling { - /** - * @var string - */ - public $name; + /** + * @var string + */ + public $name; - /** - * StopProfiling constructor. - * - * @param string $name - */ - public function __construct($name) - { - $this->name = $name; - } -} \ No newline at end of file + /** + * StopProfiling constructor. + * + * @param string $name + */ + public function __construct($name) + { + $this->name = $name; + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 5033236..5ea9cac 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -4,27 +4,27 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider { - /** - * Indicates if loading of the provider is deferred. - * - * @var bool - */ - protected $defer = false; + /** + * Indicates if loading of the provider is deferred. + * + * @var bool + */ + protected $defer = false; /** * Bootstrap application service. */ public function boot() { - $configPath = __DIR__ . '/../config/api-debugger.php'; - $this->publishes([$configPath => config_path('api-debugger.php')]); - $this->mergeConfigFrom($configPath, 'api-debugger'); + $configPath = __DIR__ . '/../config/api-debugger.php'; + $this->publishes([$configPath => config_path('api-debugger.php')]); + $this->mergeConfigFrom($configPath, 'api-debugger'); // Listen to database queries and inject them to debug output. - $config = $this->app['config']; + $config = $this->app['config']; if ($config['app.debug']) { - $this->registerCollections($config['api-debugger.collections'] ); - } + $this->registerCollections($config['api-debugger.collections']); + } } /** @@ -51,17 +51,17 @@ public function provides() ]; } - /** - * Register requested collections within debugger. - * - * @param Collection[] $collections - */ - protected function registerCollections(array $collections) - { - $debugger = $this->app->make(Debugger::class); + /** + * Register requested collections within debugger. + * + * @param Collection[] $collections + */ + protected function registerCollections(array $collections) + { + $debugger = $this->app->make(Debugger::class); - foreach ($collections as $collection) { - $debugger->populateWith($this->app->make($collection)); - } - } + foreach ($collections as $collection) { + $debugger->populateWith($this->app->make($collection)); + } + } } diff --git a/src/Support/helpers.php b/src/Support/helpers.php index ce90c66..fb07025 100644 --- a/src/Support/helpers.php +++ b/src/Support/helpers.php @@ -16,16 +16,16 @@ function lad() } if (! function_exists('lad_pr_start')) { - /** - * Start profiling event. - * - * @param string $name - * @param Closure|null $action - * @return void - */ + /** + * Start profiling event. + * + * @param string $name + * @param Closure|null $action + * @return void + */ function lad_pr_start($name, \Closure $action = null) { - app(Debugger::class)->startProfiling($name, $action); + app(Debugger::class)->startProfiling($name, $action); } } @@ -38,20 +38,20 @@ function lad_pr_start($name, \Closure $action = null) */ function lad_pr_stop($name) { - app(Debugger::class)->stopProfiling($name); + app(Debugger::class)->stopProfiling($name); } } if (! function_exists('lad_pr_me')) { - /** - * Finish profiling event. - * - * @param string $name - * @param Closure|null $action - * @return mixed - */ + /** + * Finish profiling event. + * + * @param string $name + * @param Closure|null $action + * @return mixed + */ function lad_pr_me($name, \Closure $action = null) { - return app(Debugger::class)->profileMe($name, $action); + return app(Debugger::class)->profileMe($name, $action); } } diff --git a/tests/DebuggerTest.php b/tests/DebuggerTest.php index 8462dc8..7086cd8 100644 --- a/tests/DebuggerTest.php +++ b/tests/DebuggerTest.php @@ -7,7 +7,7 @@ class DebuggerTest extends TestCase { - /** @test */ + /** @test */ public function it_can_dump_var_via_helper() { $this->app['router']->get('foo', function () { @@ -18,11 +18,11 @@ public function it_can_dump_var_via_helper() $this->json('get', '/foo') ->assertStatus(200) - ->assertJsonFragment([ - 'dump' => [ - 'baz', - ] - ]); + ->assertJsonFragment([ + 'dump' => [ + 'baz', + ] + ]); } /** @test */ @@ -56,110 +56,110 @@ public function it_can_dump_query() ]); } - /** @test */ - public function it_can_profile_custom_events() - { - $this->app['router']->get('foo', function () { - lad_pr_start('test'); - usleep(300); - lad_pr_stop('test'); - - return response()->json(['foo' => 'bar']); - }); - - $this->json('get', '/foo') - ->assertStatus(200) - ->assertJsonStructure([ - 'debug' => [ - 'profiling' => [ - '*' => [ - 'event', - 'time', - ], - ], - ], - ]) - ->assertJsonFragment([ - 'event' => 'test', - ]); - } - - /** @test */ - public function it_can_profile_simple_actions() - { - $this->app['router']->get('foo', function () { - lad_pr_me('test', function () { - usleep(300); - }); - - return response()->json(['foo' => 'bar']); - }); - - $this->json('get', '/foo') - ->assertStatus(200) - ->assertJsonStructure([ - 'debug' => [ - 'profiling' => [ - '*' => [ - 'event', - 'time', - ], - ], - ], - ]) - ->assertJsonFragment([ - 'event' => 'test', - ]); - } - - /** @test */ - public function it_can_show_cache_events() - { - $this->app['router']->get('foo', function () { - $value = Cache::tags('foo')->remember('bar', 60, function () { - return 'bar'; - }); - - $value = Cache::get('bar'); - - return response()->json(['foo' => $value]); - }); - - $this->json('get', '/foo') - ->assertStatus(200) - ->assertJsonStructure([ - 'debug' => [ - 'cache' => [ - 'hit' => [ - 'keys', - 'total', - ], - 'miss' => [ - 'keys', - 'total', - ], - 'write' => [ - 'keys', - 'total', - ], - 'forget' => [ - 'keys', - 'total', - ], - ] - ] - ]) - ->assertJsonFragment([ - 'miss' => [ - 'keys' => [ - [ - 'tags' => ['foo'], - 'key' => 'bar', - ], - 'bar' - ], - 'total' => 2 - ] - ]); - } + /** @test */ + public function it_can_profile_custom_events() + { + $this->app['router']->get('foo', function () { + lad_pr_start('test'); + usleep(300); + lad_pr_stop('test'); + + return response()->json(['foo' => 'bar']); + }); + + $this->json('get', '/foo') + ->assertStatus(200) + ->assertJsonStructure([ + 'debug' => [ + 'profiling' => [ + '*' => [ + 'event', + 'time', + ], + ], + ], + ]) + ->assertJsonFragment([ + 'event' => 'test', + ]); + } + + /** @test */ + public function it_can_profile_simple_actions() + { + $this->app['router']->get('foo', function () { + lad_pr_me('test', function () { + usleep(300); + }); + + return response()->json(['foo' => 'bar']); + }); + + $this->json('get', '/foo') + ->assertStatus(200) + ->assertJsonStructure([ + 'debug' => [ + 'profiling' => [ + '*' => [ + 'event', + 'time', + ], + ], + ], + ]) + ->assertJsonFragment([ + 'event' => 'test', + ]); + } + + /** @test */ + public function it_can_show_cache_events() + { + $this->app['router']->get('foo', function () { + $value = Cache::tags('foo')->remember('bar', 60, function () { + return 'bar'; + }); + + $value = Cache::get('bar'); + + return response()->json(['foo' => $value]); + }); + + $this->json('get', '/foo') + ->assertStatus(200) + ->assertJsonStructure([ + 'debug' => [ + 'cache' => [ + 'hit' => [ + 'keys', + 'total', + ], + 'miss' => [ + 'keys', + 'total', + ], + 'write' => [ + 'keys', + 'total', + ], + 'forget' => [ + 'keys', + 'total', + ], + ] + ] + ]) + ->assertJsonFragment([ + 'miss' => [ + 'keys' => [ + [ + 'tags' => ['foo'], + 'key' => 'bar', + ], + 'bar' + ], + 'total' => 2 + ] + ]); + } } diff --git a/tests/QueriesCollectionTest.php b/tests/QueriesCollectionTest.php index ee338d1..6d45b30 100644 --- a/tests/QueriesCollectionTest.php +++ b/tests/QueriesCollectionTest.php @@ -129,6 +129,6 @@ public function factory() } } -class Foo { - -} \ No newline at end of file +class Foo +{ +}