Skip to content

Commit

Permalink
✨ renamed oldFile function to rememberedFile (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
carbontwelve authored Jan 16, 2018
1 parent c05fc57 commit 58d8b2d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ Now you can assign the middleware `remember.files` to routes that you want the p
To ensure that remembered files remain as such accross page refreshes (due to other validation errors) you need to include a reference by way of using a hidden input field with the name `_rememberedFiles`.

```php
@if( $oldFile = oldFile('file'))
@if( $oldFile = rememberedFile('file'))
<input type="hidden" name="_rememberedFiles[file]" value="{{ $oldFile->getFilename() }}">
@else
<input type="file" name="file">
@endif
```

Then within your controller code you can obtain the file via the `oldFile` helper:
Then within your controller code you can obtain the file via the `rememberedFile` helper:

```php
function store(Illuminate\Http\Request $request) {
if ($file = oldFile('img', $request->file('img'))) {
if ($file = $request->file('img', rememberedFile('img')) {
// ... File exists ...
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ function clearRememberedFiles() {
}
}

if (! function_exists('oldFile')) {
if (! function_exists('rememberedFile'))
{
/**
* @param null|string $key
* @param null|mixed $default
* @return mixed|\Symfony\Component\HttpFoundation\FileBag|Illuminate\Http\UploadedFile
*/
function oldFile($key = null, $default = null) {
function rememberedFile($key = null, $default = null) {
/** @var Illuminate\Session\Store $session */
$session = app('session');

Expand All @@ -39,3 +40,13 @@ function oldFile($key = null, $default = null) {
return is_null($key) ? $fileBag : $fileBag->get($key, $default);
}
}

if (! function_exists('oldFile')) {
/**
* @deprecated
* @throws Exception
*/
function oldFile() {
throw new Exception('The oldFile function has been deprecated in favour of using rememberedFile');
}
}
20 changes: 10 additions & 10 deletions tests/UploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function testFileControllerExample()
$router = $this->app->make('router');

$router->post('test-request', function (Request $request) {
$file = oldFile('img', $request->file('img'));
$file = rememberedFile('img', $request->file('img'));
return ['ok' => true, 'filename' => $file->getFilename(), 'pathname' => $file->getPathname()];
})->middleware('remember.files');

Expand All @@ -127,7 +127,7 @@ public function testFileControllerExample()
$session->ageFlashData();

// Post the _rememberedFiles value
$response = $this->call('POST', 'test-request', ['_rememberedFiles' => ['img' => oldFile('img')->getFilename()]], [], [], ['Accept' => 'application/json']);
$response = $this->call('POST', 'test-request', ['_rememberedFiles' => ['img' => rememberedFile('img')->getFilename()]], [], [], ['Accept' => 'application/json']);
$content = json_decode($response->content());

$this->assertSame($file->getFilename(), $content->filename);
Expand Down Expand Up @@ -193,16 +193,16 @@ public function testHelper()
$this->assertTrue($response->isOk());
$session->ageFlashData();

$fileBag = oldFile();
$fileBag = rememberedFile();
$this->assertInstanceOf(FileBag::class, $fileBag);
$this->assertInstanceOf(\Symfony\Component\HttpFoundation\File\UploadedFile::class, $fileBag->get('img'));

$oldFile = oldFile('img');
$this->assertInstanceOf(\Symfony\Component\HttpFoundation\File\UploadedFile::class, $oldFile);
$rememberedFile = rememberedFile('img');
$this->assertInstanceOf(\Symfony\Component\HttpFoundation\File\UploadedFile::class, $rememberedFile);

$this->assertNull(oldFile('test'));
$this->assertTrue(oldFile('test', true));
$this->assertFalse(oldFile('test', false));
$this->assertNull(rememberedFile('test'));
$this->assertTrue(rememberedFile('test', true));
$this->assertFalse(rememberedFile('test', false));

}

Expand Down Expand Up @@ -232,7 +232,7 @@ public function testValidationPasses()
$response = $this->call('POST', 'test-validation', [], [], [], ['Accept' => 'application/json']);
$this->assertFalse($response->isOk());

// Test controller based oldFile is working.
// Test controller based rememberedFile is working.
$file = $this->mockUploadedFile(__DIR__.DIRECTORY_SEPARATOR.'stubs'.DIRECTORY_SEPARATOR.'test.jpg');
$response = $this->call('POST', 'test-validation', [], [], ['img' => $file], ['Accept' => 'application/json']);
$this->assertTrue($response->isOk());
Expand Down Expand Up @@ -271,7 +271,7 @@ public function testFilesForgottenWhenValidationFails()
$response = $this->call('POST', 'test-validation', [], [], ['img' => $file], ['Accept' => 'application/json']);
$this->assertFalse($response->isOk());

$remembered = oldFile('img');
$remembered = rememberedFile('img');
$this->assertNull($remembered);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/stubs/ValidationTestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function fileUpload(Request $request)
'img' => 'required_without:_rememberedFiles.img|mimes:jpeg'
]);

$file = oldFile('img', $request->file('img'));
$file = rememberedFile('img', $request->file('img'));

return json_encode([
'name' => $file->getFilename()
Expand Down

0 comments on commit 58d8b2d

Please sign in to comment.