Skip to content
This repository has been archived by the owner on May 11, 2019. It is now read-only.

Use IoC for config resolution, so it can be overridden. #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,60 @@ CKEditor instance as follows:
you are using a custom route you will have to change `/laravel-filemanager?type=Images` to correspond
to whatever route you have chosen. Be sure to include the `?type=Images` parameter.

## Advanced Usage

If you want to dynamically modify the config, to say: have an images directory per user, or alter the accepted files for different use cases, you can override the package in a service provider as follows:

```php
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;

class FileManagerServiceProvider extends ServiceProvider {
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('laravel-filemanager.config', function ()
{
if (Auth::check()) {
$user = Auth::user();

$config = Config::get('lfm');

$config = array_merge($config, [
'images_dir' => $config['images_dir']] . $user->id . '/',
'images_url' => $config['images_url']] . $user->id . '/',

'files_dir' => $config['files_dir']] . $user->id . '/',
'files_url' => $config['files_url']] . $user->id . '/',
]);

$images_path = base_path($config['images_dir']);

if (! is_dir($images_path)) {
mkdir($images_path, 0777, true);
}

$files_path = base_path($config['files_dir']);

if (! is_dir($files_path)) {
mkdir($files_path, 0777, true);
}

return $config;
}
});
}
}
```

*NB*: Don't forget to load your new service provider in `config/app.php`.

The config for the package is resolved via Laravel's `IoC` container, with key `laravel-filemanager.config`. It's expected to return an `array` or implementation of `ArrayAccess`, with the new modified config.

## Security

Expand All @@ -106,7 +160,7 @@ simply wrap the routes in a group, perhaps like this:
Route::group(array('before' => 'auth'), function ()
{
Route::get('/laravel-filemanager', '\Tsawler\Laravelfilemanager\controllers\LfmController@show');
Route::post('/laravel-filemanager/upload', '\Tsawler\Laravelfilemanager\controllers\LfmController@upload');
Route::post('/laravel-filemanager/upload', '\Tsawler\Laravelfilemanager\controllers\UploadController@upload');
// list all lfm routes here...
});

Expand Down
5 changes: 5 additions & 0 deletions src/LaravelFilemanagerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function register()
{
return true;
});

$this->app['laravel-filemanager.config'] = $this->app->share(function ()
{
return Config::get('lfm');
});
}

}
13 changes: 12 additions & 1 deletion src/controllers/Controller.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
<?php namespace Tsawler\Laravelfilemanager\controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\App;

abstract class Controller extends BaseController
{
use DispatchesJobs, ValidatesRequests;

/**
* Get the config for a given key, allowing users to override
* the config generation via the IoC container.
*/
protected function getConfig($key)
{
$config = App::make('laravel-filemanager.config');
return $config[$key];
}
}
4 changes: 2 additions & 2 deletions src/controllers/CropController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getCrop()
$image = Input::get('img');

