Skip to content

Commit c785759

Browse files
author
Recca Tsai
committed
publish config
1 parent f2b239b commit c785759

File tree

5 files changed

+101
-40
lines changed

5 files changed

+101
-40
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Instead, you may of course manually update your require block and run `composer
3232
}
3333
```
3434

35-
## standalone
35+
## Standalone
3636

3737
```php
3838

@@ -68,6 +68,12 @@ Include the service provider within `config/app.php`. The service povider is nee
6868
];
6969
```
7070

71+
publish
72+
73+
```php
74+
artisan vendor:publish --provider="Recca0120\Upload\UploadServiceProvider"
75+
```
76+
7177
## How to use
7278

7379
Controller

src/UploadServiceProvider.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@
66

77
class UploadServiceProvider extends ServiceProvider
88
{
9+
10+
/**
11+
* Bootstrap any application services.
12+
*/
13+
public function boot()
14+
{
15+
if ($this->app->runningInConsole() === true) {
16+
$this->handlePublishes();
17+
18+
return;
19+
}
20+
}
21+
922
/**
1023
* Register the service provider.
1124
*/
@@ -19,4 +32,14 @@ public function register()
1932
});
2033
$this->app->singleton(Manager::class, UploadManager::class);
2134
}
35+
36+
/**
37+
* handle publishes.
38+
*/
39+
protected function handlePublishes()
40+
{
41+
$this->publishes([
42+
__DIR__.'/../config/upload.php' => $this->app->configPath().'/upload.php',
43+
], 'config');
44+
}
2245
}

tests/UploadServiceProviderTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,38 @@ public function test_register()
5353
$app->shouldHaveReceived('make')->with('Recca0120\Upload\Filesystem')->twice();
5454
$app->shouldHaveReceived('singleton')->with('Recca0120\Upload\Manager', 'Recca0120\Upload\UploadManager')->once();
5555
}
56+
57+
public function test_boot_running_in_console()
58+
{
59+
/*
60+
|------------------------------------------------------------
61+
| Arrange
62+
|------------------------------------------------------------
63+
*/
64+
65+
$app = m::spy('Illuminate\Contracts\Foundation\Application, ArrayAccess');
66+
67+
/*
68+
|------------------------------------------------------------
69+
| Act
70+
|------------------------------------------------------------
71+
*/
72+
73+
$app
74+
->shouldReceive('runningInConsole')->andReturn(true);
75+
76+
$serviceProvider = new UploadServiceProvider($app);
77+
$serviceProvider->boot();
78+
79+
/*
80+
|------------------------------------------------------------
81+
| Assert
82+
|------------------------------------------------------------
83+
*/
84+
85+
$app->shouldHaveReceived('runningInConsole')->once();
86+
$app->shouldHaveReceived('configPath')->once();
87+
}
5688
}
5789

5890
function storage_path()

tests/Uploader/FileAPITest.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function test_upload_single_file()
2020
*/
2121

2222
$config = [
23-
'chunksPath' => __DIR__,
23+
'chunks_path' => __DIR__,
2424
];
2525
$request = m::spy('Illuminate\Http\Request');
2626
$filesystem = m::spy('Recca0120\Upload\Filesystem');
@@ -37,8 +37,8 @@ public function test_upload_single_file()
3737
->shouldReceive('file')->with($inputName)->andReturn($uploadedFile);
3838

3939
$filesystem
40-
->shouldReceive('isDirectory')->with($config['chunksPath'])->andReturn(false)
41-
->shouldReceive('files')->with($config['chunksPath'])->andReturn(['file'])
40+
->shouldReceive('isDirectory')->with($config['chunks_path'])->andReturn(false)
41+
->shouldReceive('files')->with($config['chunks_path'])->andReturn(['file'])
4242
->shouldReceive('isFile')->with('file')->andReturn(true)
4343
->shouldReceive('lastModified')->with('file')->andReturn(-1);
4444

@@ -52,11 +52,11 @@ public function test_upload_single_file()
5252

