Skip to content

Commit f556e3e

Browse files
committed
Add ability to extend handlers via config
Resolve #50
1 parent dc0811a commit f556e3e

15 files changed

+437
-75
lines changed

.gitattributes

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
tests
2+
travis.yml
3+
.php_cs
4+
.phplint.yml
5+
phpunit.xml
6+
CODE_OF_CONDUCT.md

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
composer.phar
33
/vendor/
44
composer.lock
5+
/build
56

.php_cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
$finder = PhpCsFixer\Finder::create()
3+
->in(__DIR__ . '/src')
4+
->in(__DIR__ . '/config')
5+
->in(__DIR__ . '/tests')
6+
->name('*.php')
7+
->ignoreDotFiles(true)
8+
->ignoreVCS(true);
9+
10+
return PhpCsFixer\Config::create()
11+
->setRules(array(
12+
'@PHP56Migration' => true,
13+
'@Symfony' => true,
14+
'align_multiline_comment' => true,
15+
'array_indentation' => true,
16+
'array_syntax' => ['syntax' => 'short'],
17+
))
18+
->setFinder($finder);

.phplint.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
path: ./
2+
jobs: 5
3+
cache: build/phplint.cache
4+
extensions:
5+
- php
6+
exclude:
7+
- vendor

CONTRIBUTING.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ upload and progress.
1010
## Pull Requests
1111

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

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

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

2222
- **One pull request per feature.** If you want to do more than one thing, send multiple pull requests.
23+
24+
### Before pull-request do:
25+
26+
1. Rebase your changes on master branch
27+
2. Lint project `composer run lint`
28+
3. Run tests `composer run test`
29+
4. (recommended) Write tests
30+
5. (optinal) Rebase your commits to fewer commits
2331

2432
**Thank you!**
2533

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"scripts": {
1212
"lint:fix": "./vendor/bin/php-cs-fixer fix --config=.php_cs --using-cache false",
1313
"lint:check": "./vendor/bin/phplint",
14-
"lint": "composer run-script lint-fix && composer run-script lint-check"
14+
"lint": "composer run-script lint:fix && composer run-script lint:check",
15+
"test": "./vendor/bin/phpunit"
1516
},
1617
"require": {
1718
"illuminate/http": "5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.*",
@@ -22,7 +23,7 @@
2223
"require-dev": {
2324
"laravel/laravel": "5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.*",
2425
"phpunit/phpunit": "5.7 || 6.0 || 7.0",
25-
"mockery/mockery": "^0.9.9",
26+
"mockery/mockery": "^1.1.0",
2627
"friendsofphp/php-cs-fixer": "^2.12",
2728
"overtrue/phplint": "^1.1"
2829
},

config/chunk-upload.php

+8
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,12 @@
3030
],
3131
],
3232
],
33+
'handlers' => [
34+
// A list of handlers/providers that will be appended to existing list of handlers
35+
'custom' => [],
36+
// Overrides the list of handlers - use only what you really want
37+
'override' => [
38+
// \Pion\Laravel\ChunkUpload\Handler\DropZoneUploadHandler::class
39+
],
40+
],
3341
];

phpcs.xml

-63
This file was deleted.

src/Config/AbstractConfig.php

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ public static function config()
1616
return app(AbstractConfig::class);
1717
}
1818

19+
/**
20+
* Returns a list custom handlers (custom, override).
21+
*
22+
* @return array
23+
*/
24+
abstract public function handlers();
25+
1926
/**
2027
* Returns the disk name to use for the chunk storage.
2128
*

src/Config/FileConfig.php

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ class FileConfig extends AbstractConfig
1414
*/
1515
const FILE_NAME = 'chunk-upload';
1616

