-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Create store view using the command line #29408
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
Open
0m3r
wants to merge
24
commits into
magento:2.4-develop
Choose a base branch
from
0m3r:feature-create-store-view-command
base: 2.4-develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
322cb25
Added cli command to create store view
0m3r 10352e9
Sterilized name and code arguments
0m3r 1e21918
Merge branch '2.4-develop' into feature-create-store-view-command
VladimirZaets 7949e0c
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r 28161a4
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r 050d238
Add StoreCreateManagment model
0m3r 86c4b55
Applied some of @sivaschenko's suggestions
0m3r 5d14696
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r ed4a1fe
Prevent integration test failing (Table store_website doesn\'t exist)
0m3r fab0237
Merge branch 'feature-create-store-view-command' of github.com:0m3r/m…
0m3r d2e1f40
Add integration test for CreateStore
0m3r 5e8ec78
Add GetDefaultStoreGroupTest
0m3r 024fe1d
Applied some of @sivaschenko's suggestions
0m3r 0d528ea
CreateStore::execute accept a StoreInterface instead of array
0m3r 5e87d6e
Fixed PHPdoc param name typo
0m3r 1a8e31c
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r 4c5188f
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r b333b5b
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r 37f731d
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r ff6ca5f
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r 25e08f3
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r 9b39023
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r 341a347
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r f70a5d0
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
app/code/Magento/Store/Console/Command/StoreCreateCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Store\Console\Command; | ||
|
||
use Symfony\Component\Console\Helper\Table; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
/** | ||
* Class StoreCreateCommand | ||
* Command for create a new the configured store | ||
*/ | ||
class StoreCreateCommand extends Command | ||
{ | ||
const INPUT_ARGUMENT_NAME = 'name'; | ||
const INPUT_ARGUMENT_CODE = 'code'; | ||
const INPUT_ARGUMENT_IS_ACTIVE = 'is_active'; | ||
const INPUT_ARGUMENT_SORT_ORDER = 'sort_order'; | ||
|
||
const INPUT_OPTION_GROUP = 'group_id'; | ||
|
||
/** | ||
* | ||
* @var \Magento\Store\Model\CreateStore | ||
*/ | ||
private $createStore; | ||
|
||
/** | ||
* | ||
* @var \Magento\Store\Model\GetDefaultStoreGroup | ||
*/ | ||
private $getDefaultStoreGroup; | ||
|
||
/** | ||
* @var \Magento\Framework\Filter\FilterManager | ||
*/ | ||
private $filterManager; | ||
|
||
/** | ||
* | ||
* @param \Magento\Store\Model\CreateStore $createStore | ||
* @param \Magento\Store\Model\GetDefaultStoreGroup $getDefaultStoreGroup | ||
* @param \Magento\Framework\Filter\FilterManager $filterManager | ||
* @param string $name | ||
*/ | ||
public function __construct( | ||
\Magento\Store\Model\CreateStore $createStore, | ||
\Magento\Store\Model\GetDefaultStoreGroup $getDefaultStoreGroup, | ||
\Magento\Framework\Filter\FilterManager $filterManager, | ||
$name = null | ||
) { | ||
$this->createStore = $createStore; | ||
$this->getDefaultStoreGroup = $getDefaultStoreGroup; | ||
$this->filterManager = $filterManager; | ||
parent::__construct($name); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this->setName('store:create') | ||
->setDescription('Create new store view (see list: bin/magento store:list)') | ||
->addArgument(self::INPUT_ARGUMENT_NAME, InputArgument::REQUIRED, 'Put the store view name you want to create') | ||
->addArgument(self::INPUT_ARGUMENT_CODE, InputArgument::REQUIRED, 'Put the store view code') | ||
->addArgument( | ||
self::INPUT_ARGUMENT_IS_ACTIVE, | ||
InputArgument::OPTIONAL, | ||
'Status (enable/disable)', | ||
true | ||
) | ||
->addArgument( | ||
self::INPUT_ARGUMENT_SORT_ORDER, | ||
InputArgument::OPTIONAL, | ||
'Sort Order', | ||
0 | ||
) | ||
; | ||
|
||
$this->addOption( | ||
self::INPUT_OPTION_GROUP, | ||
'g', | ||
InputOption::VALUE_OPTIONAL, | ||
'Group ID (php bin/magento store:list).' | ||
); | ||
|
||
parent::configure(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
try { | ||
$defaultGroupId = $input->getOption(self::INPUT_OPTION_GROUP); | ||
if ($defaultGroupId === null) { | ||
/** @var \Magento\Store\Api\Data\GroupInterface $defaultGroup */ | ||
$defaultGroup = $this->getDefaultStoreGroup->execute(); | ||
$defaultGroupId = $defaultGroup->getId(); | ||
} | ||
$defaultGroupId = (int) $defaultGroupId; | ||
$data = [ | ||
self::INPUT_OPTION_GROUP => (int) $input->getOption( | ||
self::INPUT_OPTION_GROUP | ||
), | ||
self::INPUT_ARGUMENT_NAME => (string) $input->getArgument( | ||
self::INPUT_ARGUMENT_NAME | ||
), | ||
self::INPUT_ARGUMENT_CODE => (string) $input->getArgument( | ||
self::INPUT_ARGUMENT_CODE | ||
), | ||
self::INPUT_ARGUMENT_IS_ACTIVE => (bool) $input->getArgument( | ||
self::INPUT_ARGUMENT_IS_ACTIVE | ||
), | ||
self::INPUT_ARGUMENT_SORT_ORDER => (int) $input->getArgument( | ||
self::INPUT_ARGUMENT_SORT_ORDER | ||
) | ||
]; | ||
$data[self::INPUT_ARGUMENT_NAME] = $this->filterManager->removeTags( | ||
$data[self::INPUT_ARGUMENT_NAME] | ||
); | ||
$data[self::INPUT_ARGUMENT_CODE] = $this->filterManager->removeTags( | ||
$data[self::INPUT_ARGUMENT_CODE] | ||
); | ||
|
||
$this->createStore->create($data); | ||
|
||
$io->success('You created the store view.'); | ||
|
||
return \Magento\Framework\Console\Cli::RETURN_SUCCESS; | ||
} catch (\Exception $e) { | ||
$io->error($e->getMessage()); | ||
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { | ||
$io->comment($e->getTraceAsString()); | ||
} | ||
|
||
return \Magento\Framework\Console\Cli::RETURN_FAILURE; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Store\Model; | ||
|
||
use Magento\Store\Api\Data\StoreInterface; | ||
|
||
/** | ||
* Class CreateStore | ||
* Model for create a new store view | ||
*/ | ||
class CreateStore | ||
{ | ||
/** | ||
* @var \Magento\Store\Api\Data\GroupInterfaceFactory | ||
*/ | ||
private $groupFactory; | ||
|
||
/** | ||
* @var \Magento\Store\Api\Data\StoreInterfaceFactory | ||
*/ | ||
private $storeFactory; | ||
|
||
/** | ||
* | ||
* @param \Magento\Store\Api\Data\GroupInterfaceFactory $groupFactory | ||
* @param \Magento\Store\Api\Data\StoreInterfaceFactory $storeFactory | ||
*/ | ||
public function __construct( | ||
\Magento\Store\Api\Data\GroupInterfaceFactory $groupFactory, | ||
\Magento\Store\Api\Data\StoreInterfaceFactory $storeFactory | ||
) { | ||
$this->groupFactory = $groupFactory; | ||
$this->storeFactory = $storeFactory; | ||
} | ||
|
||
/** | ||
* | ||
* @param array $data | ||
* @return \Magento\Store\Api\Data\StoreInterface | ||
*/ | ||
public function execute(array $data): StoreInterface | ||
{ | ||
/** @var \Magento\Store\Model\Storev $storeModel */ | ||
$storeModel = $this->storeFactory->create(['data' => $data]); | ||
|
||
/** @var \Magento\Store\Model\Group $groupModel */ | ||
$groupModel = $this->groupFactory->create() | ||
->load($storeModel->getGroupId()); | ||
|
||
$storeModel->setWebsiteId($groupModel->getWebsiteId()); | ||
$storeModel->save(); | ||
|
||
return $storeModel; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Store\Model; | ||
|
||
use Magento\Store\Api\Data\GroupInterface; | ||
|
||
/** | ||
* Class GetDefaultStoreGroup | ||
* Get default (first) store group; | ||
*/ | ||
class GetDefaultStoreGroup | ||
{ | ||
/** | ||
* @var \Magento\Store\Model\WebsiteFactory | ||
*/ | ||
private $websiteFactory; | ||
|
||
/** | ||
* @var \Magento\Store\Api\Data\GroupInterfaceFactory | ||
*/ | ||
private $groupFactory; | ||
|
||
/** | ||
* | ||
* @param \Magento\Store\Api\Data\WebsiteInterfaceFactory $websiteFactory | ||
* @param \Magento\Store\Api\Data\GroupInterfaceFactory $groupFactory | ||
*/ | ||
public function __construct( | ||
\Magento\Store\Api\Data\WebsiteInterfaceFactory $websiteFactory, | ||
\Magento\Store\Api\Data\GroupInterfaceFactory $groupFactory | ||
) { | ||
$this->websiteFactory = $websiteFactory; | ||
$this->groupFactory = $groupFactory; | ||
} | ||
|
||
/** | ||
* Get default group | ||
* | ||
* @return GroupInterface | ||
*/ | ||
public function execute() : GroupInterface | ||
{ | ||
$groups = $this->getAllStoreGroups(); | ||
|
||
return current($groups); | ||
} | ||
|
||
|
||
/** | ||
* Retrieve list of store groups | ||
* | ||
* @return array | ||
*/ | ||
private function getAllStoreGroups() : array | ||
{ | ||
$websites = $this->websiteFactory->create()->getCollection(); | ||
$allgroups = $this->groupFactory->create()->getCollection(); | ||
$groups = []; | ||
foreach ($websites as $website) { | ||
foreach ($allgroups as $group) { | ||
if ($group->getWebsiteId() == $website->getId()) { | ||
$groups[$group->getWebsiteId() . '-' . $group->getId()] = $group; | ||
} | ||
} | ||
} | ||
|
||
return $groups; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
dev/tests/integration/testsuite/Magento/Store/Model/CreateStoreTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Store\Model; | ||
|
||
class CreateStoreTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @var \Magento\Framework\ObjectManagerInterface | ||
*/ | ||
protected $objectManager; | ||
|
||
/** | ||
* @var \Magento\Store\Model\CreateStore | ||
*/ | ||
protected $createStoreModel; | ||
|
||
/** | ||
* @var \Magento\Store\Model\Website | ||
*/ | ||
protected $websiteModel; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); | ||
|
||
$this->createStoreModel = $this->objectManager->get( | ||
\Magento\Store\Model\CreateStore::class | ||
); | ||
|
||
$this->websiteModel = $this->objectManager->create( | ||
\Magento\Store\Model\Website::class | ||
); | ||
$this->websiteModel->load(1); | ||
} | ||
|
||
/** | ||
* @param $data | ||
* @dataProvider loadCreateDataProvider | ||
*/ | ||
public function testExecute($data) | ||
{ | ||
$defaultGroupId = $this->websiteModel->getDefaultGroupId(); | ||
$data['group_id'] = $defaultGroupId; | ||
|
||
$store = $this->createStoreModel->execute($data); | ||
$this->assertInstanceOf(\Magento\Store\Api\Data\StoreInterface::class, $store); | ||
$this->assertNotNull($store->getId(), 'Store was not saved correctly'); | ||
$this->assertEquals($data['code'], $store->getCode()); | ||
|
||
/** @var \Magento\Framework\Registry $registry */ | ||
$registry = $this->objectManager->get(\Magento\Framework\Registry::class); | ||
|
||
$registry->unregister('isSecureArea'); | ||
$registry->register('isSecureArea', true); | ||
0m3r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$store->delete(); | ||
|
||
$registry->unregister('isSecureArea'); | ||
$registry->register('isSecureArea', false); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function loadCreateDataProvider() | ||
{ | ||
return [[ | ||
[ | ||
'name' => 'code', | ||
'code' => 'code', | ||
'is_active' => 0, | ||
'sort_order' => 10, | ||
] | ||
]]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.