Skip to content

Commit

Permalink
New structure
Browse files Browse the repository at this point in the history
  • Loading branch information
niksamokhvalov committed Mar 18, 2016
1 parent d86f907 commit 7efbc4a
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 75 deletions.
2 changes: 1 addition & 1 deletion bin/jedi
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

require_once __DIR__ . '/../../../autoload.php';

$app = new \Notamedia\ConsoleJedi\Console\Application();
$app = new \Notamedia\ConsoleJedi\Application\Application();
$app->run();
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* file that was distributed with this source code.
*/

namespace Notamedia\ConsoleJedi\Console\Command\Agent;
namespace Notamedia\ConsoleJedi\Agent\Command;

use Bitrix\Main\Config\Option;
use Notamedia\ConsoleJedi\Console\Command\BitrixCommand;
use Notamedia\ConsoleJedi\Application\Command\BitrixCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* file that was distributed with this source code.
*/

namespace Notamedia\ConsoleJedi\Console\Command\Agent;
namespace Notamedia\ConsoleJedi\Agent\Command;

use Notamedia\ConsoleJedi\Console\Command\BitrixCommand;
use Notamedia\ConsoleJedi\Application\Command\BitrixCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand Down
134 changes: 73 additions & 61 deletions src/Console/Application.php → src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
* file that was distributed with this source code.
*/

namespace Notamedia\ConsoleJedi\Console;
namespace Notamedia\ConsoleJedi\Application;

use Bitrix\Main\DB\ConnectionException;
use Bitrix\Main\Loader;
use Bitrix\Main\ModuleManager;
use Notamedia\ConsoleJedi\Console\Command\Agent;
use Notamedia\ConsoleJedi\Console\Command\Cache;
use Notamedia\ConsoleJedi\Console\Command\Environment;
use Notamedia\ConsoleJedi\Console\Command\InitCommand;
use Notamedia\ConsoleJedi\Agent\Command\OnCronCommand;
use Notamedia\ConsoleJedi\Agent\Command\RunCommand;
use Notamedia\ConsoleJedi\Cache\Command\ClearCommand;
use Notamedia\ConsoleJedi\Environment\Command\InitCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
Expand Down Expand Up @@ -82,19 +83,15 @@ public function doRun(InputInterface $input, OutputInterface $output)

if ($this->getConfiguration())
{
$this->addCommands([
new Agent\OnCronCommand(),
new Agent\RunCommand(),
new Cache\ClearCommand(),
new Environment\InitCommand()
]);
foreach ($this->getBitrixCommands() as $bitrixCommand)
{
$this->add($bitrixCommand);
}
}

