Skip to content

Commit a53c654

Browse files
authored
Merge pull request #24 from dducro/MakeDbViewInjectableByContainer
Make db view injectable by container
2 parents 1830fca + 4687580 commit a53c654

File tree

6 files changed

+269
-242
lines changed

6 files changed

+269
-242
lines changed

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/vendor
22
composer.phar
33
composer.lock
4-
.DS_Store
4+
.DS_Store
5+
.idea

Diff for: composer.json

+6-7
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
{
88
"name": "Flyn San",
99
"email": "[email protected]"
10+
},
11+
{
12+
"name": "Ebrahim Radi",
13+
"email": "[email protected]",
14+
"homepage": "http://me2bits.com"
1015
}
11-
,
12-
{
13-
"name": "Ebrahim Radi",
14-
"email": "[email protected]",
15-
"homepage": "http://me2bits.com"
16-
}
1716
],
1817
"require": {
1918
"php": ">=5.3.0"
@@ -24,4 +23,4 @@
2423
}
2524
},
2625
"minimum-stability": "stable"
27-
}
26+
}

Diff for: src/Flynsarmy/DbBladeCompiler/DbBladeCompiler.php

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
<?php namespace Flynsarmy\DbBladeCompiler;
22

3+
use Illuminate\Config\Repository;
4+
use Illuminate\Database\Eloquent\Model;
35
use Illuminate\View\Compilers\BladeCompiler;
46
use Illuminate\View\Compilers\CompilerInterface;
57