return View::make('laravel-filemanager::crop')
->with('img', Config::get('lfm.images_url') . $dir . "/" . $image)
->with('img', $this->getConfig('images_url') . $dir . "/" . $image)
->with('dir', $dir)
->with('image', $image);
}
Expand All @@ -49,7 +49,7 @@ public function getCropimage()
// make new thumbnail
$thumb_img = Image::make(public_path() . $img);
$thumb_img->fit(200, 200)
->save(base_path() . "/" . Config::get('lfm.images_dir') . $dir . "/thumbs/" . basename($img));
->save(base_path() . "/" . $this->getConfig('images_dir') . $dir . "/thumbs/" . basename($img));
}

}
4 changes: 2 additions & 2 deletions src/controllers/DeleteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class DeleteController extends Controller {
function __construct()
{
if (Session::get('lfm_type') == "Images")
$this->file_location = Config::get('lfm.images_dir');
$this->file_location = $this->getConfig('images_dir');
else
$this->file_location = Config::get('lfm.files_dir');
$this->file_location = $this->getConfig('files_dir');
}


Expand Down
4 changes: 2 additions & 2 deletions src/controllers/DownloadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class DownloadController extends Controller {
function __construct()
{
if (Session::get('lfm_type') == "Images")
$this->file_location = Config::get('lfm.images_dir');
$this->file_location = $this->getConfig('images_dir');
else
$this->file_location = Config::get('lfm.files_dir');
$this->file_location = $this->getConfig('files_dir');
}


Expand Down
4 changes: 2 additions & 2 deletions src/controllers/FolderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class FolderController extends Controller {
function __construct()
{
if (Session::get('lfm_type') == "Images")
$this->file_location = Config::get('lfm.images_dir');
$this->file_location = $this->getConfig('images_dir');
else
$this->file_location = Config::get('lfm.files_dir');
$this->file_location = $this->getConfig('files_dir');
}


Expand Down
12 changes: 6 additions & 6 deletions src/controllers/ItemsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class ItemsController extends Controller {
function __construct()
{
if (Session::get('lfm_type') == "Images")
$this->file_location = Config::get('lfm.images_dir');
$this->file_location = $this->getConfig('images_dir');
else
$this->file_location = Config::get('lfm.files_dir');
$this->file_location = $this->getConfig('files_dir');
}


Expand Down Expand Up @@ -60,8 +60,8 @@ public function getFiles()
}

$file_info = [];
$icon_array = Config::get('lfm.file_icon_array');
$type_array = Config::get('lfm.file_type_array');
$icon_array = $this->getConfig('file_icon_array');
$type_array = $this->getConfig('file_type_array');

foreach ($files as $file)
{
Expand Down Expand Up @@ -163,9 +163,9 @@ public function getImages()
}

if ((Session::has('lfm_type')) && (Session::get('lfm_type') == "Images"))
$dir_location = Config::get('lfm.images_url');
$dir_location = $this->getConfig('images_url');
else
$dir_location = Config::get('lfm.files_url');
$dir_location = $this->getConfig('files_url');

if (Input::get('show_list') == 1)
{
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/LfmController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public function __construct()
{
if ((Session::has('lfm_type')) && (Session::get('lfm_type') == 'Files'))
{
$this->file_location = Config::get('lfm.files_dir');
$this->file_location = $this->getConfig('files_dir');
} else
{
$this->file_location = Config::get('lfm.images_dir');
$this->file_location = $this->getConfig('images_dir');
}
}

Expand All @@ -44,11 +44,11 @@ public function show()
if ((Input::has('type')) && (Input::get('type') == "Files"))
{
Session::put('lfm_type', 'Files');
$this->file_location = Config::get('lfm.files_dir');
$this->file_location = $this->getConfig('files_dir');
} else
{
Session::put('lfm_type', 'Images');
$this->file_location = Config::get('lfm.images_dir');
$this->file_location = $this->getConfig('images_dir');
}

if (Input::has('base'))
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/RenameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class RenameController extends Controller {
function __construct()
{
if (Session::get('lfm_type') == "Images")
$this->file_location = Config::get('lfm.images_dir');
$this->file_location = $this->getConfig('images_dir');
else
$this->file_location = Config::get('lfm.files_dir');
$this->file_location = $this->getConfig('files_dir');
}


Expand Down
6 changes: 3 additions & 3 deletions src/controllers/ResizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function getResize()
$image = Input::get('img');
$dir = Input::get('dir');

$original_width = Image::make(base_path() . "/" . Config::get('lfm.images_dir') . $dir . "/" . $image)->width();
$original_height = Image::make(base_path() . "/" . Config::get('lfm.images_dir') . $dir . "/" . $image)->height();
$original_width = Image::make(base_path() . "/" . $this->getConfig('images_dir') . $dir . "/" . $image)->width();
$original_height = Image::make(base_path() . "/" . $this->getConfig('images_dir') . $dir . "/" . $image)->height();

$scaled = false;

Expand All @@ -49,7 +49,7 @@ public function getResize()
}

return View::make('laravel-filemanager::resize')
->with('img', Config::get('lfm.images_url') . $dir . "/" . $image)
->with('img', $this->getConfig('images_url') . $dir . "/" . $image)
->with('dir', $dir)
->with('image', $image)
->with('height', number_format($height, 0))
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ class UploadController extends Controller {
*/
function __construct()
{
$this->allowed_types = Config::get('lfm.allowed_file_types');
$this->allowed_types = $this->getConfig('allowed_file_types');

if (Session::get('lfm_type') == "Images")
$this->file_location = Config::get('lfm.images_dir');
$this->file_location = $this->getConfig('images_dir');
else
$this->file_location = Config::get('lfm.files_dir');
$this->file_location = $this->getConfig('files_dir');
}


Expand Down
12 changes: 6 additions & 6 deletions src/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,16 +353,16 @@ function getUrlParam(paramName) {

@if ((Session::has('lfm_type')) && (Session::get('lfm_type') == "Images"))
if (path != '/') {
window.opener.CKEDITOR.tools.callFunction(funcNum, '{{ \Config::get('lfm.images_url') }}' + path + "/" + file);
window.opener.CKEDITOR.tools.callFunction(funcNum, '{{ array_get(\App::make('laravel-filemanager.config'), 'images_url') }}' + path + "/" + file);
} else {
window.opener.CKEDITOR.tools.callFunction(funcNum, '{{ \Config::get('lfm.images_url') }}' + file);
window.opener.CKEDITOR.tools.callFunction(funcNum, '{{ array_get(\App::make('laravel-filemanager.config'), 'images_url') }}' + file);
}
@else
if (path != '/') {
window.opener.CKEDITOR.tools.callFunction(funcNum, '{{ \Config::get('lfm.files_url') }}' + path + "/" + file);
} else {
window.opener.CKEDITOR.tools.callFunction(funcNum, '{{ \Config::get('lfm.files_url') }}' + file);
}
window.opener.CKEDITOR.tools.callFunction(funcNum, '{{ array_get(\App::make('laravel-filemanager.config'), 'files_url') }}' + path + "/" + file);
} else {
window.opener.CKEDITOR.tools.callFunction(funcNum, '{{ array_get(\App::make('laravel-filemanager.config'), 'files_url') }}' + file);
}
@endif
window.close();
}
Expand Down