Skip to content

Commit 90927b9

Browse files
committed
Improve output and code style of CLI controller
1 parent 0a8c8ed commit 90927b9

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

Classes/Command/ResourceToolsCommandController.php

+25-14
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use Neos\Flow\Annotations as Flow;
66
use Neos\Flow\Cli\CommandController;
7+
use Neos\Flow\ResourceManagement\Exception;
78
use Neos\Flow\ResourceManagement\ResourceManager;
89
use Neos\Flow\ResourceManagement\Storage\StorageObject;
10+
use Neos\Utility\Exception\FilesException;
911
use Neos\Utility\Files;
1012

1113
final class ResourceToolsCommandController extends CommandController
@@ -25,12 +27,13 @@ final class ResourceToolsCommandController extends CommandController
2527
* @param string $targetPath The path where the resource files should be exported to.
2628
* @param bool $emptyTargetPath If set, all files in the target path will be deleted before the export runs
2729
* @param string $collection Name of the resource collection to export. Default: "persistent"
30+
* @throws FilesException
2831
*/
29-
public function exportCommand(string $targetPath, bool $emptyTargetPath = false, $collection = 'persistent')
32+
public function exportCommand(string $targetPath, bool $emptyTargetPath = false, $collection = 'persistent'): void
3033
{
3134
if (!is_dir($targetPath)) {
3235
$this->outputLine('The target path does not exist.');
33-
$this->quit(1);
36+
exit(1);
3437
}
3538
$targetPath = realpath($targetPath) . '/';
3639

@@ -70,11 +73,11 @@ public function exportCommand(string $targetPath, bool $emptyTargetPath = false,
7073
* @param string $sourcePath The path where the resource files are located
7174
* @param string $collection Name of the resource collection to match with. Default: "persistent"
7275
*/
73-
public function matchCommand(string $sourcePath, string $collection = 'persistent')
76+
public function matchCommand(string $sourcePath, string $collection = 'persistent'): void
7477
{
7578
if (!is_dir($sourcePath)) {
7679
$this->outputLine('The source path does not exist.');
77-
$this->quit(1);
80+
exit(1);
7881
}
7982

8083
$sourcePath = rtrim($sourcePath,'/') . '/';
@@ -89,8 +92,12 @@ public function matchCommand(string $sourcePath, string $collection = 'persisten
8992
if ($stream === false) {
9093
if (file_exists($sourcePath . $object->getSha1())) {
9194
sha1_file($sourcePath . $object->getSha1());
92-
$persistentCollection->importResource(fopen($sourcePath . $object->getSha1(), 'r'));
93-
$this->outputLine('<success>imported</success> %s %s', [$object->getSha1(), $object->getFilename()]);
95+
try {
96+
$persistentCollection->importResource(fopen($sourcePath . $object->getSha1(), 'r'));
97+
$this->outputLine('<success>imported</success> %s %s', [$object->getSha1(), $object->getFilename()]);
98+
} catch (Exception $e) {
99+
$this->outputLine('<error>failed</error> %s %s %s', [$object->getSha1(), $object->getFilename(), $e->getMessage()]);
100+
}
94101
} else {
95102
$this->outputLine('<error>missing </error> %s %s', [$object->getSha1(), $object->getFilename()]);
96103
}
@@ -106,16 +113,20 @@ public function matchCommand(string $sourcePath, string $collection = 'persisten
106113
* @param string $source Can be anything PHP can open (filepath, URL, data string, etc.)
107114
* @param string $filename The filename (if empty string it will try to fetch from stream or leave empty)
108115
* @param string $collection
109-
* @throws \Neos\Flow\ResourceManagement\Exception
110116
*/
111-
public function importFileCommand(string $source, string $filename, string $collection = 'persistent')
117+
public function importFileCommand(string $source, string $filename, string $collection = 'persistent'): void
112118
{
113-
$fileHandler = fopen($source, 'r');
114-
$resource = $this->resourceManager->importResource($fileHandler, $collection);
115-
if ($filename !== '') {
116-
$resource->setFilename($filename);
117-
}
119+
$fileHandler = fopen($source, 'rb');
120+
try {
121+
$resource = $this->resourceManager->importResource($fileHandler, $collection);
122+
if ($filename !== '') {
123+
$resource->setFilename($filename);
124+
}
118125

119-
$this->outputLine('Imported file as resource "%s"', [$resource->getSha1()]);
126+
$this->outputLine('Imported file as resource "%s"', [$resource->getSha1()]);
127+
} catch (Exception $e) {
128+
$this->outputLine('<error>Failed importing file</error> %s', [$e->getMessage()]);
129+
exit(1);
130+
}
120131
}
121132
}

0 commit comments

Comments
 (0)