Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/N98/Magento/Command/Developer/Console/MakeBlockCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,11 @@ private function createClassFile(InputInterface $input, OutputInterface $output)

/** @var $classGenerator ClassGenerator */
$classGenerator->addUse('Magento\Framework\View\Element\Template');

if (version_compare($this->getMagentoVersion()->getVersion(), '2.2.0', '<')) {
$classGenerator->setExtendedClass('Template');
} else {
$classGenerator->setExtendedClass('Magento\Framework\View\Element\Template');
}

$classGenerator->setExtendedClass('Magento\Framework\View\Element\Template');
$classGenerator->setName($classNameToGenerate);

$fileGenerator = FileGenerator::fromArray(
[
'classes' => [$classGenerator],
]
);
$fileGenerator = new FileGenerator();
$fileGenerator->setClass($classGenerator);

$directoryWriter = $this->getCurrentModuleDirectoryWriter();
$directoryWriter->writeFile($filePathToGenerate, $fileGenerator->generate());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ protected function catchedExecute(InputInterface $input, OutputInterface $output
$classGenerator = $this->create(ClassGenerator::class);
$classGenerator->setName($classNameToGenerate);

$fileGenerator = FileGenerator::fromArray([
'classes' => [$classGenerator],
]);
$fileGenerator = new FileGenerator();
$fileGenerator->setClass($classGenerator);

$directoryWriter = $this->getCurrentModuleDirectoryWriter();
$directoryWriter->writeFile($filePathToGenerate, $fileGenerator->generate());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$classGenerator->addUse('Symfony\Component\Console\Command\Command');
$classGenerator->addUse('Symfony\Component\Console\Input\InputInterface');
$classGenerator->addUse('Symfony\Component\Console\Output\OutputInterface');

if (version_compare($this->getMagentoVersion()->getVersion(), '2.2.0', '<')) {
$classGenerator->setExtendedClass('Command');
} else {
$classGenerator->setExtendedClass('Symfony\Component\Console\Command\Command');
}
$classGenerator->setExtendedClass('Symfony\Component\Console\Command\Command');

$commandName = $this->prepareCommandName($input);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,9 @@ protected function catchedExecute(InputInterface $input, OutputInterface $output
. $this->getNormalizedClassnameByArgument($input->getArgument('classpath'));
$filePathToGenerate = 'Controller/' . $actionFileName . '.php';

$classGenerator = $this->create(ClassGenerator::class);

/** @var $classGenerator ClassGenerator */
if (version_compare($this->getMagentoVersion()->getVersion(), '2.2.0', '<')) {
$classGenerator->setExtendedClass('Action');
} else {
$classGenerator->setExtendedClass('Magento\Framework\App\Action\Action');
}
$classGenerator = $this->create(ClassGenerator::class);
$classGenerator->setExtendedClass('Magento\Framework\App\Action\Action');

$body = $this->createClassBody($input);
$executeMethodDefinition = $this->createClassMethodDefinitions($body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ protected function catchedExecute(InputInterface $input, OutputInterface $output

/** @var $classGenerator ClassGenerator */
$classGenerator->addUse('Magento\Framework\App\Helper\AbstractHelper');

if (version_compare($this->getMagentoVersion()->getVersion(), '2.2.0', '<')) {
$classGenerator->setExtendedClass('AbstractHelper');
} else {
$classGenerator->setExtendedClass('Magento\Framework\App\Helper\AbstractHelper');
}
$classGenerator->setExtendedClass('Magento\Framework\App\Helper\AbstractHelper');

$classGenerator->setName($classNameToGenerate);

Expand Down
17 changes: 4 additions & 13 deletions src/N98/Magento/Command/Developer/Console/MakeModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,12 @@ protected function catchedExecute(InputInterface $input, OutputInterface $output
$classGenerator = $this->create(ClassGenerator::class);

/** @var $classGenerator ClassGenerator */
$classGenerator->addUse('Magento\Framework\Model\AbstractModel');

if (version_compare($this->getMagentoVersion()->getVersion(), '2.2.0', '<')) {
$classGenerator->setExtendedClass('AbstractModel');
} else {
$classGenerator->setExtendedClass('Magento\Framework\Model\AbstractModel');
}

$classGenerator->addUse('Magento\\Framework\\Model\\AbstractModel');
$classGenerator->setExtendedClass('Magento\\Framework\\Model\\AbstractModel');
$classGenerator->setName($classNameToGenerate);

$modelFileGenerator = FileGenerator::fromArray(
[
'classes' => [$classGenerator],
]
);
$modelFileGenerator = new FileGenerator();
$modelFileGenerator->setClass($classGenerator);

$directoryWriter = $this->getCurrentModuleDirectoryWriter();
$directoryWriter->writeFile($filePathToGenerate, $modelFileGenerator->generate());
Expand Down
78 changes: 37 additions & 41 deletions src/N98/Magento/Command/ScriptCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace N98\Magento\Command;

use Exception;
use function file_get_contents;
use InvalidArgumentException;
use Magento\Framework\App\DistributionMetadataInterface;
use Magento\Framework\App\ProductMetadataInterface;
use N98\Util\BinaryString;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\RuntimeException;
Expand Down Expand Up @@ -36,9 +41,26 @@ class ScriptCommand extends AbstractMagentoCommand
protected $_stopOnError = false;

/**
* @var null|\Magento\Framework\App\ProductMetadata
* @var ProductMetadataInterface
*/
protected $productMetadata = null;
private $productMetadata;

/**
* @var DistributionMetadataInterface|null
*/
private $distributionMetadata;

/**
* Dependency injection for metadata interfaces
*/
public function inject(
ProductMetadataInterface $productMetadata
) {
$this->productMetadata = $productMetadata;
if ($productMetadata instanceof DistributionMetadataInterface) {
$this->distributionMetadata = $productMetadata;
}
}

protected function configure()
{
Expand Down Expand Up @@ -142,7 +164,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (empty($commandString)) {
continue;
}
$firstChar = substr($commandString, 0, 1);
$firstChar = $commandString[0];

switch ($firstChar) {
case '#':
Expand Down Expand Up @@ -175,7 +197,7 @@ protected function _initDefines(InputInterface $input)

foreach ($defines as $define) {
if (!strstr($define, '=')) {
throw new \InvalidArgumentException('Invalid define');
throw new InvalidArgumentException('Invalid define');
}
$parts = BinaryString::trimExplodeEmpty('=', $define);
list($variable, $value) = $parts + [1 => null];
Expand All @@ -194,7 +216,7 @@ protected function _getContent($filename)
if ($filename === '-' || empty($filename)) {
$filename = 'php://stdin';
}
$script = @\file_get_contents($filename);
$script = @file_get_contents($filename);

if (!$script) {
throw new RuntimeException('Script file was not found');
Expand Down Expand Up @@ -250,7 +272,7 @@ protected function registerVariable(InputInterface $input, OutputInterface $outp
$question->setMaxAttempts(20);
$question->setValidator(function ($value) {
if (empty($value)) {
throw new \Exception('Please enter a value');
throw new Exception('Please enter a value');
}

return $value;
Expand Down Expand Up @@ -317,10 +339,17 @@ protected function _prepareShellCommand($commandString)
protected function initScriptVars()
{
$rootFolder = $this->getApplication()->getMagentoRootFolder();
$this->getApplication()->initMagento();
if ($rootFolder !== null) {
$this->scriptVars['${magento.root}'] = $rootFolder;
$this->scriptVars['${magento.version}'] = $this->getMagentoVersion();
$this->scriptVars['${magento.edition}'] = $this->getMagentoEdition();
$this->scriptVars['${magento.version}'] = $this->productMetadata ? $this->productMetadata->getVersion() : 'UNKNOWN';
$this->scriptVars['${magento.edition}'] = $this->productMetadata ? $this->productMetadata->getEdition() : 'UNKNOWN';
$this->scriptVars['${magento.distribution}'] = 'UNKNOWN';
$this->scriptVars['${magento.distribution_version}'] = 'UNKNOWN';
if ($this->distributionMetadata) {
$this->scriptVars['${magento.distribution}'] = $this->distributionMetadata->getDistributionName();
$this->scriptVars['${magento.distribution_version}'] = $this->distributionMetadata->getDistributionVersion();
}
}

$this->scriptVars['${php.version}'] = substr(phpversion(), 0, strpos(phpversion(), '-'));
Expand Down Expand Up @@ -358,37 +387,4 @@ protected function _replaceScriptVars($commandString)

return $commandString;
}

/**
* @return string
* @throws \Exception
*/
private function getMagentoVersion()
{
return $this->getProductMetadata()->getVersion();
}

/**
* @return string
* @throws \Exception
*/
private function getMagentoEdition()
{
return $this->getProductMetadata()->getEdition();
}

/**
* @return \Magento\Framework\App\ProductMetadata
* @throws \Exception
*/
private function getProductMetadata()
{
if ($this->productMetadata === null) {
$this->initMagento(); // obtaining the object-manager requires init Magento
$objectManager = $this->getApplication()->getObjectManager();
$this->productMetadata = $objectManager->get('\Magento\Framework\App\ProductMetadata');
}

return $this->productMetadata;
}
}
3 changes: 2 additions & 1 deletion tests/N98/Magento/Command/ScriptCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function testExecute()
// Check pre defined vars
$this->assertDisplayRegExp($input, '~^\Qmagento.root: \E/.+\R$~m');
$this->assertDisplayRegExp($input, '~^\Qmagento.edition: \E(Community|Enterprise)\R$~m');
$this->assertDisplayRegExp($input, '~^\Qmagento.version: \E\d\.\d+\.\d+.*\R$~m');
$this->assertDisplayRegExp($input, '~^magento.version: (\\d\\.\\d+\\.\\d+.*|UNKNOWN)\\R$~m');
$this->assertDisplayRegExp($input, '~^magento.distribution_version: (\\d\\.\\d+\\.\\d+.*|UNKNOWN)\\R$~m');

// Test ENV vars
$this->assertDisplayRegExp($input, '~^\QPath ENV Variable: \E.*\R$~m');
Expand Down
1 change: 1 addition & 0 deletions tests/N98/Magento/Command/_files/test.mr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ${bar}=${foo}.gz
! echo "magento.root: ${magento.root}"
! echo "magento.edition: ${magento.edition}"
! echo "magento.version: ${magento.version}"
! echo "magento_distribution_version: ${magento.distribution_version}"
! echo "php.version: ${php.version}"
! echo "magerun.version: ${magerun.version}"
! echo "Path ENV Variable: ${env.PATH}"
Expand Down