Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[multisite:new] Validated uri name #4039

Merged
merged 1 commit into from
May 16, 2019
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
12 changes: 10 additions & 2 deletions src/Command/Multisite/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Drupal\Console\Command\Multisite;

use Drupal\Console\Core\Command\Command;
use Drupal\Console\Utils\Validator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -25,14 +26,21 @@ class NewCommand extends Command
{
protected $appRoot;

/**
* @var Validator
*/
protected $validator;

/**
* DebugCommand constructor.
*
* @param $appRoot
* @param Validator $validator
*/
public function __construct($appRoot)
public function __construct($appRoot, Validator $validator)
{
$this->appRoot = $appRoot;
$this->validator = $validator;
parent::__construct();
}

Expand Down Expand Up @@ -80,6 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$this->fs = new Filesystem();
$this->directory = $input->getArgument('directory');
$uri = $this->validator->validateUriName($input->getArgument('uri'));

if (!$this->directory) {
$this->getIo()->error($this->trans('commands.multisite.new.errors.subdir-empty'));
Expand Down Expand Up @@ -117,7 +126,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}

$uri = $input->getArgument('uri');
try {
$this->addToSitesFile($uri);
} catch (\Exception $e) {
Expand Down
15 changes: 15 additions & 0 deletions src/Utils/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Validator
const REGEX_CONTROLLER_CLASS_NAME = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+Controller$/';
const REGEX_MACHINE_NAME = '/^[a-z0-9_]+$/';
const REGEX_DEPENDENCY_NAME = '/^[a-z0-9_:]+$/';
const REGEX_URI_NAME = '/^[a-z0-9_.]+$/';
// This REGEX remove spaces between words
const REGEX_REMOVE_SPACES = '/[\\s+]/';
// Max length to 32
Expand Down Expand Up @@ -66,6 +67,20 @@ public function validateClassName($class_name)
}
}

public function validateUriName($uri_name)
{
if (preg_match(self::REGEX_URI_NAME, $uri_name)) {
return $uri_name;
} else {
throw new \InvalidArgumentException(
sprintf(
'Uri name "%s" is invalid, it must starts with a letter, followed by any number of letters, numbers, or underscores.',
$uri_name
)
);
}
}

public function validateBundleTitle($bundle_title)
{
if (!empty($bundle_title)) {
Expand Down
2 changes: 1 addition & 1 deletion uninstall.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ services:
- { name: drupal.command }
console.multisite_new:
class: Drupal\Console\Command\Multisite\NewCommand
arguments: ['@app.root']
arguments: ['@app.root', '@console.validator']
tags:
- { name: drupal.command }
console.multisite_update:
Expand Down