if ($this->isBitrixLoaded() && $this->getConfiguration()['useModules'] === true)
{
$moduleCommands = $this->getModulesCommands();

foreach ($moduleCommands as $moduleCommand)
foreach ($this->getModulesCommands() as $moduleCommand)
{
$this->add($moduleCommand);
}
Expand Down Expand Up @@ -139,11 +136,72 @@ public function doRun(InputInterface $input, OutputInterface $output)
protected function getDefaultCommands()
{
$commands = parent::getDefaultCommands();
$commands[] = new InitCommand();
$commands[] = new \Notamedia\ConsoleJedi\Application\Command\InitCommand();

return $commands;
}

/**
* Gets Bitrix console commands from this package.
*
* @return Command[]
*/
protected function getBitrixCommands()
{
return [
new OnCronCommand(),
new RunCommand(),
new ClearCommand(),
new InitCommand()
];
}

/**
* Gets console commands from modules.
*
* @return Command[]
*
* @throws \Bitrix\Main\LoaderException
*/
protected function getModulesCommands()
{
$commands = [];

foreach (ModuleManager::getInstalledModules() as $module)
{
$moduleBitrixDir = $_SERVER['DOCUMENT_ROOT'] . BX_ROOT . '/modules/' . $module['ID'];
$moduleLocalDir = $_SERVER['DOCUMENT_ROOT'] . '/local/modules/' . $module['ID'];
$cliFile = '/cli.php';

if (is_file($moduleBitrixDir . $cliFile))
{
$cliFile = $moduleBitrixDir . $cliFile;
}
elseif (is_file($moduleLocalDir . $cliFile))
{
$cliFile = $moduleLocalDir . $cliFile;
}
else
{
continue;
}

if (!Loader::includeModule($module['ID']))
{
continue;
}

$config = include_once $cliFile;

if (isset($config['commands']) && is_array($config['commands']))
{
$commands = array_merge($commands, $config['commands']);
}
}

return $commands;
}

/**
* Loading application configuration.
*
Expand Down Expand Up @@ -196,52 +254,6 @@ public function getConfiguration()
return $this->configuration;
}

/**
* Gets console commands from modules.
*
* @return array
*
* @throws \Bitrix\Main\LoaderException
*/
protected function getModulesCommands()
{
$commands = [];

foreach (ModuleManager::getInstalledModules() as $module)
{
$moduleBitrixDir = $_SERVER['DOCUMENT_ROOT'] . BX_ROOT . '/modules/' . $module['ID'];
$moduleLocalDir = $_SERVER['DOCUMENT_ROOT'] . '/local/modules/' . $module['ID'];
$cliFile = '/cli.php';

if (is_file($moduleBitrixDir . $cliFile))
{
$cliFile = $moduleBitrixDir . $cliFile;
}
elseif (is_file($moduleLocalDir . $cliFile))
{
$cliFile = $moduleLocalDir . $cliFile;
}
else
{
continue;
}

if (!Loader::includeModule($module['ID']))
{
continue;
}

$config = include_once $cliFile;

if (isset($config['commands']) && is_array($config['commands']))
{
$commands = array_merge($commands, $config['commands']);
}
}

return $commands;
}

/**
* Initialize kernel of Bitrix.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* file that was distributed with this source code.
*/

namespace Notamedia\ConsoleJedi\Console\Command;
namespace Notamedia\ConsoleJedi\Application\Command;

/**
* Base class for Bitrix console command.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* file that was distributed with this source code.
*/

namespace Notamedia\ConsoleJedi\Console\Command;
namespace Notamedia\ConsoleJedi\Application\Command;

/**
* Base class for console command.
Expand All @@ -14,7 +14,7 @@
class Command extends \Symfony\Component\Console\Command\Command
{
/**
* @return \Notamedia\ConsoleJedi\Console\Application
* @return \Notamedia\ConsoleJedi\Application\Application
*/
public function getApplication()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* file that was distributed with this source code.
*/

namespace Notamedia\ConsoleJedi\Console\Command;
namespace Notamedia\ConsoleJedi\Application\Command;

use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
* file that was distributed with this source code.
*/

namespace Notamedia\ConsoleJedi\Console\Command\Cache;
namespace Notamedia\ConsoleJedi\Cache\Command;

use Bitrix\Main\Application;
use Bitrix\Main\Data\Cache;
use Bitrix\Main\Data\StaticHtmlCache;
use Notamedia\ConsoleJedi\Console\Command\BitrixCommand;
use Notamedia\ConsoleJedi\Application\Command\BitrixCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* file that was distributed with this source code.
*/

namespace Notamedia\ConsoleJedi\Console\Command\Environment;
namespace Notamedia\ConsoleJedi\Environment\Command;

use Bitrix\Main\Application;
use Bitrix\Main\Config\Configuration;
Expand All @@ -13,7 +13,7 @@
use Bitrix\Main\Loader;
use Bitrix\Main\LoaderException;
use Bitrix\Main\ModuleManager;
use Notamedia\ConsoleJedi\Console\Command\Command;
use Notamedia\ConsoleJedi\Application\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

require_once __DIR__ . '/../../../autoload.php';

$app = new \Notamedia\ConsoleJedi\Console\Application();
$app = new \Notamedia\ConsoleJedi\Application\Application();
$app->loadConfiguration();
$app->initializeBitrix();

Expand Down

0 comments on commit 7efbc4a

Please sign in to comment.