5353
$this->assertSame($uploadedFile, $uploader->receive($inputName));
5454

55-
$filesystem->shouldHaveReceived('isDirectory')->with($config['chunksPath'])->once();
56-
$filesystem->shouldHaveReceived('makeDirectory')->with($config['chunksPath'], 0777, true, true)->once();
55+
$filesystem->shouldHaveReceived('isDirectory')->with($config['chunks_path'])->once();
56+
$filesystem->shouldHaveReceived('makeDirectory')->with($config['chunks_path'], 0777, true, true)->once();
5757
$request->shouldHaveReceived('header')->with('content-range')->once();
5858
$request->shouldHaveReceived('file')->with($inputName)->once();
59-
$filesystem->shouldHaveReceived('files')->with($config['chunksPath'])->once();
59+
$filesystem->shouldHaveReceived('files')->with($config['chunks_path'])->once();
6060
$filesystem->shouldHaveReceived('isFile')->with('file')->once();
6161
$filesystem->shouldHaveReceived('lastModified')->with('file')->once();
6262
$filesystem->shouldHaveReceived('delete')->with('file')->once();
@@ -71,7 +71,7 @@ public function test_upload_chunk_file_throw_chunk_response()
7171
*/
7272

7373
$config = [
74-
'chunksPath' => __DIR__,
74+
'chunks_path' => __DIR__,
7575
];
7676
$request = m::spy('Illuminate\Http\Request');
7777
$filesystem = m::spy('Recca0120\Upload\Filesystem');
@@ -83,7 +83,7 @@ public function test_upload_chunk_file_throw_chunk_response()
8383
$originalName = basename($file);
8484
$extension = pathinfo($originalName, PATHINFO_EXTENSION);
8585
$mimeType = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file);
86-
$tmpfile = $config['chunksPath'].'/'.md5($originalName.$token).'.'.$extension;
86+
$tmpfile = $config['chunks_path'].'/'.md5($originalName.$token).'.'.$extension;
8787
$tmpfileExtension = FileAPI::TMPFILE_EXTENSION;
8888

8989
$start = 5242880;
@@ -107,7 +107,7 @@ public function test_upload_chunk_file_throw_chunk_response()
107107
->shouldReceive('get')->with('token')->andReturn($token);
108108

109109
$filesystem
110-
->shouldReceive('isDirectory')->with($config['chunksPath'])->andReturn(false)
110+
->shouldReceive('isDirectory')->with($config['chunks_path'])->andReturn(false)
111111
->shouldReceive('extension')->andReturn($extension);
112112

113113
$uploader = new FileAPI($config, $request, $filesystem);
@@ -126,8 +126,8 @@ public function test_upload_chunk_file_throw_chunk_response()
126126
$this->assertSame($end, $response->headers->get('X-Last-Known-Byte'));
127127
}
128128

129-
$filesystem->shouldHaveReceived('isDirectory')->with($config['chunksPath'])->once();
130-
$filesystem->shouldHaveReceived('makeDirectory')->with($config['chunksPath'], 0777, true, true)->once();
129+
$filesystem->shouldHaveReceived('isDirectory')->with($config['chunks_path'])->once();
130+
$filesystem->shouldHaveReceived('makeDirectory')->with($config['chunks_path'], 0777, true, true)->once();
131131
$request->shouldHaveReceived('header')->with('content-range')->once();
132132
$request->shouldHaveReceived('get')->with('name')->once();
133133
$request->shouldHaveReceived('header')->with('content-disposition')->once();
@@ -146,7 +146,7 @@ public function test_upload_chunk_file()
146146
*/
147147

148148
$config = [
149-
'chunksPath' => __DIR__,
149+
'chunks_path' => __DIR__,
150150
];
151151
$request = m::spy('Illuminate\Http\Request');
152152
$filesystem = m::spy('Recca0120\Upload\Filesystem');
@@ -158,7 +158,7 @@ public function test_upload_chunk_file()
158158
$originalName = basename($file);
159159
$extension = pathinfo($originalName, PATHINFO_EXTENSION);
160160
$mimeType = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file);
161-
$tmpfile = $config['chunksPath'].'/'.md5($originalName.$token).'.'.$extension;
161+
$tmpfile = $config['chunks_path'].'/'.md5($originalName.$token).'.'.$extension;
162162
$tmpfileExtension = FileAPI::TMPFILE_EXTENSION;
163163

