1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* WellCommerce Open-Source E-Commerce Platform |
4
|
|
|
* |
5
|
|
|
* This file is part of the WellCommerce package. |
6
|
|
|
* |
7
|
|
|
* (c) Adam Piotrowski <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, |
10
|
|
|
* please view the LICENSE file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WellCommerce\Bundle\AppBundle\Command\Admin; |
14
|
|
|
|
15
|
|
|
use Doctrine\ORM\EntityManager; |
16
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
17
|
|
|
use Symfony\Component\Config\Util\XmlUtils; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
21
|
|
|
use Symfony\Component\Finder\Finder; |
22
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
23
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
24
|
|
|
use WellCommerce\Bundle\AppBundle\Entity\AdminMenu; |
25
|
|
|
use WellCommerce\Bundle\CoreBundle\Doctrine\Repository\RepositoryInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class RefreshMenuCommand |
29
|
|
|
* |
30
|
|
|
* @author Adam Piotrowski <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
final class RefreshMenuCommand extends ContainerAwareCommand |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var EntityManager |
36
|
|
|
*/ |
37
|
|
|
private $em; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var RepositoryInterface |
41
|
|
|
*/ |
42
|
|
|
private $repository; |
43
|
|
|
|
44
|
|
|
protected function configure() |
45
|
|
|
{ |
46
|
|
|
$this->setDescription('Refreshes the admin menu'); |
47
|
|
|
$this->setName('wellcommerce:admin:menu-refresh'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
51
|
|
|
{ |
52
|
|
|
$this->em = $this->getContainer()->get('doctrine.helper')->getEntityManager(); |
53
|
|
|
$this->repository = $this->getContainer()->get('admin_menu.repository'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
57
|
|
|
{ |
58
|
|
|
$files = $this->locateFiles(); |
59
|
|
|
|
60
|
|
|
foreach ($files as $file) { |
|
|
|
|
61
|
|
|
$output->writeln(sprintf('<info>Reimporting %s</info>', $file->getRealPath())); |
62
|
|
|
$this->importFile($file); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function importFile(SplFileInfo $file) |
67
|
|
|
{ |
68
|
|
|
$xml = $this->parseFile($file); |
69
|
|
|
foreach ($xml->documentElement->getElementsByTagName('item') as $item) { |
70
|
|
|
$dom = simplexml_import_dom($item); |
71
|
|
|
$this->addMenuItem($dom); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function addMenuItem(\SimpleXMLElement $xml) |
76
|
|
|
{ |
77
|
|
|
$item = $this->repository->findOneBy(['identifier' => (string)$xml->identifier]); |
78
|
|
|
$parent = $this->repository->findOneBy(['identifier' => (string)$xml->parent]); |
79
|
|
|
|
80
|
|
|
if (!$item instanceof AdminMenu) { |
81
|
|
|
$item = new AdminMenu(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$item->setCssClass((string)$xml->css_class); |
85
|
|
|
$item->setIdentifier((string)$xml->identifier); |
86
|
|
|
$item->setName((string)$xml->name); |
87
|
|
|
$item->setRouteName((string)$xml->route_name); |
88
|
|
|
$item->setHierarchy((int)$xml->hierarchy); |
89
|
|
|
$item->setParent($parent); |
90
|
|
|
|
91
|
|
|
$this->em->persist($item); |
92
|
|
|
$this->em->flush(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return array|SplFileInfo |
97
|
|
|
*/ |
98
|
|
|
private function locateFiles(): array |
99
|
|
|
{ |
100
|
|
|
$filesystem = new Filesystem(); |
101
|
|
|
$files = []; |
102
|
|
|
|
103
|
|
|
foreach ($this->getKernel()->getBundles() as $bundle) { |
104
|
|
|
$directory = $bundle->getPath() . '/Resources/config/admin_menu'; |
105
|
|
|
if (false === $filesystem->exists($directory)) { |
106
|
|
|
continue; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$finder = new Finder(); |
110
|
|
|
|
111
|
|
|
/** @var SplFileInfo $file */ |
112
|
|
|
foreach ($finder->files()->name('*.xml')->in($directory) as $file) { |
113
|
|
|
$files[] = $file; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $files; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function parseFile(SplFileInfo $file): \DOMDocument |
121
|
|
|
{ |
122
|
|
|
return XmlUtils::loadFile($file->getRealPath()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function getKernel(): KernelInterface |
126
|
|
|
{ |
127
|
|
|
return $this->getContainer()->get('kernel'); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.