Skip to content

Commit ab9f287

Browse files
committedJun 7, 2015
Merge pull request #10 from me2bits/master
Support Laravel 5
2 parents 215ea69 + 9019916 commit ab9f287

13 files changed

+88
-25
lines changed
 

‎.gitignore

100644100755
File mode changed.

‎.travis.yml

100644100755
File mode changed.

‎composer.json

100644100755
+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
"name": "Flyn San",
99
"email": "flynsarmy@gmail.com"
1010
}
11+
,
12+
{
13+
"name": "Ebrahim Radi",
14+
"email": "ebrahim.radi@gmail.com",
15+
"homepage": "http://me2bits.com"
16+
}
1117
],
1218
"require": {
1319
"php": ">=5.3.0"

‎public/.gitkeep ‎config/.gitkeep

File renamed without changes.

‎src/config/config.php ‎config/db-blade-compiler.php

+2
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414
* with DbView::field()
1515
*/
1616
'model_default_field' => 'content',
17+
18+
'cache' => false
1719
);

‎phpunit.xml

100644100755
File mode changed.

‎readme.md

100644100755
+17-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
This package generates and returns a compiled view from a blade-syntax field in your Eloquent model.
77

88

9-
### Installation
9+
### Installation (Laravel v < 5)
1010

1111
Require this package in your composer.json and run composer update (or run `composer require flynsarmy/db-blade-compiler:1.*` directly):
1212

@@ -25,6 +25,21 @@ You can also optionally publish the config-file
2525
php artisan config:publish flynsarmy/db-blade-compiler
2626

2727

28+
### Installation (Laravel 5.x)
29+
30+
Require this package in your composer.json and run composer update (or run `composer require flynsarmy/db-blade-compiler:2.*` directly):
31+
32+
"flynsarmy/db-blade-compiler": "2.*"
33+
34+
After updating composer, add the ServiceProvider to the providers array in app/config/app.php
35+
36+
'Flynsarmy\DbBladeCompiler\DbBladeCompilerServiceProvider',
37+
38+
You have to also publish the config-file
39+
40+
php artisan vendor:publish
41+
42+
2843
### Usage
2944

3045
This package offers a `DbView` facade with the same syntax as `View` but accepts a Model instance instead of path to view.
@@ -38,6 +53,7 @@ Because you're passing a model to `DbView::make()`, db-blade-compiler needs to k
3853
return DbView::make($template)->field('excerpt')->with(['foo' => 'Bar'])->render();
3954

4055
You may set the default column used in the package config.
56+
You can enable using cache in compiling view from a blade-syntax field in your Eloquent model operation by enabling cache config in package config. By default this option is disabled.
4157

4258

4359
### License

‎src/Flynsarmy/DbBladeCompiler/Compilers/DbBladeCompiler.php ‎src/Flynsarmy/DbBladeCompiler/DbBladeCompiler.php

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
<?php namespace Flynsarmy\DbBladeCompiler\Compilers;
1+
<?php namespace Flynsarmy\DbBladeCompiler;
22

3-
use Config;
43
use Illuminate\View\Compilers\BladeCompiler;
54
use Illuminate\View\Compilers\CompilerInterface;
65