164164
$start = 5242880;
@@ -182,12 +182,12 @@ public function test_upload_chunk_file()
182182
->shouldReceive('get')->with('token')->andReturn($token);
183183

184184
$filesystem
185-
->shouldReceive('isDirectory')->with($config['chunksPath'])->andReturn(false)
185+
->shouldReceive('isDirectory')->with($config['chunks_path'])->andReturn(false)
186186
->shouldReceive('extension')->with($originalName)->andReturn($extension)
187187
->shouldReceive('mimeType')->with($originalName)->andReturn($mimeType)
188188
->shouldReceive('move')->with($tmpfile.$tmpfileExtension, $tmpfile)
189189
->shouldReceive('size')->with($tmpfile)->andReturn($total)
190-
->shouldReceive('files')->with($config['chunksPath'])->andReturn(['file'])
190+
->shouldReceive('files')->with($config['chunks_path'])->andReturn(['file'])
191191
->shouldReceive('isFile')->with('file')->andReturn(true)
192192
->shouldReceive('lastModified')->with('file')->andReturn(-1);
193193

@@ -201,8 +201,8 @@ public function test_upload_chunk_file()
201201

202202
$uploader->receive($inputName);
203203

204-
$filesystem->shouldHaveReceived('isDirectory')->with($config['chunksPath'])->once();
205-
$filesystem->shouldHaveReceived('makeDirectory')->with($config['chunksPath'], 0777, true, true)->once();
204+
$filesystem->shouldHaveReceived('isDirectory')->with($config['chunks_path'])->once();
205+
$filesystem->shouldHaveReceived('makeDirectory')->with($config['chunks_path'], 0777, true, true)->once();
206206
$request->shouldHaveReceived('header')->with('content-range')->once();
207207
$request->shouldHaveReceived('get')->with('name')->once();
208208
$request->shouldHaveReceived('header')->with('content-disposition')->once();
@@ -213,7 +213,7 @@ public function test_upload_chunk_file()
213213
$filesystem->shouldHaveReceived('move')->with($tmpfile.$tmpfileExtension, $tmpfile)->once();
214214
$filesystem->shouldHaveReceived('size')->once();
215215
$filesystem->shouldHaveReceived('createUploadedFile')->with($tmpfile, $originalName, $mimeType, $size)->once();
216-
$filesystem->shouldHaveReceived('files')->with($config['chunksPath'])->once();
216+
$filesystem->shouldHaveReceived('files')->with($config['chunks_path'])->once();
217217
$filesystem->shouldHaveReceived('isFile')->with('file')->once();
218218
$filesystem->shouldHaveReceived('lastModified')->with('file')->once();
219219
$filesystem->shouldHaveReceived('delete')->with('file')->once();
@@ -228,7 +228,7 @@ public function test_completed_response()
228228
*/
229229

230230
$config = [
231-
'chunksPath' => __DIR__,
231+
'chunks_path' => __DIR__,
232232
];
233233
$request = m::spy('Illuminate\Http\Request');
234234
$filesystem = m::spy('Recca0120\Upload\Filesystem');
@@ -260,7 +260,7 @@ public function test_delete_uploaded_file()
260260
*/
261261

262262
$config = [
263-
'chunksPath' => __DIR__,
263+
'chunks_path' => __DIR__,
264264
];
265265
$request = m::spy('Illuminate\Http\Request');
266266
$filesystem = m::spy('Recca0120\Upload\Filesystem');

tests/Uploader/PluploadTest.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function test_upload_single_file()
2020
*/
2121

