Skip to content

Commit

Permalink
Merge pull request #147 from mitelg/ntr/master/fix-issues
Browse files Browse the repository at this point in the history
Fix model creation; fixes #108
  • Loading branch information
mitelg authored Nov 16, 2018
2 parents c2e623f + 0d0bccf commit a3719b4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class ShopwareInstallVcsCommand extends BaseCommand
{
const MAIN_BRANCH = '5.4';
const MAIN_BRANCH = '5.5';

/**
* {@inheritdoc}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
<?php

namespace Shopware\PluginCreator\Command;

use Shopware\PluginCreator\Services\Generator;
use Shopware\PluginCreator\Services\GeneratorFactory;
use Shopware\PluginCreator\Services\IoAdapter\HardDrive;
use Shopware\PluginCreator\Services\NameGenerator;
use Shopware\PluginCreator\Services\Template;
use Shopware\PluginCreator\Services\TemplateFileProvider\LegacyOptionFileProviderLoader;
use Shopware\PluginCreator\Services\WorkingDirectoryProvider\CurrentOutputDirectoryProvider;
use Shopware\PluginCreator\Services\WorkingDirectoryProvider\LegacyOutputDirectoryProvider;
use Shopware\PluginCreator\Services\WorkingDirectoryProvider\RootDetector\ShopwareRootDetector;
use Shopware\PluginCreator\Struct\Configuration;
use ShopwareCli\Command\BaseCommand;
use ShopwareCli\Config;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -119,11 +113,8 @@ protected function configure()
The <info>%command.name%</info> creates a new plugin.
EOF
);
;
}



public function interact(InputInterface $input, OutputInterface $output)
{
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
Expand All @@ -144,14 +135,16 @@ public function interact(InputInterface $input, OutputInterface $output)

// for backend / api the backendModel is mandatory
if (($input->getOption('haveBackend') || $input->getOption('haveApi')) && empty($backendModel)) {
$question = new Question('<question>Please specify the main model for your backend application:</question> <comment>' . $defaultModel . '</comment>: ');
$question->setValidator($this->validateModel($input));
$question = new Question('<question>Please specify the main model for your backend application:</question> <comment>' . $defaultModel . '</comment>: ', $defaultModel);
$question->setValidator($this->validateModel());
$modelName = $helper->ask($input, $output, $question);
$input->setOption('backendModel', $modelName);
}

// a backend implicitly sets "haveModel" to true, if the backend model is not a default model
if ($input->getOption('haveBackend') && strpos($input->getOption('backendModel'), 'Shopware\Models') === false) {
if ($input->getOption('haveBackend')
&& strpos($input->getOption('backendModel'), 'Shopware\Models') === false
) {
$input->setOption('haveModels', true);
}
}
Expand All @@ -163,7 +156,18 @@ public function interact(InputInterface $input, OutputInterface $output)
*/
public function normalizeBooleanFields(InputInterface $input)
{
foreach (['haveBackend', 'haveFrontend', 'haveModels', 'haveCommands', 'haveWidget', 'haveApi', 'haveFilter', self::LEGACY_OPTION] as $key) {
$inputOptions = [
'haveBackend',
'haveFrontend',
'haveModels',
'haveCommands',
'haveWidget',
'haveApi',
'haveFilter',
self::LEGACY_OPTION
];

foreach ($inputOptions as $key) {
switch (strtolower($input->getOption($key))) {
case 'false':
case '0':
Expand Down Expand Up @@ -191,8 +195,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$configuration = $this->getConfigurationObject($input);
$configuration->pluginConfig = $this->getConfig()->offsetGet('PluginConfig');

$generatorFactory = new GeneratorFactory();
$generator = $generatorFactory->create($configuration);
$generator = (new GeneratorFactory())->create($configuration);

$generator->run();
}
Expand All @@ -209,8 +212,6 @@ protected function validateName($name)
if (count($parts) <= 1) {
throw new \InvalidArgumentException('Name must be in CamelCase and have at least two components. Don\'t forget you developer-prefix');
}

return $name;
}

/**
Expand Down Expand Up @@ -243,13 +244,11 @@ public function validateNamespace($input)
/**
* Check the entered model (check might be somewhat more sufisticated)
*
* @param $input
* @throws \InvalidArgumentException
* @return $input
*/
public function validateModel($input)
public function validateModel()
{
return function () use ($input) {
return function ($input) {
if (empty($input)) {
throw new \InvalidArgumentException('You need to enter a model name like »Shopware\Models\Article\Article«');
}
Expand Down
12 changes: 0 additions & 12 deletions src/Extensions/Shopware/PluginCreator/Services/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,7 @@
namespace Shopware\PluginCreator\Services;

use Shopware\PluginCreator\Services\IoAdapter\IoAdapter;
use Shopware\PluginCreator\Services\TemplateFileProvider\ApiFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\BackendControllerFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\BackendFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\CommandFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\ControllerPathFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\DefaultFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\FileProviderInterface;
use Shopware\PluginCreator\Services\TemplateFileProvider\FileProviderLoaderInterface;
use Shopware\PluginCreator\Services\TemplateFileProvider\FilterFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\FrontendFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\LegacyOptionFileProviderLoader;
use Shopware\PluginCreator\Services\TemplateFileProvider\ModelFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\WidgetFileProvider;
use Shopware\PluginCreator\Services\WorkingDirectoryProvider\OutputDirectoryProviderInterface;
use Shopware\PluginCreator\Struct\Configuration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
*/
class Shopware_Controllers_Backend_<?= $configuration->name; ?> extends Shopware_Controllers_Backend_Application
{
protected $model = '\<?= $configuration->name; ?>\Models\<?= $configuration->backendModel; ?>';
protected $model = \<?= $configuration->backendModel; ?>::class;
protected $alias = '<?= $names->under_score_model; ?>';
<?php } else { ?>
class Shopware_Controllers_Backend_<?= $configuration->name; ?> extends Shopware_Controllers_Backend_ExtJs
{
<?php } ?>


<?php if ($configuration->hasWidget) { ?>
/**
* Loads data for the backend widget
Expand Down

6 comments on commit a3719b4

@ray-magini
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mitelg In my understanding $model in 'src/Extensions/Shopware/PluginCreator/template/current/Controllers/Backend.tpl' requires an absolute path as no namespace is defined?

@mitelg
Copy link
Contributor Author

@mitelg mitelg commented on a3719b4 Dec 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ray-magini the backendModel should be a class name including the namespace.
does this not work for you?

@ray-magini
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mitelg Not sure if we are talking about the same thing. I am referring to the $model variable, in my understanding this needs to be an absolute path as it was before (independent of the notation) so:
protected $model = '\<?= $configuration->name; ?>\Models\<?= $configuration->backendModel; ?>';
or
protected $model = \<?= $configuration->name; ?>\Models\<?= $configuration->backendModel; ?>::class;

@ray-magini
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mitelg Probably I got your comment now. In my tests the $configuration->backendModel is not absolute when I use the command:
bin/sw plugin:create --haveBackend --backendModel=MyPlugin --haveApi --haveModels MyPlugin

@mitelg
Copy link
Contributor Author

@mitelg mitelg commented on a3719b4 Dec 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about something like this? --backendModel=MyPlugin\Models\MyPlugin

@mitelg
Copy link
Contributor Author

@mitelg mitelg commented on a3719b4 Dec 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you don't set the Parameter --backendModel the program will suggest you a model name

Please sign in to comment.