Repository: dmitry-ivanov/laravel-db-profiler Branch: master Commit: 29ec5739a081 Files: 29 Total size: 22.0 KB Directory structure: gitextract__81p91l1/ ├── .editorconfig ├── .gitattributes ├── .github/ │ ├── ISSUE_TEMPLATE/ │ │ ├── 1_Bug_Report.md │ │ ├── 2_Feature_Request.md │ │ └── 3_Laravel_Version_Support.md │ ├── PULL_REQUEST_TEMPLATE.md │ └── workflows/ │ └── tests.yml ├── .gitignore ├── LICENSE.md ├── README.md ├── art/ │ ├── sponsor-laravel-idea.pxd/ │ │ ├── QuickLook/ │ │ │ ├── Icon.tiff │ │ │ └── Thumbnail.tiff │ │ ├── data/ │ │ │ └── originalImportedContentDocumentInfo │ │ └── metadata.info │ └── sponsor-material-theme.pxd/ │ ├── QuickLook/ │ │ ├── Icon.tiff │ │ └── Thumbnail.tiff │ ├── data/ │ │ ├── 0D6D801E-30F3-46BF-815B-DC27D2EB5BE0 │ │ └── originalImportedContentDocumentInfo │ └── metadata.info ├── composer.json ├── config/ │ └── db-profiler.php ├── phpunit.xml.dist ├── src/ │ ├── DbProfilerDumper.php │ └── DbProfilerServiceProvider.php └── tests/ ├── ConsoleProfilingTest.php ├── HttpProfilingTest.php ├── TestCase.php └── fixture/ ├── app/ │ └── Post.php └── database/ └── migrations/ └── 2016_11_01_131415_create_posts_table.php ================================================ FILE CONTENTS ================================================ ================================================ FILE: .editorconfig ================================================ 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_size = 2 ================================================ FILE: .gitattributes ================================================ * text=auto /.github export-ignore /doc export-ignore /tests export-ignore .editorconfig export-ignore .gitattributes export-ignore .gitignore export-ignore phpunit.xml.dist export-ignore ================================================ FILE: .github/ISSUE_TEMPLATE/1_Bug_Report.md ================================================ --- name: "🐞 Bug Report" about: 'If you want to report an issue.' --- ### Versions: - Package Version: #.#.# - PHP Version: #.#.# ### Description: ... ### Steps to reproduce: ... #### Expected: ... #### Actual: ... ================================================ FILE: .github/ISSUE_TEMPLATE/2_Feature_Request.md ================================================ --- name: "💡 Feature Request" about: 'If you want to propose an idea.' --- ### Description: ... ================================================ FILE: .github/ISSUE_TEMPLATE/3_Laravel_Version_Support.md ================================================ --- name: "🛠 Laravel Version Support" about: 'If you want to support a new Laravel version.' --- ### Follow these steps: > Please, note that we have to update versions for everything, not only `illuminate` packages. 1. Update versions in `composer.json` according to Laravel's `composer.json`. 2. Add the newly supported version to `README.md`. 3. Open PR to the `master` branch. ================================================ FILE: .github/PULL_REQUEST_TEMPLATE.md ================================================ ================================================ FILE: .github/workflows/tests.yml ================================================ name: tests on: [push, pull_request] jobs: tests: runs-on: ubuntu-latest strategy: fail-fast: false matrix: php: [8.2, 8.3, 8.4] stability: [prefer-lowest, prefer-stable] name: PHP ${{ matrix.php }} / ${{ matrix.stability }} steps: - name: Checkout code uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} extensions: sqlite3 tools: composer:v2 coverage: xdebug - name: Setup problem matchers run: | echo "::add-matcher::${{ runner.tool_cache }}/php.json" echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - name: Install dependencies uses: nick-invision/retry@v2 with: timeout_minutes: 5 max_attempts: 5 command: composer update --prefer-dist --${{ matrix.stability }} --no-interaction --no-progress --ansi - name: Run tests run: vendor/bin/phpunit --colors=always --coverage-clover ./build/logs/clover.xml - name: Code coverage uses: codecov/codecov-action@v3 with: token: ${{ secrets.CODECOV_TOKEN }} files: ./build/logs/clover.xml fail_ci_if_error: true ================================================ FILE: .gitignore ================================================ /.idea /.phpunit.cache /.vscode /vendor .DS_Store .phpunit.result.cache Thumbs.db composer.lock composer.phar phpunit.xml ================================================ FILE: LICENSE.md ================================================ The MIT License (MIT) Copyright (c) Dmitry Ivanov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ ![Database Profiler for Laravel Web and Console Applications](art/1380x575-optimized.jpg) # Laravel Database Profiler [Buy me a coffee](https://buymeacoffee.com/dmitry.ivanov) [![StyleCI](https://github.styleci.io/repos/68023936/shield?branch=master&style=flat)](https://github.styleci.io/repos/68023936?branch=master) [![Build Status](https://img.shields.io/github/actions/workflow/status/dmitry-ivanov/laravel-db-profiler/tests.yml?branch=master)](https://github.com/dmitry-ivanov/laravel-db-profiler/actions?query=workflow%3Atests+branch%3Amaster) [![Coverage Status](https://img.shields.io/codecov/c/github/dmitry-ivanov/laravel-db-profiler/master)](https://app.codecov.io/gh/dmitry-ivanov/laravel-db-profiler/tree/master) ![Packagist Version](https://img.shields.io/packagist/v/illuminated/db-profiler) ![Packagist Stars](https://img.shields.io/packagist/stars/illuminated/db-profiler) ![Packagist Downloads](https://img.shields.io/packagist/dt/illuminated/db-profiler) ![Packagist License](https://img.shields.io/packagist/l/illuminated/db-profiler) Database Profiler for Laravel Web and Console Applications. > A simple tool that works correctly even with `dd()` in your code. | Laravel | Database Profiler | |---------|------------------------------------------------------------------------| | 12.x | [12.x](https://github.com/dmitry-ivanov/laravel-db-profiler/tree/12.x) | | 11.x | [11.x](https://github.com/dmitry-ivanov/laravel-db-profiler/tree/11.x) | | 10.x | [10.x](https://github.com/dmitry-ivanov/laravel-db-profiler/tree/10.x) | | 9.x | [9.x](https://github.com/dmitry-ivanov/laravel-db-profiler/tree/9.x) | | 8.x | [8.x](https://github.com/dmitry-ivanov/laravel-db-profiler/tree/8.x) | | 7.x | [7.x](https://github.com/dmitry-ivanov/laravel-db-profiler/tree/7.x) | | 6.x | [6.x](https://github.com/dmitry-ivanov/laravel-db-profiler/tree/6.x) | | 5.8.* | [5.8.*](https://github.com/dmitry-ivanov/laravel-db-profiler/tree/5.8) | | 5.7.* | [5.7.*](https://github.com/dmitry-ivanov/laravel-db-profiler/tree/5.7) | | 5.6.* | [5.6.*](https://github.com/dmitry-ivanov/laravel-db-profiler/tree/5.6) | | 5.5.* | [5.5.*](https://github.com/dmitry-ivanov/laravel-db-profiler/tree/5.5) | | 5.4.* | [5.4.*](https://github.com/dmitry-ivanov/laravel-db-profiler/tree/5.4) | | 5.3.* | [5.3.*](https://github.com/dmitry-ivanov/laravel-db-profiler/tree/5.3) | | 5.2.* | [5.2.*](https://github.com/dmitry-ivanov/laravel-db-profiler/tree/5.2) | | 5.1.* | [5.1.*](https://github.com/dmitry-ivanov/laravel-db-profiler/tree/5.1) | ## Usage 1. Install the package via Composer: ```shell script composer require illuminated/db-profiler ``` 2. Use the `vvv` parameter for Web: ![Laravel Database Profiler - Demo - Web](doc/img/demo-web-c.gif) 3. Use the `-vvv` option for Console: ![Laravel Database Profiler - Demo - Console](doc/img/demo-console.gif) ## Local by default Enabled only for the `local` environment, so you don't have to worry about `production`. If you want to force profiling for non-local environments - specify it explicitly in your `.env` file: > DB_PROFILER_FORCE=true ## Sponsors [![Laravel Idea](art/sponsor-laravel-idea.png)](https://laravel-idea.com)
[![Material Theme UI Plugin](art/sponsor-material-theme.png)](https://material-theme.com)
## License Laravel Database Profiler is open-sourced software licensed under the [MIT license](LICENSE.md). [Buy me a coffee](https://buymeacoffee.com/dmitry.ivanov)  ================================================ FILE: composer.json ================================================ { "name": "illuminated/db-profiler", "description": "Database Profiler for Laravel Web and Console Applications.", "keywords": ["laravel", "db", "database", "profiler", "profiling", "query", "sql", "mysql", "web-profiling", "console-profiling"], "license": "MIT", "support": { "issues": "https://github.com/dmitry-ivanov/laravel-db-profiler/issues", "source": "https://github.com/dmitry-ivanov/laravel-db-profiler" }, "authors": [{ "name": "Dmitry Ivanov", "email": "dmitry.g.ivanov@gmail.com" }], "require": { "php": "^8.2", "illuminate/database": "^12.0", "illuminate/support": "^12.0" }, "require-dev": { "phpunit/phpunit": "^11.5.3", "mockery/mockery": "^1.6.10", "orchestra/testbench": "^10.0", "illuminated/testing-tools": "^12.0", "illuminated/helper-functions": "^12.0" }, "autoload": { "psr-4": { "Illuminated\\Database\\": "src/" } }, "autoload-dev": { "psr-4": { "Illuminated\\Database\\Tests\\": "tests/", "Illuminated\\Database\\Tests\\App\\": "tests/fixture/app/" } }, "extra": { "laravel": { "providers": [ "Illuminated\\Database\\DbProfilerServiceProvider" ] } }, "config": { "sort-packages": true }, "minimum-stability": "dev", "prefer-stable": true } ================================================ FILE: config/db-profiler.php ================================================ env('DB_PROFILER_FORCE', false), ]; ================================================ FILE: phpunit.xml.dist ================================================ ./tests ./src ./src/DbProfilerDumper.php ================================================ FILE: src/DbProfilerDumper.php ================================================ mergeConfigFrom(__DIR__.'/../config/db-profiler.php', 'db-profiler'); app()->instance('db.idpdumper', new DbProfilerDumper()); } /** * Boot the service provider. */ public function boot(): void { if (!$this->isEnabled()) { return; } DB::listen(function (QueryExecuted $query) { $i = $this->counter++; $sql = $this->applyQueryBindings($query->sql, $query->bindings); $time = $query->time; app('db.idpdumper')->dump("[{$i}]: {$sql}; ({$time} ms)"); }); } /** * Check whether database profiling is enabled or not. */ private function isEnabled(): bool { if (!$this->app->isLocal() && !config('db-profiler.force')) { return false; } return $this->app->runningInConsole() ? collect($_SERVER['argv'])->contains('-vvv') : Request::exists('vvv'); } /** * Apply query bindings to the given SQL query. */ private function applyQueryBindings(string $sql, array $bindings): string { $bindings = collect($bindings)->map(function ($binding) { return match (gettype($binding)) { 'boolean' => (int) $binding, 'string' => "'{$binding}'", default => $binding, }; })->toArray(); return Str::replaceArray('?', $bindings, $sql); } } ================================================ FILE: tests/ConsoleProfilingTest.php ================================================ notLocal()->boot(); $this->assertDbProfilerNotActivated(); } #[Test] public function it_is_disabled_if_environment_is_local_but_there_is_no_vvv_option(): void { $this->local()->boot(); $this->assertDbProfilerNotActivated(); } #[Test] public function it_is_enabled_if_environment_is_local_and_there_is_vvv_option(): void { $this->local()->withVvv()->boot(); $this->assertDbProfilerActivated(); $this->assertDbQueriesDumped(); } #[Test] public function it_is_enabled_if_environment_is_not_local_but_there_is_a_force_flag_in_config(): void { config(['db-profiler.force' => true]); $this->notLocal()->withVvv()->boot(); $this->assertDbProfilerActivated(); $this->assertDbQueriesDumped(); } } ================================================ FILE: tests/HttpProfilingTest.php ================================================ true]); return $this; } #[Test] public function it_is_disabled_if_environment_is_not_local(): void { $this->notLocal()->boot(); $this->assertDbProfilerNotActivated(); } #[Test] public function it_is_disabled_if_environment_is_local_but_there_is_no_vvv_request_param(): void { $this->local()->boot(); $this->assertDbProfilerNotActivated(); } #[Test] public function it_is_enabled_if_environment_is_local_and_there_is_vvv_request_param(): void { $this->local()->withVvv()->boot(); $this->assertDbProfilerActivated(); $this->assertDbQueriesDumped(); } } ================================================ FILE: tests/TestCase.php ================================================ setUpDatabase(); } /** * Setup the database. */ private function setUpDatabase(): void { config(['database.default' => 'testing']); config(['database.connections.testing.foreign_key_constraints' => true]); $this->artisan('migrate', [ '--database' => 'testing', '--path' => relative_path(__DIR__, base_path()) . '/fixture/database/migrations/', ])->assertSuccessful(); } /** * Emulate the local environment. */ protected function local(): self { $this->env = 'local'; return $this; } /** * Emulate the non-local environment. */ protected function notLocal(): self { $this->env = 'production'; return $this; } /** * Define whether the app is running in console or not. */ abstract protected function runningInConsole(): bool; /** * Emulate the "vvv" flag set. */ abstract protected function withVvv(): self; /** * Emulate the app boot. */ protected function boot(): void { $app = mock(Application::class); $app->expects('isLocal')->andReturn($this->env == 'local'); $app->allows('runningInConsole')->andReturn($this->runningInConsole()); $app->allows('configurationIsCached')->andReturnTrue(); $serviceProvider = new DbProfilerServiceProvider($app); $serviceProvider->register(); $serviceProvider->boot(); } /** * Assert that the database profiler is activated. */ protected function assertDbProfilerActivated(): void { $connection = DB::connection(); $dispatcher = $connection->getEventDispatcher(); $this->assertTrue($dispatcher->hasListeners(QueryExecuted::class)); } /** * Assert that the database profiler is not activated. */ protected function assertDbProfilerNotActivated(): void { $connection = DB::connection(); $dispatcher = $connection->getEventDispatcher(); $this->assertFalse($dispatcher->hasListeners(QueryExecuted::class)); } /** * Assert that the database queries are dumped. */ protected function assertDbQueriesDumped(): void { $queries = collect([ '[1]: select * from "posts"', '[2]: select * from "posts" where "posts"."id" = 1 limit 1', '[3]: select * from "posts" where "posts"."id" = \'2\' limit 1', '[4]: select * from "posts" where "title" = \'foo bar baz\'', '[5]: select * from "posts" where "price" > 123.45', '[6]: select * from "posts" where "price" < \'543.21\'', '[7]: select * from "posts" where "is_enabled" = 1', '[8]: select * from "posts" where "is_enabled" = 0', '[9]: select * from "posts" where "is_enabled" = 1', '[10]: select * from "posts" where "is_enabled" = \'1\'', '[11]: select * from "posts" where "id" in (1, \'2\', 3)', '[12]: select * from "posts" where "title" in (\'foo\', \'bar\', \'baz\')', '[13]: select * from "posts" where "price" in (1.23, \'2.34\', 3.45)', '[14]: select * from "posts" where "is_enabled" in (1, 0, 1, 0, \'1\', \'0\')', '[15]: select * from "posts" where "id" > 3 and "title" = \'foo bar baz\' and "price" > 123.45 and "is_enabled" = 1 and "created_at" > \'2016-11-03 21:00:00\' limit 1', '[16]: select * from "posts" where "title" is null', '[17]: select * from "posts" where "title" is null and "price" > 123.45', ]); $mock = Mockery::mock(); app()->instance('db.idpdumper', $mock); $queries->each(function (string $query) use ($mock) { $queryPattern = $this->prepareQueryPattern($query); $mock->shouldReceive('dump')->with(Mockery::pattern($queryPattern))->once(); }); Post::all(); Post::query()->find(1); Post::query()->find('2'); Post::query()->where('title', 'foo bar baz')->get(); Post::query()->where('price', '>', 123.45)->get(); Post::query()->where('price', '<', '543.21')->get(); Post::query()->where('is_enabled', true)->get(); Post::query()->where('is_enabled', false)->get(); Post::query()->where('is_enabled', 1)->get(); Post::query()->where('is_enabled', '1')->get(); Post::query()->whereIn('id', [1, '2', 3])->get(); Post::query()->whereIn('title', ['foo', 'bar', 'baz'])->get(); Post::query()->whereIn('price', [1.23, '2.34', 3.45])->get(); Post::query()->whereIn('is_enabled', [true, false, 1, 0, '1', '0'])->get(); Post::query()->where('id', '>', 3)->where('title', 'foo bar baz')->where('price', '>', 123.45)->where('is_enabled', true)->where('created_at', '>', '2016-11-03 21:00:00')->first(); Post::query()->where('title', null)->get(); Post::query()->where('title', null)->where('price', '>', 123.45)->get(); } /** * Prepare the query pattern for mocking. */ private function prepareQueryPattern(string $query): string { $query = preg_quote($query, '/'); return "/{$query}; \(.*? ms\)/"; } /** * Clean up the testing environment before the next test. */ protected function tearDown(): void { $connection = DB::connection(); $dispatcher = $connection->getEventDispatcher(); if ($dispatcher->hasListeners(QueryExecuted::class)) { $dispatcher->forget(QueryExecuted::class); } parent::tearDown(); } } ================================================ FILE: tests/fixture/app/Post.php ================================================ increments('id'); $table->string('title'); $table->float('price'); $table->boolean('is_enabled'); $table->timestamps(); }); } /** * Rollback the migration. */ public function down(): void { Schema::drop('posts'); } };