2222
$config = [
23-
'chunksPath' => __DIR__,
23+
'chunks_path' => __DIR__,
2424
];
2525
$request = m::spy('Illuminate\Http\Request');
2626
$filesystem = m::spy('Recca0120\Upload\Filesystem');
@@ -37,8 +37,8 @@ public function test_upload_single_file()
3737
->shouldReceive('file')->with($inputName)->andReturn($uploadedFile);
3838

3939
$filesystem
40-
->shouldReceive('isDirectory')->with($config['chunksPath'])->andReturn(false)
41-
->shouldReceive('files')->with($config['chunksPath'])->andReturn(['file'])
40+
->shouldReceive('isDirectory')->with($config['chunks_path'])->andReturn(false)
41+
->shouldReceive('files')->with($config['chunks_path'])->andReturn(['file'])
4242
->shouldReceive('isFile')->with('file')->andReturn(true)
4343
->shouldReceive('lastModified')->with('file')->andReturn(-1);
4444

@@ -52,11 +52,11 @@ public function test_upload_single_file()
5252

5353
$this->assertSame($uploadedFile, $uploader->receive($inputName));
5454

55-
$filesystem->shouldHaveReceived('isDirectory')->with($config['chunksPath'])->once();
56-
$filesystem->shouldHaveReceived('makeDirectory')->with($config['chunksPath'], 0777, true, true)->once();
55+
$filesystem->shouldHaveReceived('isDirectory')->with($config['chunks_path'])->once();
56+
$filesystem->shouldHaveReceived('makeDirectory')->with($config['chunks_path'], 0777, true, true)->once();
5757
$request->shouldHaveReceived('get')->with('chunks')->once();
5858
$request->shouldHaveReceived('file')->with($inputName)->once();
59-
$filesystem->shouldHaveReceived('files')->with($config['chunksPath'])->once();
59+
$filesystem->shouldHaveReceived('files')->with($config['chunks_path'])->once();
6060
$filesystem->shouldHaveReceived('isFile')->with('file')->once();
6161
$filesystem->shouldHaveReceived('lastModified')->with('file')->once();
6262
$filesystem->shouldHaveReceived('delete')->with('file')->once();
@@ -71,7 +71,7 @@ public function test_upload_chunk_file_throw_chunk_response()
7171
*/
7272

7373
$config = [
74-
'chunksPath' => __DIR__,
74+
'chunks_path' => __DIR__,
7575
];
7676
$request = m::spy('Illuminate\Http\Request');
7777
$filesystem = m::spy('Recca0120\Upload\Filesystem');
@@ -95,7 +95,7 @@ public function test_upload_chunk_file_throw_chunk_response()
9595
->shouldReceive('file')->with($inputName)->andReturn($uploadedFile);
9696

9797
$filesystem
98-
->shouldReceive('isDirectory')->with($config['chunksPath'])->andReturn(false);
98+
->shouldReceive('isDirectory')->with($config['chunks_path'])->andReturn(false);
9999

100100
$uploader = new Plupload($config, $request, $filesystem);
101101

@@ -112,8 +112,8 @@ public function test_upload_chunk_file_throw_chunk_response()
112112
$this->assertSame(201, $response->getStatusCode());
113113
}
114114

115-
$filesystem->shouldHaveReceived('isDirectory')->with($config['chunksPath'])->once();
116-
$filesystem->shouldHaveReceived('makeDirectory')->with($config['chunksPath'], 0777, true, true)->once();
115+
$filesystem->shouldHaveReceived('isDirectory')->with($config['chunks_path'])->once();
116+
$filesystem->shouldHaveReceived('makeDirectory')->with($config['chunks_path'], 0777, true, true)->once();
117117
$request->shouldHaveReceived('get')->with('chunks')->once();
118118
$request->shouldHaveReceived('file')->with($inputName)->once();
119119
$request->shouldHaveReceived('get')->with('chunk')->once();
@@ -129,7 +129,7 @@ public function test_upload_chunk_file()
129129
*/
130130