68
class DbBladeCompiler extends BladeCompiler implements CompilerInterface
79
{
810

9-
/** @var \Illuminate\Config\Repository */
11+
/** @var Repository */
1012
protected $config;
1113

12-
public function __construct($filesystem, $cache_path, $config, $app)
14+
public function __construct($filesystem, $cache_path, Repository $config)
1315
{
1416
// Get Current Blade Instance
1517
$blade = app('view')->getEngineResolver()->resolve('blade')->getCompiler();
1618

1719
parent::__construct($filesystem, $cache_path);
18-
$this->rawTags = $blade->getRawTags();
19-
$this->contentTags = $blade->getContentTags();
20-
$this->escapedTags = $blade->getEscapedContentTags();
21-
$this->extensions = $blade->getExtensions();
20+
$this->rawTags = $blade->getRawTags();
21+
$this->contentTags = $blade->getContentTags();
22+
$this->escapedTags = $blade->getEscapedContentTags();
23+
$this->extensions = $blade->getExtensions();
2224
$this->customDirectives = $blade->getCustomDirectives();
23-
$this->config = $config;
25+
$this->config = $config;
2426
}
2527

2628
/**
2729
* Compile the view at the given path.
2830
*
29-
* @param Illuminate\Database\Eloquent\Model $path
31+
* @param Model $path
3032
* @return void
3133
*/
3234
public function compile($path)
@@ -48,7 +50,7 @@ public function compile($path)
4850
/**
4951
* Get the path to the compiled version of a view.
5052
*
51-
* @param Illuminate\Database\Eloquent\Model $model
53+
* @param Model $model
5254
* @return string
5355
*/
5456
public function getCompiledPath($model)
@@ -61,7 +63,7 @@ public function getCompiledPath($model)
6163
* e.g db_table_name_id_4
6264
*/
6365
$field = $this->config->get('db-blade-compiler.model_property');
64-
$path = 'db_' . $model->getTable() . '_' . $model->{$field} . '_';
66+
$path = 'db_' . $model->getTable() . '_' . $model->{$field} . '_';
6567
if (is_null($model->primaryKey)) {
6668
$path .= $model->id;
6769
} else if (is_array($model->primaryKey)) {
@@ -78,7 +80,7 @@ public function getCompiledPath($model)
7880
/**
7981
* Determine if the view at the given path is expired.
8082
*
81-
* @param string $path
83+
* @param string $path
8284
* @return bool
8385
*/
8486
public function isExpired($path)
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php namespace Flynsarmy\DbBladeCompiler;
2+
3+
use Illuminate\View\Engines\CompilerEngine;
4+
5+
class DbBladeCompilerEngine extends CompilerEngine
6+
{
7+
/**
8+
* DbBladeCompilerEngine constructor.
9+
*
10+
* @param DbBladeCompiler $bladeCompiler
11+
*/
12+
public function __construct(DbBladeCompiler $bladeCompiler)
13+
{
14+
parent::__construct($bladeCompiler);
15+
}
16+
}

Diff for: src/Flynsarmy/DbBladeCompiler/DbBladeCompilerServiceProvider.php

+49-49
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,64 @@
33
use Illuminate\Support\ServiceProvider;
44
use Illuminate\View\Engines\CompilerEngine;
55

6-
class DbBladeCompilerServiceProvider extends ServiceProvider {
6+
class DbBladeCompilerServiceProvider extends ServiceProvider
7+
{
78

8-
/**
9-
* Indicates if loading of the provider is deferred.
10-
*
11-
* @var bool
12-
*/
13-
protected $defer = false;
9+
/**
10+
* Indicates if loading of the provider is deferred.
11+
*
12+
* @var bool
13+
*/
14+
protected $defer = false;
1415

15-
/**
16-
* Bootstrap the application events.
17-
*
18-
* @return void
19-
*/
20-
public function boot()
21-
{
22-
$config_path = __DIR__ . '/../../../config/db-blade-compiler.php';
23-
$this->publishes([$config_path => config_path('db-blade-compiler.php')], 'config');
16+
/**
17+
* Bootstrap the application events.
18+
*
19+
* @return void
20+
*/
21+
public function boot()
22+
{
23+
$config_path = __DIR__ . '/../../../config/db-blade-compiler.php';
24+
$this->publishes([$config_path => config_path('db-blade-compiler.php')], 'config');
2425

25-
$views_path = __DIR__ . '/../../../config/.gitkeep';
26-
$this->publishes([$views_path => storage_path('app/db-blade-compiler/views/.gitkeep')]);
27-
}
26+
$views_path = __DIR__ . '/../../../config/.gitkeep';
27+
$this->publishes([$views_path => storage_path('app/db-blade-compiler/views/.gitkeep')]);
28+
}
2829

29-
/**
30-
* Register the service provider.
31-
*
32-
* @return void
33-
*/
34-
public function register()
35-
{
36-
$config_path = __DIR__ . '/../../../config/db-blade-compiler.php';
37-
$this->mergeConfigFrom($config_path, 'db-blade-compiler');
30+
/**
31+
* Register the service provider.
32+
*
33+
* @return void
34+
*/
35+
public function register()
36+
{
37+
$config_path = __DIR__ . '/../../../config/db-blade-compiler.php';
38+
$this->mergeConfigFrom($config_path, 'db-blade-compiler');
3839

39-
$this->app['dbview'] = $this->app->share(function($app)
40-
{
41-
$cache_path = storage_path('app/db-blade-compiler/views');
40+
$this->app['dbview'] = $this->app->share(function ($app) {
41+
return $app->make(DbView::class);
42+
});
4243

43-
$db_view = new DbView($app['config']);
44-
$compiler = new DbBladeCompiler($app['files'], $cache_path, $app['config'], $app);
45-
$db_view->setEngine(new CompilerEngine($compiler));
44+
$this->app->bind(DbBladeCompiler::class, function($app) {
45+
$cache_path = storage_path('app/db-blade-compiler/views');
4646

47-
return $db_view;
47+
return new DbBladeCompiler($app['files'], $cache_path, $app['config']);
4848
});
49-
$this->app->booting(function()
50-
{
51-
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
52-
$loader->alias('DbView', 'Flynsarmy\DbBladeCompiler\Facades\DbView');
49+
50+
$this->app->booting(function () {
51+
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
52+
$loader->alias('DbView', DbView::class);
5353
});
54-
}
54+
}
5555

56-
/**
57-
* Get the services provided by the provider.
58-
*
59-
* @return array
60-
*/
61-
public function provides()
62-
{
63-
return array();
64-
}
56+
/**
57+
* Get the services provided by the provider.
58+
*
59+
* @return array
60+
*/
61+
public function provides()
62+
{
63+
return array();
64+
}
6565

6666
}

0 commit comments

Comments
 (0)