|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Cli\Command\AbstractSimpleImportCommand |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import-cli-simple |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Cli\Command; |
|
22
|
|
|
|
|
23
|
|
|
use Symfony\Component\Console\Command\Command; |
|
24
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
25
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
26
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
27
|
|
|
use TechDivision\Import\ConfigurationInterface; |
|
28
|
|
|
use TechDivision\Import\Configuration\Jms\Configuration; |
|
29
|
|
|
use TechDivision\Import\Cli\Utils\DependencyInjectionKeys; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Abstract command implementation for simple import commands (not using Importer class). |
|
33
|
|
|
* |
|
34
|
|
|
* @author Tim Wagner <[email protected]> |
|
35
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
36
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
37
|
|
|
* @link https://github.com/techdivision/import-cli-simple |
|
38
|
|
|
* @link http://www.techdivision.com |
|
39
|
|
|
*/ |
|
40
|
|
|
abstract class AbstractSimpleImportCommand extends Command |
|
41
|
|
|
{ |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Configures the current command. |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
* @see \Symfony\Component\Console\Command\Command::configure() |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function configure() |
|
50
|
|
|
{ |
|
51
|
|
|
|
|
52
|
|
|
// configure the command |
|
53
|
|
|
$this->addOption(InputOptionKeys::PID_FILENAME, null, InputOption::VALUE_REQUIRED, 'The explicit PID filename to use', sprintf('%s/%s', sys_get_temp_dir(), Configuration::PID_FILENAME)) |
|
54
|
|
|
->addOption(InputOptionKeys::INSTALLATION_DIR, null, InputOption::VALUE_REQUIRED, 'The Magento installation directory to which the files has to be imported', $installationDir = getcwd()) |
|
55
|
|
|
->addOption(InputOptionKeys::SOURCE_DIR, null, InputOption::VALUE_REQUIRED, 'The directory that has to be watched for new files', sprintf('%s/var/importexport', $installationDir)) |
|
56
|
|
|
->addOption(InputOptionKeys::MAGENTO_EDITION, null, InputOption::VALUE_REQUIRED, 'The Magento edition to be used, either one of "CE" or "EE"', 'CE') |
|
57
|
|
|
->addOption(InputOptionKeys::CONFIGURATION, null, InputOption::VALUE_REQUIRED, 'Specify the pathname to the configuration file to use') |
|
58
|
|
|
->addOption(InputOptionKeys::LOG_LEVEL, null, InputOption::VALUE_REQUIRED, 'The log level to use'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Return's the container instance. |
|
63
|
|
|
* |
|
64
|
|
|
* @return \Symfony\Component\DependencyInjection\ContainerInterface The container instance |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function getContainer() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->getApplication()->getContainer(); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Executes the current command. |
|
73
|
|
|
* |
|
74
|
|
|
* This method is not abstract because you can use this class |
|
75
|
|
|
* as a concrete class. In this case, instead of defining the |
|
76
|
|
|
* execute() method, you set the code to execute by passing |
|
77
|
|
|
* a Closure to the setCode() method. |
|
78
|
|
|
* |
|
79
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
|
80
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
|
81
|
|
|
* |
|
82
|
|
|
* @return null|int null or 0 if everything went fine, or an error code |
|
83
|
|
|
* @throws \LogicException When this abstract method is not implemented |
|
84
|
|
|
* @see \Symfony\Component\Console\Command\Command::execute() |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
87
|
|
|
{ |
|
88
|
|
|
|
|
89
|
|
|
// try to load the configuration file |
|
90
|
|
|
$configuration = $this->getContainer()->get(DependencyInjectionKeys::CONFIGURATION_SIMPLE); |
|
91
|
|
|
|
|
92
|
|
|
// finally execute the simple command |
|
93
|
|
|
$this->executeSimpleCommand($configuration, $input, $output); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Finally executes the simple command. |
|
98
|
|
|
* |
|
99
|
|
|
* @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance |
|
100
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
|
101
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
|
102
|
|
|
* |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
abstract protected function executeSimpleCommand( |
|
106
|
|
|
ConfigurationInterface $configuration, |
|
107
|
|
|
InputInterface $input, |
|
108
|
|
|
OutputInterface $output |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: