Skip to content

Commit 3de3f5e

Browse files
committed
Fixes a bug in the file uploading processes
1 parent 5365849 commit 3de3f5e

File tree

9 files changed

+36
-44
lines changed

9 files changed

+36
-44
lines changed

config/codegenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
|
5151
|
5252
*/
53-
'files_upload_path' => public_path('uploads'),
53+
'files_upload_path' => 'uploads',
5454

5555
/*
5656
|--------------------------------------------------------------------------

src/Commands/.php_cs.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"php":"5.6.24","version":"2.1.2:","rules":{"encoding":true,"full_opening_tag":true,"blank_line_after_namespace":true,"braces":true,"class_definition":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_constants":true,"lowercase_keywords":true,"method_argument_space":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":["property"],"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true},"hashes":{"CreateControllerCommand.php":-1155478208,"CreateCreateViewCommand.php":131691758,"CreateEditViewCommand.php":1171483022,"CreateFieldsFileCommand.php":1361178139,"CreateFormRequestCommand.php":194589446,"CreateFormViewCommand.php":1806670607,"CreateIndexViewCommand.php":-1734787902,"CreateLanguageCommand.php":-1844147042,"CreateLayoutCommand.php":396331127,"CreateMigrationCommand.php":1405553898,"CreateModelCommand.php":271158263,"CreateResourcesCommand.php":-1298335991,"CreateRoutesCommand.php":-1996040625,"CreateShowViewCommand.php":2103655095,"CreateViewLayoutCommand.php":213476386,"CreateViewsCommand.php":651544166}}
1+
{"php":"5.6.24","version":"2.1.2:","rules":{"encoding":true,"full_opening_tag":true,"blank_line_after_namespace":true,"braces":true,"class_definition":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_constants":true,"lowercase_keywords":true,"method_argument_space":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":["property"],"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true},"hashes":{"CreateControllerCommand.php":-138046371,"CreateCreateViewCommand.php":131691758,"CreateEditViewCommand.php":1171483022,"CreateFieldsFileCommand.php":1361178139,"CreateFormRequestCommand.php":194589446,"CreateFormViewCommand.php":1806670607,"CreateIndexViewCommand.php":-1734787902,"CreateLanguageCommand.php":-1844147042,"CreateLayoutCommand.php":396331127,"CreateMigrationCommand.php":1405553898,"CreateModelCommand.php":271158263,"CreateResourcesCommand.php":-1298335991,"CreateRoutesCommand.php":-1996040625,"CreateShowViewCommand.php":2103655095,"CreateViewLayoutCommand.php":213476386,"CreateViewsCommand.php":651544166}}

src/Commands/CreateControllerCommand.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,15 +377,19 @@ public function getNameInput()
377377
protected function getFileSnippet(array $fields)
378378
{
379379
$code = '';
380+
$template = <<<EOF
381+
if (\$request->hasFile('%s')) {
382+
\$data['%s'] = \$this->moveFile(\$request->file('%s'));
383+
}
384+
EOF;
380385

381386
foreach ($fields as $field) {
382387
if ($field->isFile()) {
383-
$code = ($code) ?: ' $this';
384-
$code .= sprintf("->uploadFile('%s', \$data)", $field->name);
388+
$code .= sprintf($template, $field->name, $field->name, $field->name);
385389
}
386390
}
387391

388-
return $code != '' ? $code . ';' : $code;
392+
return $code;
389393
}
390394

391395
/**
@@ -421,7 +425,7 @@ protected function getStringToNullSnippet(array $fields)
421425
$code = '';
422426

423427
foreach ($fields as $field) {
424-
if ($field->isNullable && !$field->isPrimary && !$field->isAutoIncrement && !$field->isRequired() && !$field->isBoolean()) {
428+
if ($field->isNullable && !$field->isPrimary && !$field->isAutoIncrement && !$field->isRequired() && !$field->isBoolean() && !$field->isFile()) {
425429
$code .= sprintf(" \$data['%s'] = !empty(\$request->input('%s')) ? \$request->input('%s') : null;", $field->name, $field->name, $field->name) . PHP_EOL;
426430
}
427431
}
Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
1+
22
/**
3-
* Moves the uploaded file to the server.
3+
* Moves the attached file to the server.
44
*
5-
* @param string $name
6-
* @param array $data
5+
* @param Symfony\Component\HttpFoundation\File\UploadedFile $file
76
*
8-
* @return $this
7+
* @return string
98
*/
10-
protected function uploadFile($name, array & $data)
9+
protected function moveFile($file)
1110
{
12-
$file = Input::file($name);
13-
14-
if( $file && $file->isValid() )
15-
{
16-
$destination = config('codegenerator.files_upload_path');
17-
$newName = sprintf('%s.%s', str_random(30), $file->getClientOriginalExtension());
18-
$file->move($destination, $newName);
19-
$data[$name] = $file->getRealPath() . '/' . $newName;
11+
if (! $file->isValid()) {
12+
return '';
2013
}
21-
22-
return $this;
14+
15+
$uploadPath = config('codegenerator.files_upload_path');
16+
$name = sprintf('%s.%s', str_random(30), $file->getClientOriginalExtension());
17+
$file->move(public_path($uploadPath), $name);
18+
19+
return asset($uploadPath) . "/". $name;
2320
}

templates/default-collective/controller-with-form-request.stub

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ namespace DummyNamespace;
55
use Session;
66
use Illuminate\Http\Request;
77
use DummyRootNamespaceHttp\Controllers\Controller;
8-
use Illuminate\Support\Facades\Input;
9-
108
use DummyRootNamespace{{modelFullName}};
119
use DummyRootNamespace{{formRequestFullName}};
1210

templates/default-collective/controller.stub

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ namespace DummyNamespace;
44

55
use Session;
66
use Illuminate\Http\Request;
7-
use Illuminate\Support\Facades\Input;
87
use DummyRootNamespaceHttp\Controllers\Controller;
98
use DummyRootNamespace{{modelFullName}};
109

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
1+
22
/**
3-
* Moves the uploaded file to the server.
3+
* Moves the attached file to the server.
44
*
5-
* @param string $name
6-
* @param array $data
5+
* @param Symfony\Component\HttpFoundation\File\UploadedFile $file
76
*
8-
* @return $this
7+
* @return string
98
*/
10-
protected function uploadFile($name, array & $data)
9+
protected function moveFile($file)
1110
{
12-
$file = Input::file($name);
13-
14-
if( $file && $file->isValid() )
15-
{
16-
$destination = config('codegenerator.files_upload_path');
17-
$newName = sprintf('%s.%s', str_random(30), $file->getClientOriginalExtension());
18-
$file->move($destination, $newName);
19-
$data[$name] = $file->getRealPath() . '/' . $newName;
11+
if (! $file->isValid()) {
12+
return '';
2013
}
21-
22-
return $this;
14+
15+
$uploadPath = config('codegenerator.files_upload_path');
16+
$name = sprintf('%s.%s', str_random(30), $file->getClientOriginalExtension());
17+
$file->move(public_path($uploadPath), $name);
18+
19+
return asset($uploadPath) . "/". $name;
2320
}

templates/default/controller-with-form-request.stub

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ namespace DummyNamespace;
55
use Session;
66
use Illuminate\Http\Request;
77
use DummyRootNamespaceHttp\Controllers\Controller;
8-
use Illuminate\Support\Facades\Input;
9-
108
use DummyRootNamespace{{modelFullName}};
119
use DummyRootNamespace{{formRequestFullName}};
1210

templates/default/controller.stub

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ namespace DummyNamespace;
44

55
use Session;
66
use Illuminate\Http\Request;
7-
use Illuminate\Support\Facades\Input;
87
use DummyRootNamespaceHttp\Controllers\Controller;
98
use DummyRootNamespace{{modelFullName}};
109

0 commit comments

Comments
 (0)