131131
$config = [
132-
'chunksPath' => __DIR__,
132+
'chunks_path' => __DIR__,
133133
];
134134
$request = m::spy('Illuminate\Http\Request');
135135
$filesystem = m::spy('Recca0120\Upload\Filesystem');
@@ -141,7 +141,7 @@ public function test_upload_chunk_file()
141141
$originalName = basename($file);
142142
$extension = pathinfo($originalName, PATHINFO_EXTENSION);
143143
$mimeType = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file);
144-
$tmpfile = $config['chunksPath'].'/'.md5($originalName.$token).'.'.$extension;
144+
$tmpfile = $config['chunks_path'].'/'.md5($originalName.$token).'.'.$extension;
145145
$tmpfileExtension = Plupload::TMPFILE_EXTENSION;
146146

147147
$chunks = 8;
@@ -171,11 +171,11 @@ public function test_upload_chunk_file()
171171
->shouldReceive('getPathname')->andReturn($input);
172172

173173
$filesystem
174-
->shouldReceive('isDirectory')->with($config['chunksPath'])->andReturn(false)
174+
->shouldReceive('isDirectory')->with($config['chunks_path'])->andReturn(false)
175175
->shouldReceive('extension')->with($originalName)->andReturn($extension)
176176
->shouldReceive('move')->with($tmpfile.$tmpfileExtension, $tmpfile)
177177
->shouldReceive('size')->with($tmpfile)->andReturn($size)
178-
->shouldReceive('files')->with($config['chunksPath'])->andReturn(['file'])
178+
->shouldReceive('files')->with($config['chunks_path'])->andReturn(['file'])
179179
->shouldReceive('isFile')->with('file')->andReturn(true)
180180
->shouldReceive('lastModified')->with('file')->andReturn(-1);
181181

@@ -189,8 +189,8 @@ public function test_upload_chunk_file()
189189

190190
$uploader->receive($inputName);
191191

192-
$filesystem->shouldHaveReceived('isDirectory')->with($config['chunksPath'])->once();
193-
$filesystem->shouldHaveReceived('makeDirectory')->with($config['chunksPath'], 0777, true, true)->once();
192+
$filesystem->shouldHaveReceived('isDirectory')->with($config['chunks_path'])->once();
193+
$filesystem->shouldHaveReceived('makeDirectory')->with($config['chunks_path'], 0777, true, true)->once();
194194
$request->shouldHaveReceived('get')->with('chunks')->once();
195195
$request->shouldHaveReceived('file')->with($inputName)->once();
196196
$request->shouldHaveReceived('get')->with('chunk')->once();
@@ -200,7 +200,7 @@ public function test_upload_chunk_file()
200200
$filesystem->shouldHaveReceived('extension')->with($originalName)->once();
201201
$filesystem->shouldHaveReceived('appendStream')->with($tmpfile.$tmpfileExtension, $input, $start)->once();
202202
$filesystem->shouldHaveReceived('createUploadedFile')->with($tmpfile, $originalName, $mimeType, $size)->once();
203-
$filesystem->shouldHaveReceived('files')->with($config['chunksPath'])->once();
203+
$filesystem->shouldHaveReceived('files')->with($config['chunks_path'])->once();
204204
$filesystem->shouldHaveReceived('isFile')->with('file')->once();
205205
$filesystem->shouldHaveReceived('lastModified')->with('file')->once();
206206
$filesystem->shouldHaveReceived('delete')->with('file')->once();
@@ -215,7 +215,7 @@ public function test_completed_response()
215215
*/
216216

217217
$config = [
218-
'chunksPath' => __DIR__,
218+
'chunks_path' => __DIR__,
219219
];
220220
$request = m::spy('Illuminate\Http\Request');
221221
$filesystem = m::spy('Recca0120\Upload\Filesystem');
@@ -256,7 +256,7 @@ public function test_delete_uploaded_file()
256256
*/
257257

258258
$config = [
259-
'chunksPath' => __DIR__,
259+
'chunks_path' => __DIR__,
260260
];
261261
$request = m::spy('Illuminate\Http\Request');
262262
$filesystem = m::spy('Recca0120\Upload\Filesystem');

0 commit comments

Comments
 (0)