Skip to content

Commit ab6330b

Browse files
committed
Initial commit
0 parents  commit ab6330b

File tree

9 files changed

+237
-0
lines changed

9 files changed

+237
-0
lines changed

.editorconfig

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 4
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
# The JSON files contain newlines inconsistently
13+
[*.json]
14+
insert_final_newline = ignore
15+
16+
# Minified JavaScript files shouldn't be changed
17+
[**.min.js]
18+
indent_style = ignore
19+
insert_final_newline = ignore
20+
21+
# Makefiles always use tabs for indentation
22+
[Makefile]
23+
indent_style = tab
24+
25+
[*.phtml]
26+
indent_size = 2
27+
28+
[*.md]
29+
trim_trailing_whitespace = false

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
composer.phar
2+
composer.lock
3+
/vendor/
4+
5+
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
6+
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
7+
# composer.lock

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Trần Xuân Thanh
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Nette-eloquent
2+
Eloquent Bridge for Nette Framework
3+
4+
[![StyleCI](https://github.styleci.io/repos/372696270/shield?branch=main)](https://github.styleci.io/repos/372696270?branch=main)
5+
6+
7+
## Installation
8+
```sh
9+
composer require ging-dev/nette-eloquent
10+
```
11+
12+
## Configuration
13+
```
14+
extensions:
15+
eloquent: Gingdev\NetteExtension\EloquentExtension
16+
17+
18+
eloquent:
19+
driver: sqlite
20+
database: %appDir%/database.db
21+
```
22+
23+
## Example
24+
```php
25+
<?php
26+
27+
declare(strict_types=1);
28+
29+
namespace App\Presenters;
30+
31+
use Illuminate\Database\Capsule\Manager;
32+
use Nette;
33+
34+
35+
final class HomepagePresenter extends Nette\Application\UI\Presenter
36+
{
37+
/** @var Manager */
38+
protected $database;
39+
40+
public function injectDatabase(Manager $database) {
41+
$this->database = $database;
42+
}
43+
44+
public function actionDefault()
45+
{
46+
$this->database->schema()->drop('users');
47+
$this->database->schema()->create('users', function ($table) {
48+
$table->increments('id');
49+
$table->string('name')->unique();
50+
});
51+
$this->database->table('users')->select('*')
52+
->where('name', 'gingdev')
53+
->get();
54+
}
55+
}
56+
```

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "ging-dev/nette-eloquent",
3+
"description": "Eloquent ORM for Nette Framework",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "ging-dev",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"minimum-stability": "stable",
13+
"require": {
14+
"illuminate/database": "^8.44",
15+
"illuminate/events": "^8.44"
16+
},
17+
"require-dev": {
18+
"nette/utils": "^3.2",
19+
"tracy/tracy": "^2.8",
20+
"nette/di": "^3.0"
21+
},
22+
"autoload": {
23+
"classmap": ["src/"]
24+
}
25+
}

src/EloquentExtension.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gingdev\NetteExtension;
6+
7+
use Illuminate\Database\Capsule\Manager;
8+
use Nette\DI\CompilerExtension;
9+
10+
class EloquentExtension extends CompilerExtension
11+
{
12+
public function loadConfiguration()
13+
{
14+
$container = $this->getContainerBuilder();
15+
$config = $this->getConfig();
16+
17+
$connection = $container->addDefinition($this->prefix('connection'))
18+
->setFactory(Manager::class)
19+
->addSetup('addConnection', [$config])
20+
->addSetup('setAsGlobal')
21+
->addSetup('bootEloquent')
22+
->setAutowired(true);
23+
24+
if ($container->parameters['debugMode']) {
25+
$panel = $container->addDefinition($this->prefix('panel'))
26+
->setFactory(EloquentPanel::class);
27+
$connection->addSetup([$panel, 'register'], [$connection]);
28+
}
29+
}
30+
}

src/EloquentPanel.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gingdev\NetteExtension;
6+
7+
use Illuminate\Database\Capsule\Manager;
8+
use Illuminate\Events\Dispatcher;
9+
use Nette\Utils\Helpers;
10+
use Tracy\Debugger;
11+
use Tracy\IBarPanel;
12+
13+
class EloquentPanel implements IBarPanel
14+
{
15+
private $queries = [];
16+
17+
public function register(Manager $connection)
18+
{
19+
Debugger::getBar()->addPanel($this);
20+
$connection->connection()->setEventDispatcher(new Dispatcher());
21+
$connection->connection()->listen(function ($query) {
22+
$this->queries[] = $query;
23+
});
24+
}
25+
26+
public function getTab()
27+
{
28+
$count = count($this->queries);
29+
30+
if (!$count) {
31+
return;
32+
}
33+
34+
return Helpers::capture(function () use ($count) {
35+
require __DIR__.'/templates/tab.phtml';
36+
});
37+
}
38+
39+
public function getPanel()
40+
{
41+
return Helpers::capture(function () {
42+
$queries = $this->queries;
43+
require __DIR__.'/templates/panel.phtml';
44+
});
45+
}
46+
}

src/templates/panel.phtml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<h1>SQL Queries</h1>
2+
<div class="tracy-inner tracy-Eloquent">
3+
<table>
4+
<tr>
5+
<th>SQL</th>
6+
<th>Bindings</th>
7+
<th>Time (ms)</th>
8+
</tr>
9+
<?php foreach($queries as $query): ?>
10+
<tr>
11+
<td><?= $query->sql ?></td>
12+
<td><pre><?= implode(', ', $query->bindings) ?></pre></td>
13+
<td><?= $query->time ?></td>
14+
</tr>
15+
<?php endforeach; ?>
16+
</table>
17+
</div>

src/templates/tab.phtml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<span title="Eloquent">
2+
<svg viewBox="0 0 2048 2048" style="vertical-align: bottom; width:1.23em; height:1.55em">
3+
<path fill="#b079d6" d="M1024 896q237 0 443-43t325-127v170q0 69-103 128t-280 93.5-385 34.5-385-34.5-280-93.5-103-128v-170q119 84 325 127t443 43zm0 768q237 0 443-43t325-127v170q0 69-103 128t-280 93.5-385 34.5-385-34.5-280-93.5-103-128v-170q119 84 325 127t443 43zm0-384q237 0 443-43t325-127v170q0 69-103 128t-280 93.5-385 34.5-385-34.5-280-93.5-103-128v-170q119 84 325 127t443 43zm0-1152q208 0 385 34.5t280 93.5 103 128v128q0 69-103 128t-280 93.5-385 34.5-385-34.5-280-93.5-103-128v-128q0-69 103-128t280-93.5 385-34.5z"/>
4+
</svg>
5+
<span class="tracy-label" title="Eloquent">Queries (<?= $count ?>)</span>
6+
</span>

0 commit comments

Comments
 (0)