diff --git a/workflow/engine/src/ProcessMaker/Util/Common.php b/workflow/engine/src/ProcessMaker/Util/Common.php new file mode 100644 index 000000000..875c1f8aa --- /dev/null +++ b/workflow/engine/src/ProcessMaker/Util/Common.php @@ -0,0 +1,92 @@ + + * @param string $pattern pattern for native glob function + * @param int|string $flags any valid flag for glob function + * @param bool $onlyFiles to filter return array with only matched files, or all matched results + * @return array array containing the recursive glob results + * + * Example: + * + * Common::rglob("/example/path/*"); + * + * it will returns: + * + * Array + * ( + * [0] => /example/path/README.txt + * [1] => /example/path/composer.json + * [4] => /example/path/one/one_text.txt + * [6] => /example/path/two/two_text.txt + * [7] => /example/path/two/two_one/two_one_text.txt + * [8] => /example/path/two/two_one/build.json + * ) + * + * Example 2: + * + * Common::rglob("/example/path/*.json"); + * + * it will returns: + * + * Array + * ( + * [0] => /example/path/composer.json + * [1] => /example/path/two/two_one/build.json + * ) + */ + public static function rglob($pattern, $flags = 0, $onlyFiles = false) + { + $singlePattern = basename($pattern); + + if (strpos($singlePattern, "*") !== false) { + $path = rtrim(str_replace($singlePattern, "", $pattern), DIRECTORY_SEPARATOR); + } else { + $singlePattern = ""; + $path = $pattern; + } + + $files = glob("$path/$singlePattern", $flags); + $dirs = glob("$path/*", GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT); + + foreach ($dirs as $dir) { + $files = array_merge($files, self::rglob("$dir/$singlePattern", $flags)); + } + + if ($onlyFiles) { + $files = array_filter($files, function($v) { return is_dir($v) ? false : true;}); + } + + return $files; + } + + /** + * Returns the last version given a pattern of file name + * + * @param string $pattern a valid pattern for glob(...) native function + * @param int $flag php flags for glob(...) native function + * @return int + */ + public static function getLastVersion($pattern, $flag = 0) + { + $files = glob($pattern, $flag); + $maxVersion = 0; + + foreach ($files as $file) { + $filename = basename($file); + + if (preg_match("/-([0-9]+)/", $filename, $match)) { + if ($maxVersion < $match[1]) { + $maxVersion = $match[1]; + } + } + } + + return $maxVersion; + } +} \ No newline at end of file diff --git a/workflow/engine/src/Tests/ProcessMaker/Util/CommonTest.php b/workflow/engine/src/Tests/ProcessMaker/Util/CommonTest.php new file mode 100644 index 000000000..2908bf56e --- /dev/null +++ b/workflow/engine/src/Tests/ProcessMaker/Util/CommonTest.php @@ -0,0 +1,37 @@ + + */ +class XmlExporterTest extends \PHPUnit_Framework_TestCase +{ + function testGetLastVersion() + { + $lastVer = Util\Common::getLastVersion(__DIR__."/../../fixtures/files_struct/first/sample-*.txt"); + $this->assertEquals(3, $lastVer); + } + + function testGetLastVersionSec() + { + $lastVer = Util\Common::getLastVersion(__DIR__."/../../fixtures/files_struct/second/sample-*.txt"); + + $this->assertEquals(5, $lastVer); + } + + /** + * Negative test, no matched files found + */ + function testGetLastVersionThr() + { + $lastVer = Util\Common::getLastVersion(sys_get_temp_dir()."/sample-*.txt"); + + $this->assertEquals(0, $lastVer); + } +} \ No newline at end of file diff --git a/workflow/engine/src/Tests/fixtures/files_struct/first/sample-1.txt b/workflow/engine/src/Tests/fixtures/files_struct/first/sample-1.txt new file mode 100644 index 000000000..c4de27d5a --- /dev/null +++ b/workflow/engine/src/Tests/fixtures/files_struct/first/sample-1.txt @@ -0,0 +1 @@ +file sample-1.txt \ No newline at end of file diff --git a/workflow/engine/src/Tests/fixtures/files_struct/first/sample-2.txt b/workflow/engine/src/Tests/fixtures/files_struct/first/sample-2.txt new file mode 100644 index 000000000..c4de27d5a --- /dev/null +++ b/workflow/engine/src/Tests/fixtures/files_struct/first/sample-2.txt @@ -0,0 +1 @@ +file sample-1.txt \ No newline at end of file diff --git a/workflow/engine/src/Tests/fixtures/files_struct/first/sample-3.txt b/workflow/engine/src/Tests/fixtures/files_struct/first/sample-3.txt new file mode 100644 index 000000000..c4de27d5a --- /dev/null +++ b/workflow/engine/src/Tests/fixtures/files_struct/first/sample-3.txt @@ -0,0 +1 @@ +file sample-1.txt \ No newline at end of file diff --git a/workflow/engine/src/Tests/fixtures/files_struct/second/sample-1.txt b/workflow/engine/src/Tests/fixtures/files_struct/second/sample-1.txt new file mode 100644 index 000000000..c4de27d5a --- /dev/null +++ b/workflow/engine/src/Tests/fixtures/files_struct/second/sample-1.txt @@ -0,0 +1 @@ +file sample-1.txt \ No newline at end of file diff --git a/workflow/engine/src/Tests/fixtures/files_struct/second/sample-2.txt b/workflow/engine/src/Tests/fixtures/files_struct/second/sample-2.txt new file mode 100644 index 000000000..c4de27d5a --- /dev/null +++ b/workflow/engine/src/Tests/fixtures/files_struct/second/sample-2.txt @@ -0,0 +1 @@ +file sample-1.txt \ No newline at end of file diff --git a/workflow/engine/src/Tests/fixtures/files_struct/second/sample-3.txt b/workflow/engine/src/Tests/fixtures/files_struct/second/sample-3.txt new file mode 100644 index 000000000..c4de27d5a --- /dev/null +++ b/workflow/engine/src/Tests/fixtures/files_struct/second/sample-3.txt @@ -0,0 +1 @@ +file sample-1.txt \ No newline at end of file diff --git a/workflow/engine/src/Tests/fixtures/files_struct/second/sample-5.txt b/workflow/engine/src/Tests/fixtures/files_struct/second/sample-5.txt new file mode 100644 index 000000000..c4de27d5a --- /dev/null +++ b/workflow/engine/src/Tests/fixtures/files_struct/second/sample-5.txt @@ -0,0 +1 @@ +file sample-1.txt \ No newline at end of file