Skip to content

Commit

Permalink
Add ability to extend handlers via config
Browse files Browse the repository at this point in the history
Resolve #50
  • Loading branch information
pionl committed Sep 11, 2018
1 parent dc0811a commit f556e3e
Show file tree
Hide file tree
Showing 15 changed files with 437 additions and 75 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests
travis.yml
.php_cs
.phplint.yml
phpunit.xml
CODE_OF_CONDUCT.md
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
composer.phar
/vendor/
composer.lock
/build

18 changes: 18 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
$finder = PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->in(__DIR__ . '/config')
->in(__DIR__ . '/tests')
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true);

return PhpCsFixer\Config::create()
->setRules(array(
'@PHP56Migration' => true,
'@Symfony' => true,
'align_multiline_comment' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
))
->setFinder($finder);
7 changes: 7 additions & 0 deletions .phplint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
path: ./
jobs: 5
cache: build/phplint.cache
extensions:
- php
exclude:
- vendor
10 changes: 9 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ upload and progress.
## Pull Requests

- **Use the [PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md).**
The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).
The easiest way to apply the conventions is to use `composer run lint:fix`.

- **Consider our release cycle.** We try to follow [SemVer v2.0.0](http://semver.org/).

Expand All @@ -20,6 +20,14 @@ upload and progress.
- **Create feature branches.** Don't ask us to pull from your master branch.

- **One pull request per feature.** If you want to do more than one thing, send multiple pull requests.

### Before pull-request do:

1. Rebase your changes on master branch
2. Lint project `composer run lint`
3. Run tests `composer run test`
4. (recommended) Write tests
5. (optinal) Rebase your commits to fewer commits

**Thank you!**

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"scripts": {
"lint:fix": "./vendor/bin/php-cs-fixer fix --config=.php_cs --using-cache false",
"lint:check": "./vendor/bin/phplint",
"lint": "composer run-script lint-fix && composer run-script lint-check"
"lint": "composer run-script lint:fix && composer run-script lint:check",
"test": "./vendor/bin/phpunit"
},
"require": {
"illuminate/http": "5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.*",
Expand All @@ -22,7 +23,7 @@
"require-dev": {
"laravel/laravel": "5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.*",
"phpunit/phpunit": "5.7 || 6.0 || 7.0",
"mockery/mockery": "^0.9.9",
"mockery/mockery": "^1.1.0",
"friendsofphp/php-cs-fixer": "^2.12",
"overtrue/phplint": "^1.1"
},
Expand Down
8 changes: 8 additions & 0 deletions config/chunk-upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@
],
],
],
'handlers' => [
// A list of handlers/providers that will be appended to existing list of handlers
'custom' => [],
// Overrides the list of handlers - use only what you really want
'override' => [
// \Pion\Laravel\ChunkUpload\Handler\DropZoneUploadHandler::class
],
],
];
63 changes: 0 additions & 63 deletions phpcs.xml

This file was deleted.

7 changes: 7 additions & 0 deletions src/Config/AbstractConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public static function config()
return app(AbstractConfig::class);
}

/**
* Returns a list custom handlers (custom, override).
*
* @return array
*/
abstract public function handlers();

/**
* Returns the disk name to use for the chunk storage.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Config/FileConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ class FileConfig extends AbstractConfig
*/
const FILE_NAME = 'chunk-upload';

/**
* Returns a list custom handlers (custom, override).
*
* @return array
*/
public function handlers()
{
return $this->get('hanlders', []);
}

/**
* Returns the disk name to use for the chunk storage.
*
Expand Down
20 changes: 20 additions & 0 deletions src/Handler/HandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ public static function register($handlerClass)
static::$handlers[] = $handlerClass;
}

/**
* Overrides the handler list.
*
* @param array $handlers
*/
public static function setHandlers($handlers)
{
static::$handlers = $handlers;
}

/**
* Returns the handler list.
*
* @return array
*/
public static function getHandlers()
{
return static::$handlers;
}

