Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FixtureModel.php
Go to the documentation of this file.
1 <?php
10 namespace Magento\Setup\Fixtures;
11 
14 use Symfony\Component\Console\Input\ArrayInput;
15 use Symfony\Component\Console\Output\OutputInterface;
16 
21 {
25  const AREA_CODE = 'adminhtml';
26 
30  const FIXTURE_PATTERN = '?*Fixture.php';
31 
37  protected $application;
38 
42  protected $objectManager;
43 
49  protected $fixtures = [];
50 
56  private $fixturesByNames = [];
57 
64  protected $paramLabels = [];
65 
69  protected $initArguments;
70 
74  private $config;
75 
82  public function __construct(IndexerReindexCommand $reindexCommand, $initArguments = [])
83  {
84  $this->initArguments = $initArguments;
85  $this->reindexCommand = $reindexCommand;
86  }
87 
94  public function reindex(OutputInterface $output)
95  {
96  $input = new ArrayInput([]);
97  $this->reindexCommand->run($input, $output);
98  }
99 
106  public function loadFixtures()
107  {
108  $files = glob(__DIR__ . DIRECTORY_SEPARATOR . self::FIXTURE_PATTERN, GLOB_NOSORT);
109 
110  foreach ($files as $file) {
111  $file = basename($file, '.php');
113  $type = 'Magento\Setup\Fixtures' . '\\' . $file;
114  $fixture = $this->getObjectManager()->create(
115  $type,
116  [
117  'fixtureModel' => $this,
118  ]
119  );
120 
121  if (isset($this->fixtures[$fixture->getPriority()])) {
122  throw new \InvalidArgumentException(
123  sprintf('Duplicate priority %d in fixture %s', $fixture->getPriority(), $type)
124  );
125  }
126 
127  if ($fixture->getPriority() >= 0) {
128  $this->fixtures[$fixture->getPriority()] = $fixture;
129  }
130 
131  $this->fixturesByNames[get_class($fixture)] = $fixture;
132  }
133 
134  ksort($this->fixtures);
135  return $this;
136  }
137 
144  public function getParamLabels()
145  {
146  return $this->paramLabels;
147  }
148 
154  public function getFixtures()
155  {
156  return $this->fixtures;
157  }
158 
165  public function getFixtureByName($name)
166  {
167  if (!array_key_exists($name, $this->fixturesByNames)) {
168  throw new Exception('Wrong fixture name');
169  }
170 
171  return $this->fixturesByNames[$name];
172  }
173 
179  public function getObjectManager()
180  {
181  if (!$this->objectManager) {
183  BP,
184  $this->initArguments
185  );
186  $this->objectManager = $objectManagerFactory->create($this->initArguments);
187  $this->objectManager->get(\Magento\Framework\App\State::class)->setAreaCode(self::AREA_CODE);
188  }
189 
190  return $this->objectManager;
191  }
192 
199  public function initObjectManager($area = self::AREA_CODE)
200  {
201  $objectManger = $this->getObjectManager();
202  $configuration = $objectManger
203  ->get(\Magento\Framework\ObjectManager\ConfigLoaderInterface::class)
204  ->load($area);
205  $objectManger->configure($configuration);
206 
207  $diConfiguration = $this->getValue('di');
208  if (file_exists($diConfiguration)) {
209  $dom = new \DOMDocument();
210  $dom->load($diConfiguration);
211 
212  $objectManger->configure(
213  $objectManger
214  ->get(\Magento\Framework\ObjectManager\Config\Mapper\Dom::class)
215  ->convert($dom)
216  );
217  }
218 
219  $objectManger->get(\Magento\Framework\Config\ScopeInterface::class)
220  ->setCurrentScope($area);
221  return $this;
222  }
223 
230  public function resetObjectManager()
231  {
232  return $this;
233  }
234 
238  private function getConfig()
239  {
240  if (null === $this->config) {
241  $this->config = $this->getObjectManager()->get(FixtureConfig::class);
242  }
243 
244  return $this->config;
245  }
246 
255  public function loadConfig($filename)
256  {
257  return $this->getConfig()->loadConfig($filename);
258  }
259 
268  public function getValue($key, $default = null)
269  {
270  return $this->getConfig()->getValue($key, $default);
271  }
272 }
$configuration
Definition: index.php:33
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
__construct(IndexerReindexCommand $reindexCommand, $initArguments=[])
$type
Definition: item.phtml:13
reindex(OutputInterface $output)
initObjectManager($area=self::AREA_CODE)
$objectManagerFactory
static createObjectManagerFactory($rootDir, array $initParams)
Definition: Bootstrap.php:149
const BP
Definition: autoload.php:14
foreach($appDirs as $dir) $files
if(!isset($_GET['name'])) $name
Definition: log.php:14