diff --git a/src/Meritoo/Common/Utilities/Miscellaneous.php b/src/Meritoo/Common/Utilities/Miscellaneous.php index a5e3a08..309be62 100644 --- a/src/Meritoo/Common/Utilities/Miscellaneous.php +++ b/src/Meritoo/Common/Utilities/Miscellaneous.php @@ -1473,4 +1473,37 @@ class Miscellaneous return $invertedColor; } + + /** + * Returns project's root path. + * Looks for directory that contains composer.json. + * + * @return string + */ + public static function getProjectRootPath() + { + $projectRootPath = ''; + + $fileName = 'composer.json'; + $directoryPath = __DIR__; + + /* + * Path of directory it's not the path of last directory? + */ + while (DIRECTORY_SEPARATOR !== $directoryPath) { + $filePath = static::concatenatePaths($directoryPath, $fileName); + + /* + * Is here file we are looking for? + * Maybe it's a project's root path + */ + if (file_exists($filePath)) { + $projectRootPath = $directoryPath; + } + + $directoryPath = dirname($directoryPath); + } + + return $projectRootPath; + } } diff --git a/tests/Meritoo/Common/Test/Utilities/MiscellaneousTest.php b/tests/Meritoo/Common/Test/Utilities/MiscellaneousTest.php index 5ec8b9b..b4f9267 100644 --- a/tests/Meritoo/Common/Test/Utilities/MiscellaneousTest.php +++ b/tests/Meritoo/Common/Test/Utilities/MiscellaneousTest.php @@ -742,6 +742,11 @@ class MiscellaneousTest extends BaseTestCase self::assertSame($expected, Miscellaneous::fillMissingZeros($number, $length, $before)); } + public function testGetProjectRootPath() + { + self::assertNotEmpty(Miscellaneous::getProjectRootPath()); + } + /** * Provides string to convert characters to latin characters and not lower cased and not human-readable *