/**
* Sets the default fallback handler when the detection fails.
*
Expand Down
53 changes: 48 additions & 5 deletions src/Providers/ChunkUploadServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use Pion\Laravel\ChunkUpload\Commands\ClearChunksCommand;
use Pion\Laravel\ChunkUpload\Config\AbstractConfig;
use Pion\Laravel\ChunkUpload\Config\FileConfig;
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory;
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver;
use Pion\Laravel\ChunkUpload\Storage\ChunkStorage;
use Storage;

class ChunkUploadServiceProvider extends ServiceProvider
{
Expand All @@ -22,7 +22,8 @@ class ChunkUploadServiceProvider extends ServiceProvider
public function boot()
{
// Get the schedule config
$scheduleConfig = AbstractConfig::config()->scheduleConfig();
$config = $this->app->make(AbstractConfig::class);
$scheduleConfig = $config->scheduleConfig();

// Run only if schedule is enabled
if (true === Arr::get($scheduleConfig, 'enabled', false)) {
Expand All @@ -33,9 +34,12 @@ public function boot()
$schedule = $this->app->make(Schedule::class);

// Register the clear chunks with custom schedule
$schedule->command('uploads:clear')->cron(Arr::get($scheduleConfig, 'cron', '* * * * *'));
$schedule->command('uploads:clear')
->cron(Arr::get($scheduleConfig, 'cron', '* * * * *'));
});
}

$this->registerHandlers($config->handlers());
}

/**
Expand Down Expand Up @@ -64,7 +68,7 @@ public function register()
$config = $app->make(AbstractConfig::class);

// Build the chunk storage
return new ChunkStorage(Storage::disk($config->chunksDiskName()), $config);
return new ChunkStorage($this->disk($config->chunksDiskName()), $config);
});

/*
Expand All @@ -83,11 +87,25 @@ public function register()
}

/**
* Publishes and mergers the config. Uses the FileConfig.
* Returns disk name.
*
* @param string $diskName
*
* @return \Illuminate\Contracts\Filesystem\Filesystem
*/
protected function disk($diskName)
{
return Storage::disk($diskName);
}

/**
* Publishes and mergers the config. Uses the FileConfig. Registers custom handlers.
*
* @see FileConfig
* @see ServiceProvider::publishes
* @see ServiceProvider::mergeConfigFrom
*
* @return $this
*/
protected function registerConfig()
{
Expand All @@ -106,5 +124,30 @@ protected function registerConfig()
$configPath,
$configIndex
);

return $this;
}

/**
* Registers handlers from config.
*
* @param array $handlersConfig
*
* @return $this
*/
protected function registerHandlers(array $handlersConfig)
{
$overrideHandlers = Arr::get($handlersConfig, 'override', []);
if (count($overrideHandlers) > 0) {
HandlerFactory::setHandlers($overrideHandlers);

return $this;
}

foreach (Arr::get($handlersConfig, 'custom', []) as $handler) {
HandlerFactory::register($handler);
}

return $this;
}
}
8 changes: 4 additions & 4 deletions src/Storage/ChunkStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Pion\Laravel\ChunkUpload\Storage;

use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Contracts\Filesystem\Filesystem as FilesystemContract;
use Illuminate\Support\Collection;
use League\Flysystem\Adapter\Local;
use League\Flysystem\FilesystemInterface;
Expand Down Expand Up @@ -51,10 +51,10 @@ public static function storage()
/**
* ChunkStorage constructor.
*
* @param FilesystemAdapter $disk the desired disk for chunk storage
* @param AbstractConfig $config
* @param FilesystemContract $disk the desired disk for chunk storage
* @param AbstractConfig $config
*/
public function __construct(FilesystemAdapter $disk, $config)
public function __construct(FilesystemContract $disk, $config)
{
// save the config
$this->config = $config;
Expand Down
11 changes: 11 additions & 0 deletions tests/FileSystemDriverMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Tests;

class FileSystemDriverMock
{
public function getAdapter()
{
return null;
}
}
Loading

0 comments on commit f556e3e

Please sign in to comment.