Skip to content

Commit

Permalink
Add Encryptor Util command
Browse files Browse the repository at this point in the history
- Set stable version to to v3.8.0
- Update README
- Refactor after phpcs
  • Loading branch information
ctasca committed Aug 25, 2023
1 parent 5c99b8b commit c95515d
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Api/LocatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ interface LocatorInterface

/**
* File to override working directory when creating files.
*
*
* If it exists, files won't be created in app/code directory but in the one specified in the
* json file.
* json file.
*/
public const PWD_FILENAME = 'dev/mage-bundle/pwd.json';

Expand Down
18 changes: 18 additions & 0 deletions Api/UtilCommandInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Ctasca\MageBundle\Api;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

interface UtilCommandInterface
{
/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
public function execute(InputInterface $input, OutputInterface $output): void;
}
48 changes: 48 additions & 0 deletions Console/Command/EncryptorCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

// phpcs:disable SlevomatCodingStandard.Classes.RequireConstructorPropertyPromotion.RequiredConstructorPropertyPromotion


declare(strict_types=1);

namespace Ctasca\MageBundle\Console\Command;

use Ctasca\MageBundle\Model\Util\Encryptor;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class EncryptorCommand extends Command
{
private Encryptor $encryptor;

/**
* @param \Ctasca\MageBundle\Model\Util\Encryptor $encryptor
*/
public function __construct(Encryptor $encryptor)
{
parent::__construct();
$this->encryptor = $encryptor;
}

/**
* @return void
*/
protected function configure(): void
{
$this->setName('magebundle:util:encryptor')
->setDescription('Encrypts/Decrypts a string using Magento crypt key');

parent::configure();
}

/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output): void
{
$this->encryptor->execute($input, $output);
}
}
3 changes: 0 additions & 3 deletions Model/AbstractLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ abstract class AbstractLocator implements LocatorInterface
protected PwdLocatorFactory $pwdLocatorFactory;
protected string $dirname;
protected string $templateFilename = '';
/**
* @var \Ctasca\MageBundle\Model\Pwd\Locator
*/

/**
* @return string
Expand Down
1 change: 1 addition & 0 deletions Model/Maker/AbstractMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ protected function writeFileFromTemplateChoice(
$file = $this->makeFile($dataProvider, $fileTemplate);

if (empty($filename)) {
//@phpcs:ignore Security.BadFunctions.PregReplace.PregReplaceDyn
$template = preg_replace(self::CUSTOM_TEMPLATE_PATTERN_MATCH, '', $template);
$this->writeFile(
$appCodeLocator,
Expand Down
2 changes: 2 additions & 0 deletions Model/Maker/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function make(InputInterface $input, OutputInterface $output): void
'Is the handler defined as a virtual type in di.xml?'
);
$isHandlerAVirtualType = $this->questionHelper->ask($input, $output, $confirmationQuestion);

if ($isHandlerAVirtualType === false) {
// Logger Handler class name question
$question = $this->questionFactory->create(
Expand All @@ -48,6 +49,7 @@ public function make(InputInterface $input, OutputInterface $output): void
);
$logFilename = $this->questionHelper->ask($input, $output, $question);
}

// Logger class name question
$question = $this->questionFactory->create(
'Enter Logger class name. It can be also a directory. (e.g. Logger or Dummy/Logger)'
Expand Down
4 changes: 2 additions & 2 deletions Model/Pwd/Locator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function locate(): string
try {
$pwdJson = $this->getRead('dev/mage-bundle')
->readFile('pwd.json');
} catch(\Exception) {
} catch (\Exception) {
return '';
}
$pwd = $this->jsonSerializer->unserialize($pwdJson);
Expand Down Expand Up @@ -58,7 +58,7 @@ public function getTemplateFilename(): string
* @param string $templateFilename
* @return \Ctasca\MageBundle\Api\LocatorInterface
*/
public function setTemplateFilename(string $templateFilename): LocatorInterface
public function setTemplateFilename(string $templateFilename): LocatorInterface //@phpcs:ignore
{
return $this;
}
Expand Down
96 changes: 96 additions & 0 deletions Model/Util/Encryptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

// phpcs:disable SlevomatCodingStandard.Classes.RequireConstructorPropertyPromotion.RequiredConstructorPropertyPromotion
// phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification
// phpcs:disable SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
// phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint


declare(strict_types=1);

namespace Ctasca\MageBundle\Model\Util;