17+
/**
18+
* Returns a list custom handlers (custom, override).
19+
*
20+
* @return array
21+
*/
22+
public function handlers()
23+
{
24+
return $this->get('hanlders', []);
25+
}
26+
1727
/**
1828
* Returns the disk name to use for the chunk storage.
1929
*

src/Handler/HandlerFactory.php

+20
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ public static function register($handlerClass)
6363
static::$handlers[] = $handlerClass;
6464
}
6565

66+
/**
67+
* Overrides the handler list.
68+
*
69+
* @param array $handlers
70+
*/
71+
public static function setHandlers($handlers)
72+
{
73+
static::$handlers = $handlers;
74+
}
75+
76+
/**
77+
* Returns the handler list.
78+
*
79+
* @return array
80+
*/
81+
public static function getHandlers()
82+
{
83+
return static::$handlers;
84+
}
85+
6686
/**
6787
* Sets the default fallback handler when the detection fails.
6888
*

src/Providers/ChunkUploadServiceProvider.php

+48-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
use Illuminate\Console\Scheduling\Schedule;
66
use Illuminate\Http\Request;
77
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Facades\Storage;
89
use Illuminate\Support\ServiceProvider;
910
use Pion\Laravel\ChunkUpload\Commands\ClearChunksCommand;
1011
use Pion\Laravel\ChunkUpload\Config\AbstractConfig;
1112
use Pion\Laravel\ChunkUpload\Config\FileConfig;
1213
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory;
1314
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver;
1415
use Pion\Laravel\ChunkUpload\Storage\ChunkStorage;
15-
use Storage;
1616

1717
class ChunkUploadServiceProvider extends ServiceProvider
1818
{
@@ -22,7 +22,8 @@ class ChunkUploadServiceProvider extends ServiceProvider
2222
public function boot()
2323
{
2424
// Get the schedule config
25-
$scheduleConfig = AbstractConfig::config()->scheduleConfig();
25+
$config = $this->app->make(AbstractConfig::class);
26+
$scheduleConfig = $config->scheduleConfig();
2627

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

3536
// Register the clear chunks with custom schedule
36-
$schedule->command('uploads:clear')->cron(Arr::get($scheduleConfig, 'cron', '* * * * *'));
37+
$schedule->command('uploads:clear')
38+
->cron(Arr::get($scheduleConfig, 'cron', '* * * * *'));
3739
});
3840
}
41+
42+
$this->registerHandlers($config->handlers());
3943
}
4044

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

6670
// Build the chunk storage
67-
return new ChunkStorage(Storage::disk($config->chunksDiskName()), $config);
71+
return new ChunkStorage($this->disk($config->chunksDiskName()), $config);
6872
});
6973

7074
/*
@@ -83,11 +87,25 @@ public function register()
8387
}
8488

8589
/**
86-
* Publishes and mergers the config. Uses the FileConfig.
90+
* Returns disk name.
91+
*
92+
* @param string $diskName
93+
*
94+
* @return \Illuminate\Contracts\Filesystem\Filesystem
95+
*/
96+
protected function disk($diskName)
97+
{
98+
return Storage::disk($diskName);
99+
}
100+
101+
/**
102+
* Publishes and mergers the config. Uses the FileConfig. Registers custom handlers.
87103
*
88104
* @see FileConfig
89105
* @see ServiceProvider::publishes
90106
* @see ServiceProvider::mergeConfigFrom
107+
*
108+
* @return $this
91109
*/
92110
protected function registerConfig()
93111
{
@@ -106,5 +124,30 @@ protected function registerConfig()
106124
$configPath,
107125
$configIndex
108126
);
127+
128+
return $this;
129+
}
130+
131+
/**
132+
* Registers handlers from config.
133+
*
134+
* @param array $handlersConfig
135+
*
136+
* @return $this
137+
*/
138+
protected function registerHandlers(array $handlersConfig)
139+
{
140+
$overrideHandlers = Arr::get($handlersConfig, 'override', []);
141+
if (count($overrideHandlers) > 0) {
142+
HandlerFactory::setHandlers($overrideHandlers);
143+
144+
return $this;
145+
}
146+
147+
foreach (Arr::get($handlersConfig, 'custom', []) as $handler) {
148+
HandlerFactory::register($handler);
149+
}
150+
151+
return $this;
109152
}
110153
}

src/Storage/ChunkStorage.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Pion\Laravel\ChunkUpload\Storage;
44

5-
use Illuminate\Filesystem\FilesystemAdapter;
5+
use Illuminate\Contracts\Filesystem\Filesystem as FilesystemContract;
66
use Illuminate\Support\Collection;
77
use League\Flysystem\Adapter\Local;
88
use League\Flysystem\FilesystemInterface;
@@ -51,10 +51,10 @@ public static function storage()
5151
/**
5252
* ChunkStorage constructor.
5353
*
54-
* @param FilesystemAdapter $disk the desired disk for chunk storage
55-
* @param AbstractConfig $config
54+
* @param FilesystemContract $disk the desired disk for chunk storage
55+
* @param AbstractConfig $config
5656
*/
57-
public function __construct(FilesystemAdapter $disk, $config)
57+
public function __construct(FilesystemContract $disk, $config)
5858
{
5959
// save the config
6060
$this->config = $config;

tests/FileSystemDriverMock.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
class FileSystemDriverMock
6+
{
7+
public function getAdapter()
8+
{
9+
return null;
10+
}
11+
}

0 commit comments

Comments
 (0)