/** * Runs plugin with supplied arguments. * * @li First element in @c $args must be a path to ZIP archive with input data. * @li Plugin execution is stopped on all triggered errors or uncaught exceptions * and plugin error is returned. * @li setUp() and execute() methods are run respectively after input is * unpacked and output folder is created. * @li Plugin creates and later removes two temporary folders. It can also create * one ZIP archive with plugin output (path is returned in plugin results). * * @param array $args simple array with plugin arguments * @return PluginResponse plugin results or error */ public final function run(array $args) { set_error_handler(array('asm\\utils\\Utils', 'turnErrorToException')); $cwd = getcwd(); try { if ($args == null || count($args) < 1) { throw new PluginUseException('Data file argument missing'); } $this->dataFolder = Filesystem::tempDir(); $dataFile = array_shift($args); if (!Compression::unzip($dataFile, $this->dataFolder, $unzipMessage)) { $response = PluginResponse::createError("ZIP extraction failed (" . $unzipMessage . ").\n\nPerhaps you did not submit a ZIP file " . "or that file was corrupted during upload. You may try again. Extraction was attempted to folder " . str_replace("\\", "/", $this->dataFolder)); } else { $this->outputFolder = Filesystem::tempDir(); chdir($this->dataFolder); $this->setUp($args); $this->execute(); chdir($cwd); $outputFile = $this->packOutput(); $outputPath = $outputFile != null ? realpath($outputFile) : null; $response = PluginResponse::create($this->results, $outputPath); } } catch (PluginException $e) { $response = PluginResponse::createError($e->getMessage()); } catch (Exception $e) { $response = PluginResponse::createError('Runtime error: ' . $e->getMessage() . " (file " . $e->getFile() . ", line " . $e->getLine()); } // If an exception occurred during $this->setUp or $this->execute, we must still change the current directory back, // in case more plugins are to be run (in a test case battery) chdir($cwd); restore_error_handler(); if ($this->dataFolder != null) { Filesystem::removeDir($this->dataFolder); } if ($this->outputFolder != null) { Filesystem::removeDir($this->outputFolder); } return $response; }
public function testTempDir() { $folder = Filesystem::tempDir(null, "hello"); $this->assertStringStartsWith('hello', basename($folder)); $this->assertTrue(is_dir($folder)); $this->assertEquals(0, count(array_diff(scandir($folder), ['.', '..']))); $this->assertTrue(Filesystem::removeDir($folder)); $this->assertFalse(is_dir($folder)); }