diff --git a/src/Application/Application.php b/src/Application/Application.php index 70ac241..0cde197 100644 --- a/src/Application/Application.php +++ b/src/Application/Application.php @@ -11,6 +11,7 @@ use Bitrix\Main\ModuleManager; use Notamedia\ConsoleJedi\Agent\Command\OnCronCommand; use Notamedia\ConsoleJedi\Agent\Command\RunCommand; +use Notamedia\ConsoleJedi\Application\Exception\ConfigurationException; use Notamedia\ConsoleJedi\Cache\Command\ClearCommand; use Notamedia\ConsoleJedi\Environment\Command\InitCommand; use Symfony\Component\Console\Command\Command; @@ -209,7 +210,7 @@ protected function getModulesCommands() * * @return bool * - * @throws \Exception + * @throws ConfigurationException */ public function loadConfiguration($path = self::CONFIG_DEFAULT_FILE) { @@ -222,7 +223,7 @@ public function loadConfiguration($path = self::CONFIG_DEFAULT_FILE) if (!is_array($this->configuration)) { - throw new \Exception('Configuration file ' . $path . ' must return an array'); + throw new ConfigurationException('Configuration file ' . $path . ' must return an array'); } $filesystem = new Filesystem(); @@ -261,7 +262,11 @@ public function getConfiguration() */ public function initializeBitrix() { - if (!$this->checkBitrix()) + if ($this->bitrixStatus === static::BITRIX_STATUS_COMPLETE) + { + return static::BITRIX_STATUS_COMPLETE; + } + elseif (!$this->checkBitrix()) { return static::BITRIX_STATUS_UNAVAILABLE; } @@ -324,6 +329,62 @@ public function isBitrixLoaded() return $this->bitrixStatus === static::BITRIX_STATUS_COMPLETE; } + public function autoloadTests() + { + if ($this->getConfiguration() === null) + { + $this->loadConfiguration(); + } + + $this->initializeBitrix(); + + spl_autoload_register(function ($className) { + $file = ltrim($className, "\\"); + $file = strtr($file, Loader::ALPHA_UPPER, Loader::ALPHA_LOWER); + $file = str_replace('\\', '/', $file); + + if (substr($file, -5) === 'table') + { + $file = substr($file, 0, -5); + } + + $arFile = explode('/', $file); + + if (preg_match("#[^\\\\/a-zA-Z0-9_]#", $file)) + { + return false; + } + elseif ($arFile[0] === 'bitrix') + { + return false; + } + elseif ($arFile[2] !== 'tests') + { + return false; + } + + $module = array_shift($arFile) . '.' . array_shift($arFile); + + if (!Loader::includeModule($module)) + { + return false; + } + + $file = $module . '/' . implode('/', $arFile) . '.php'; + $bitrixPath = \Bitrix\Main\Application::getDocumentRoot() . '/' . Loader::BITRIX_HOLDER . '/modules/' . $file; + $localPath = \Bitrix\Main\Application::getDocumentRoot() . '/' . Loader::LOCAL_HOLDER . '/modules/' . $file; + + if (file_exists($bitrixPath)) + { + include_once $bitrixPath; + } + elseif (file_exists($localPath)) + { + include_once $localPath; + } + }); + } + /** * Gets root directory from which are running Console Jedi. * diff --git a/src/Application/Exception/ConfigurationException.php b/src/Application/Exception/ConfigurationException.php new file mode 100644 index 0000000..14dd4b6 --- /dev/null +++ b/src/Application/Exception/ConfigurationException.php @@ -0,0 +1,17 @@ + + */ +class ConfigurationException extends \ErrorException +{ + +} \ No newline at end of file diff --git a/src/Loader.php b/src/Loader.php deleted file mode 100644 index 9eb094f..0000000 --- a/src/Loader.php +++ /dev/null @@ -1,61 +0,0 @@ -loadConfiguration(); -$app->initializeBitrix(); - -$loader = new \Notamedia\ConsoleJedi\Loader(); -$loader->loadTests(); \ No newline at end of file +$app->autoloadTests(); \ No newline at end of file