Skip to content

Commit f32612d

Browse files
authored
Merge pull request #22 from dmstr/dev/fix-export-filehandling
Export filehandling fixed
2 parents 2627110 + 8c4c7f7 commit f32612d

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/commands/PrototypeController.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
class PrototypeController extends Controller
3434
{
3535

36-
public $escapeFileNames = false;
36+
public $escapeFileNames = true;
3737
public $exportPath = '@runtime/export';
3838

3939
/**
@@ -57,17 +57,17 @@ public function actions()
5757
$actions['export-html'] = [
5858
'class' => ExportAction::class,
5959
'modelClass' => Html::class,
60-
'extention' => 'html',
60+
'extension' => 'html',
6161
];
6262
$actions['export-less'] = [
6363
'class' => ExportAction::class,
6464
'modelClass' => Less::class,
65-
'extention' => 'less',
65+
'extension' => 'less',
6666
];
6767
$actions['export-twig'] = [
6868
'class' => ExportAction::class,
6969
'modelClass' => Twig::class,
70-
'extention' => 'twig',
70+
'extension' => 'twig',
7171
];
7272
return $actions;
7373
}
@@ -77,7 +77,6 @@ public function actions()
7777
*
7878
* Returns path alias if alias defined otherwise return plain path
7979
* Removes slash from end
80-
* Add sub dir with name of extention
8180
*/
8281
public function getExportPath()
8382
{

src/commands/actions/ExportAction.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
* @author Elias Luhr <[email protected]>
3232
*
3333
* @property ActiveRecord modelClass
34-
* @property string extention
34+
* @property string extension
3535
* @property PrototypeController controller
3636
*/
3737
class ExportAction extends Action
3838
{
3939
public $modelClass;
40-
public $extention;
40+
public $extension;
4141

4242
private static $availableModelTypes = [
4343
Html::class,
@@ -53,7 +53,7 @@ class ExportAction extends Action
5353
protected function run()
5454
{
5555

56-
$this->controller->stdout("Exporting {$this->extention} files" . PHP_EOL, Console::FG_BLUE);
56+
$this->controller->stdout("Exporting {$this->extension} files" . PHP_EOL, Console::FG_BLUE);
5757
if (!class_exists($this->modelClass)) {
5858
$this->controller->stderr("Model class '{$this->modelClass}' does not exist", Console::FG_RED);
5959
return ExitCode::IOERR;
@@ -83,7 +83,15 @@ protected function run()
8383
$fileName = Inflector::slug(str_replace('/','-',$entry->key));
8484
}
8585
try {
86-
if (file_put_contents($exportPath . DIRECTORY_SEPARATOR . $fileName . '.' . $this->extention, $entry->value) === false) {
86+
// check if filename "looks" like a path, if yes, we must create subdirs
87+
if ($fileName !== basename($fileName)) {
88+
$subDir = $exportPath . DIRECTORY_SEPARATOR . trim(dirname($fileName), DIRECTORY_SEPARATOR);
89+
if (!FileHelper::createDirectory($subDir)) {
90+
throw new ErrorException("Error while creating sub directory '{$subDir}' for file '{$fileName}'");
91+
}
92+
}
93+
94+
if (file_put_contents($exportPath . DIRECTORY_SEPARATOR . $fileName . '.' . $this->extension, $entry->value) === false) {
8795
throw new ErrorException("Error while writing file for key '{$entry->key}'");
8896
}
8997
$this->controller->stdout('.');

0 commit comments

Comments
 (0)