7-
class DbBladeCompiler extends BladeCompiler implements CompilerInterface {
6+
class DbBladeCompiler extends BladeCompiler implements CompilerInterface
7+
{
8+
9+
/** @var \Illuminate\Config\Repository */
10+
protected $config;
11+
12+
public function __construct($filesystem, $cache_path, $config)
13+
{
14+
parent::__construct($filesystem, $cache_path);
15+
$this->config = $config;
16+
}
817

918
/**
1019
* Compile the view at the given path.
@@ -15,7 +24,7 @@ class DbBladeCompiler extends BladeCompiler implements CompilerInterface {
1524
public function compile($path)
1625
{
1726
// Defaults to '__db_blade_compiler_content_field' property
18-
$property = Config::get('db-blade-compiler::model_property');
27+
$property = $this->config->get('db-blade-compiler.model_property');
1928
// Defaults to 'contents' column
2029
$column = $path->{$property};
2130
// Grab the column contents
@@ -44,7 +53,7 @@ public function getCompiledPath($model)
4453
*
4554
* e.g db_table_name_id_4
4655
*/
47-
$field = Config::get('db-blade-compiler::model_property');
56+
$field = $this->config->get('db-blade-compiler.model_property');
4857
$path = 'db_' . $model->getTable() . '_' . $model->{$field} . '_';
4958
if ( is_null($model->primaryKey) )
5059
$path .= $model->id;
@@ -66,8 +75,10 @@ public function getCompiledPath($model)
6675
*/
6776
public function isExpired($path)
6877
{
69-
if(Config::get('app.debug')) return true;
70-
78+
if(!$this->config->get('cache'))
79+
{
80+
return true;
81+
}
7182
$compiled = $this->getCompiledPath($path);
7283

7384
// If the compiled file doesn't exist we will indicate that the view is expired

‎src/Flynsarmy/DbBladeCompiler/DbBladeCompilerServiceProvider.php

100644100755
+24-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace Flynsarmy\DbBladeCompiler;
22

33
use Illuminate\Support\ServiceProvider;
4+
use Illuminate\View\Engines\CompilerEngine;
45

56
class DbBladeCompilerServiceProvider extends ServiceProvider {
67

@@ -18,7 +19,11 @@ class DbBladeCompilerServiceProvider extends ServiceProvider {
1819
*/
1920
public function boot()
2021
{
21-
$this->package('flynsarmy/db-blade-compiler');
22+
$config_path = __DIR__ . '/../../../config/db-blade-compiler.php';
23+
$this->publishes([$config_path => config_path('db-blade-compiler.php')], 'config');
24+
25+
$views_path = __DIR__ . '/../../../config/.gitkeep';
26+
$this->publishes([$views_path => storage_path('app/db-blade-compiler/views/.gitkeep')]);
2227
}
2328

2429
/**
@@ -28,7 +33,24 @@ public function boot()
2833
*/
2934
public function register()
3035
{
31-
$this->app->bind('dbview', 'Flynsarmy\DbBladeCompiler\DbView');
36+
$config_path = __DIR__ . '/../../../config/db-blade-compiler.php';
37+
$this->mergeConfigFrom($config_path, 'db-blade-compile');
38+
39+
$this->app['dbview'] = $this->app->share(function($app)
40+
{
41+
$cache_path = storage_path('app/db-blade-compiler/views');
42+
43+
$db_view = new DbView($app['config']);
44+
$compiler = new DbBladeCompiler($app['files'], $cache_path, $app['config']);
45+
$db_view->setEngine(new CompilerEngine($compiler));
46+
47+
return $db_view;
48+
});
49+
$this->app->booting(function()
50+
{
51+
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
52+
$loader->alias('DbView', 'Flynsarmy\DbBladeCompiler\Facades\DbView');
53+
});
3254
}
3355

3456
/**

‎src/Flynsarmy/DbBladeCompiler/DbView.php

100644100755
+16-14
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
<?php namespace Flynsarmy\DbBladeCompiler;
22

3-
use App, View, Closure, Config, ArrayAccess;
4-
use Illuminate\Support\MessageBag;
5-
use Illuminate\View\Engines\EngineInterface;
6-
use Illuminate\Support\Contracts\MessageProviderInterface;
7-
use Illuminate\Support\Contracts\ArrayableInterface as Arrayable;
8-
use Illuminate\Support\Contracts\RenderableInterface as Renderable;
9-
use Flynsarmy\DbBladeCompiler\Compilers\DbBladeCompiler;
10-
use Illuminate\View\Engines\CompilerEngine;
3+
use View, Closure, ArrayAccess;
4+
use Illuminate\Contracts\Support\Arrayable;
5+
use Illuminate\Contracts\Support\Renderable;
116

127
class DbView extends \Illuminate\View\View implements ArrayAccess, Renderable {
138

149
protected $content_field = null;
1510

16-
public function __construct()
11+
/** @var \Illuminate\Config\Repository */
12+
protected $config;
13+
14+
public function __construct($config)
1715
{
18-
$cache = App::make('path.storage').'/views';
19-
$compiler = new DbBladeCompiler(App::make('files'), $cache);
20-
$this->engine = new CompilerEngine($compiler);
16+
$this->config = $config;
2117
}
2218

19+
public function setEngine($compiler)
20+
{
21+
$this->engine = $compiler;
22+
return $this;
23+
}
24+
2325
/**
2426
* Get a evaluated view contents for the given view.
2527
*
@@ -34,7 +36,7 @@ public function make($view, $data = array(), $mergeData = array(), $content_fiel
3436
$this->path = $view;
3537
$this->data = array_merge($mergeData, $this->parseData($data));
3638
if ( !is_null($content_field) ) $this->content_field = $content_field;
37-
else $this->content_field = Config::get('db-blade-compiler::model_default_field');
39+
else $this->content_field = $this->config->get('db-blade-compiler.model_default_field');
3840

3941
return $this;
4042
}
@@ -90,7 +92,7 @@ protected function renderContents()
9092

9193
protected function getContents()
9294
{
93-
$field = Config::get('db-blade-compiler::model_property');
95+
$field = $this->config->get('db-blade-compiler.model_property');
9496
$this->path->{$field} = $this->content_field;
9597

9698
return parent::getContents();

‎src/Flynsarmy/DbBladeCompiler/Facades/DbView.php

100644100755
+5-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ class DbView extends Facade {
99
*
1010
* @return string
1111
*/
12-
protected static function getFacadeAccessor() { return 'dbview'; }
12+
protected static function getFacadeAccessor()
13+
{
14+
return 'dbview';
15+
}
16+
1317
}

‎src/config/.gitkeep

Whitespace-only changes.

‎tests/.gitkeep

100644100755
File mode changed.

0 commit comments

Comments
 (0)
Please sign in to comment.