/
BlockApi.php
143 lines (120 loc) · 4.52 KB
/
BlockApi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
/*
* This file is part of the Zikula package.
*
* Copyright Zikula Foundation - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zikula\BlocksModule\Api;
use InvalidArgumentException;
use Zikula\BlocksModule\AbstractBlockHandler;
use Zikula\BlocksModule\Api\ApiInterface\BlockApiInterface;
use Zikula\BlocksModule\Api\ApiInterface\BlockFactoryApiInterface;
use Zikula\BlocksModule\BlockHandlerInterface;
use Zikula\BlocksModule\Collector\BlockCollector;
use Zikula\BlocksModule\Entity\BlockEntity;
use Zikula\BlocksModule\Entity\BlockPositionEntity;
use Zikula\BlocksModule\Entity\RepositoryInterface\BlockPositionRepositoryInterface;
use Zikula\ExtensionsModule\Constant;
use Zikula\ExtensionsModule\Entity\ExtensionEntity;
use Zikula\ExtensionsModule\Entity\RepositoryInterface\ExtensionRepositoryInterface;
/**
* Class BlockApi
*
* This class provides an API for interaction with and management of blocks. The class is mainly used internally
* by Twig-based theme tags in order to 'decorate' a page with the requested blocks.
*/
class BlockApi implements BlockApiInterface
{
public const BLOCK_ACTIVE = 1;
public const BLOCK_INACTIVE = 0;
/**
* @var BlockPositionRepositoryInterface
*/
private $blockPositionRepository;
/**
* @var BlockFactoryApiInterface
*/
private $blockFactory;
/**
* @var ExtensionRepositoryInterface
*/
private $extensionRespository;
/**
* @var BlockCollector;
*/
private $blockCollector;
public function __construct(
BlockPositionRepositoryInterface $blockPositionRepository,
BlockFactoryApiInterface $blockFactoryApi,
ExtensionRepositoryInterface $extensionRepository,
BlockCollector $blockCollector
) {
$this->blockPositionRepository = $blockPositionRepository;
$this->blockFactory = $blockFactoryApi;
$this->extensionRespository = $extensionRepository;
$this->blockCollector = $blockCollector;
}
public function getBlocksByPosition(string $positionName): array
{
if (empty($positionName)) {
throw new InvalidArgumentException('Name must not be empty.');
}
/** @var BlockPositionEntity $position */
$position = $this->blockPositionRepository->findByName($positionName);
$blocks = [];
if (null === $position) {
return $blocks;
}
foreach ($position->getPlacements() as $placement) {
/** @var BlockEntity $block */
$block = $placement->getBlock();
$blocks[$block->getBid()] = $block;
}
return $blocks;
}
public function createInstanceFromBKey(string $bKey): BlockHandlerInterface
{
if (false !== mb_strpos($bKey, ':')) {
@trigger_error('The BKey should not contain the module name with a colon as of Core-3.0.0');
}
return $this->blockFactory->getInstance($bKey);
}
public function getAvailableBlockTypes(ExtensionEntity $moduleEntity = null): array
{
$modulesByName = [];
$modules = isset($moduleEntity) ? [$moduleEntity] : $this->extensionRespository->findBy(['state' => Constant::STATE_ACTIVE]);
/** @var ExtensionEntity $module */
foreach ($modules as $module) {
$modulesByName[$module->getName()] = $module;
}
$foundBlocks = [];
foreach ($this->blockCollector->getBlocks() as $id => $blockInstance) {
/** @var AbstractBlockHandler $blockInstance */
$bundleName = $blockInstance->getBundle()->getName();
if (!array_key_exists($bundleName, $modulesByName)) {
continue;
}
$foundBlocks[$id] = $modulesByName[$bundleName]->getDisplayname() . '/' . $blockInstance->getType();
}
asort($foundBlocks);
return $foundBlocks;
}
public function getModulesContainingBlocks(): array
{
$modules = $this->extensionRespository->findBy(['state' => Constant::STATE_ACTIVE]);
$modulesContainingBlocks = [];
foreach ($modules as $module) {
/** @var ExtensionEntity $module */
$blocks = $this->getAvailableBlockTypes($module);
if (!empty($blocks)) {
$modulesContainingBlocks[$module->getId()] = $module->getName();
}
}
asort($modulesContainingBlocks);
return $modulesContainingBlocks;
}
}