use Ctasca\MageBundle\Api\UtilCommandInterface;
use Ctasca\MageBundle\Console\Question\Choice\Factory as QuestionChoiceFactory;
use Ctasca\MageBundle\Console\Question\Factory as QuestionFactory;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Framework\Encryption\Encryptor as MagentoEncryptor;
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Encryptor implements UtilCommandInterface
{
private State $state;
private MagentoEncryptor $encryptor;
private QuestionChoiceFactory $questionChoiceFactory;
private SymfonyQuestionHelper $questionHelper;
private QuestionFactory $questionFactory;

/**
* @param \Magento\Framework\App\State $state
* @param \Magento\Framework\Encryption\Encryptor $encryptor
* @param \Ctasca\MageBundle\Console\Question\Choice\Factory $questionChoiceFactory
* @param \Symfony\Component\Console\Helper\SymfonyQuestionHelper $questionHelper
* @param \Ctasca\MageBundle\Console\Question\Factory $questionFactory
*/
public function __construct(
State $state,
MagentoEncryptor $encryptor,
QuestionChoiceFactory $questionChoiceFactory,
SymfonyQuestionHelper $questionHelper,
QuestionFactory $questionFactory
) {
$this->state = $state;
$this->encryptor = $encryptor;
$this->questionChoiceFactory = $questionChoiceFactory;
$this->questionHelper = $questionHelper;
$this->questionFactory = $questionFactory;
}

/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
public function execute(InputInterface $input, OutputInterface $output): void
{
try {
$this->state->setAreaCode(Area::AREA_ADMINHTML);
$question = $this->questionChoiceFactory->create(
'Choose whether you want to encrypt or decrypt',
$this->getEncryptorChoices()
);
$question->setErrorMessage('Chosen action %s is invalid.');
$action = $this->questionHelper->ask($input, $output, $question);

$question = $this->questionFactory->create(sprintf('Enter string to %s', $action));
$string = $this->questionHelper->ask($input, $output, $question);

if ($action === 'encrypt') {
$output->writeln(
'<info>' . $this->encryptor->encrypt($string) . '</info>'
);
}

if ($action === 'decrypt') {
$output->writeln(
'<info>' . $this->encryptor->decrypt($string) . '</info>'
);
}

$output->writeln('');
} catch (\Exception $e) {
$output->writeln($e->getMessage());
}
}

/**
* @return string[]
*/
private function getEncryptorChoices(): array
{
return ['encrypt', 'decrypt'];
}
}
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,11 @@ bin/magento m:r:c
<p>Do not forget to add the preferences to your di.xml for the repository classes once created. For example:</p>

```xml
<config>
<preference for="Company\Module\Api\Data\$MODEL_NAMEInterface" type="Company\Module\Model\$MODEL_NAME" />
<preference for="Company\Module\Api\$MODEL_NAMERepositoryInterface" type="Company\Module\Model\$MODEL_NAMERepository" />
<preference for="Company\Module\Api\Data\$MODEL_NAMESearchResultInterface" type="Company\Module\Model\$MODEL_NAMESearchResult" />
</config>
```

---
Expand Down Expand Up @@ -312,6 +314,33 @@ bin/magento m:e:c
```
<p>Creates an Exception class in specified Company/Module.</p>

## Utilities commands

```bash
bin/magento magebundle:util:encryptor
```
#### Shortcut
```bash
bin/magento m:u:e
```
<p>Encrypts/Decrypts a string using Magento crypt key</p>

## Setting custom working directory
<p>By default, MageBundle creates files in the <code>app/code</code> directory and in the specified module's namespace</p>
<p>It is possible, to change this behaviour by creating a json file named <code>pwd.json</code> in the <code>$MAGENTO-ROOT/dev/mage-bundle</code> directory and specifying the directory (relative to magento root) where files will be created when executing MageBundle commands.</p>
<p>This can be useful, for example, when developing a module which is not located in <code>app/code</code> directory</p>

### Important:
The directory specified in <code>pwd.json</code> must end with a forward-slash

#### pwd.json example:

```json
{
"pwd" : "packages/my-package/"
}
```


## About template files:
<p>Template files are written in PHP version 8.1.</p>
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"config": {
"sort-packages": true
},
"version": "v3.7.2-RC",
"version": "v3.8.0",
"require": {
"php": ">=7.4"
},
Expand Down
1 change: 1 addition & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<item name="createJsMixinCommand" xsi:type="object">Ctasca\MageBundle\Console\Command\CreateJsMixinCommand</item>
<item name="createRepositoryCommand" xsi:type="object">Ctasca\MageBundle\Console\Command\CreateRepositoryCommand</item>
<item name="createExceptionCommand" xsi:type="object">Ctasca\MageBundle\Console\Command\CreateExceptionCommand</item>
<item name="utilEncryptorCommand" xsi:type="object">Ctasca\MageBundle\Console\Command\EncryptorCommand</item>
</argument>
</arguments>
</type>
Expand Down

0 comments on commit c95515d

Please sign in to comment.