From 0cf729d8f10e9ed54b845532a8bf823d986e77c4 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Fri, 21 Sep 2018 15:51:58 +0200 Subject: [PATCH] added `LINK_SCANNER_TARGET` constant --- config/bootstrap.php | 17 +++++++++++++++-- src/Utility/LinkScanner.php | 6 +++--- tests/TestCase/Shell/LinkScannerShellTest.php | 6 +++--- tests/TestCase/Utility/LinkScannerTest.php | 4 ++-- tests/bootstrap.php | 5 ++--- 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/config/bootstrap.php b/config/bootstrap.php index d14250b..003d2fc 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -22,11 +22,24 @@ define('LINK_SCANNER_LOCK_FILE', TMP . 'link_scanner_lock_file'); } +//Sets the path where to export results +if (!defined('LINK_SCANNER_TARGET')) { + define('LINK_SCANNER_TARGET', TMP . 'link-scanner'); +} + +if (!file_exists(LINK_SCANNER_TARGET)) { + safe_mkdir(LINK_SCANNER_TARGET); +} + +if (!is_writeable(LINK_SCANNER_TARGET)) { + trigger_error(sprintf('Directory %s not writeable', LINK_SCANNER_TARGET), E_USER_ERROR); +} + if (!Cache::getConfig(LINK_SCANNER)) { Cache::setConfig(LINK_SCANNER, [ 'className' => 'File', 'duration' => '+1 day', - 'path' => CACHE . 'link_scanner', - 'prefix' => null, + 'path' => CACHE, + 'prefix' => 'link_scanner_', ]); } diff --git a/src/Utility/LinkScanner.php b/src/Utility/LinkScanner.php index 4c56c69..0b9d3f3 100644 --- a/src/Utility/LinkScanner.php +++ b/src/Utility/LinkScanner.php @@ -227,8 +227,8 @@ public function export($filename = null) } try { - $filename = $filename ?: TMP . sprintf('results_%s_%s', $this->hostname, $this->startTime); - $filename = Folder::isAbsolute($filename) ? $filename : TMP . $filename; + $filename = $filename ?: sprintf('results_%s_%s', $this->hostname, $this->startTime); + $filename = Folder::isAbsolute($filename) ? $filename : LINK_SCANNER_TARGET . DS . $filename; file_put_contents($filename, serialize($this)); } catch (Exception $e) { $message = preg_replace('/^file_put_contents\([\/\w\d:\-\\\\]+\): /', null, $e->getMessage()); @@ -256,7 +256,7 @@ public function export($filename = null) public static function import($filename) { try { - $filename = Folder::isAbsolute($filename) ? $filename : TMP . $filename; + $filename = Folder::isAbsolute($filename) ? $filename : LINK_SCANNER_TARGET . DS . $filename; $instance = unserialize(file_get_contents($filename)); } catch (Exception $e) { $message = preg_replace('/^file_get_contents\([\/\w\d:\-\\\\]+\): /', null, $e->getMessage()); diff --git a/tests/TestCase/Shell/LinkScannerShellTest.php b/tests/TestCase/Shell/LinkScannerShellTest.php index b7379ee..29848ab 100644 --- a/tests/TestCase/Shell/LinkScannerShellTest.php +++ b/tests/TestCase/Shell/LinkScannerShellTest.php @@ -192,7 +192,7 @@ public function testScanParams() $this->assertEventFired(LINK_SCANNER . '.resultsExported', $this->EventManager); - $expectedExportFile = TMP . 'results_' . $this->LinkScanner->hostname . '_' . $this->LinkScanner->startTime; + $expectedExportFile = LINK_SCANNER_TARGET . DS . 'results_' . $this->LinkScanner->hostname . '_' . $this->LinkScanner->startTime; $this->assertFileExists($expectedExportFile); $messages = $this->out->messages(); @@ -207,8 +207,8 @@ public function testScanParams() $this->assertEquals($params['timeout'], $this->LinkScanner->Client->getConfig('timeout')); foreach ([ - 'example' => TMP . 'example', - sys_get_temp_dir() . DS . 'example' => sys_get_temp_dir() . DS . 'example', + 'example' => LINK_SCANNER_TARGET . DS . 'example', + TMP . 'example' => TMP . 'example', ] as $filename => $expectedExportFile) { $this->LinkScannerShell = $this->getLinkScannerShell(); $this->LinkScannerShell->params = ['export' => $filename] + $params; diff --git a/tests/TestCase/Utility/LinkScannerTest.php b/tests/TestCase/Utility/LinkScannerTest.php index a12c8d3..1f196ad 100644 --- a/tests/TestCase/Utility/LinkScannerTest.php +++ b/tests/TestCase/Utility/LinkScannerTest.php @@ -147,8 +147,8 @@ public function testExport() //Filename can be `null`, relative or absolute foreach ([ - null => TMP . 'results_' . $this->LinkScanner->hostname . '_' . $this->LinkScanner->startTime, - 'example' => TMP . 'example', + null => LINK_SCANNER_TARGET . DS . 'results_' . $this->LinkScanner->hostname . '_' . $this->LinkScanner->startTime, + 'example' => LINK_SCANNER_TARGET . DS . 'example', TMP . 'example' => TMP . 'example', ] as $filenameWhereToExport => $expectedFilename) { $result = $this->LinkScanner->export($filenameWhereToExport); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 4100594..10cac3d 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -34,13 +34,12 @@ define('APP_DIR', 'TestApp'); define('WEBROOT_DIR', 'webroot'); define('WWW_ROOT', APP . 'webroot' . DS); -define('TMP', sys_get_temp_dir() . DS . 'cakephp-link-scanner' . DS . 'tmp' . DS); -define('CACHE', sys_get_temp_dir() . DS . 'cakephp-link-scanner' . DS . 'cache' . DS); +define('TMP', sys_get_temp_dir() . DS . 'cakephp-link-scanner' . DS); +define('CACHE', TMP . 'cache' . DS); define('CONFIG', APP . 'config' . DS); define('LOGS', TMP); define('SESSIONS', TMP . 'sessions' . DS); -safe_mkdir(sys_get_temp_dir() . DS . 'cakephp-link-scanner'); safe_mkdir(TMP); safe_mkdir(LOGS); safe_mkdir(SESSIONS);