Skip to content

Commit 43d9c19

Browse files
Add LazyWidgetManager to replace ineffective WidgetManager.
Changed the registration of widgets - now is used improved `LazyWidgetManager` and all widgets extend `BaseLazyWidget` class. remp/crm#2075
1 parent 543f7b0 commit 43d9c19

File tree

15 files changed

+582
-124
lines changed

15 files changed

+582
-124
lines changed

src/Components/Widgets/SimpleWidget/SimpleWidget.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace Crm\ApplicationModule\Components;
44

5-
use Crm\ApplicationModule\Widget\BaseWidget;
5+
use Crm\ApplicationModule\Widget\BaseLazyWidget;
66

77
/**
88
* Widget used for rendering other widgets in groups.
99
*
1010
* @package Crm\ApplicationModule\Components
1111
*/
12-
class SimpleWidget extends BaseWidget
12+
class SimpleWidget extends BaseLazyWidget
1313
{
1414
private $templateName = 'simple_widget.latte';
1515

src/Components/Widgets/SingleStatWidget/SingleStatWidget.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,27 @@
22

33
namespace Crm\ApplicationModule\Components;
44

5-
use Crm\ApplicationModule\Widget\BaseWidget;
6-
use Crm\ApplicationModule\Widget\WidgetManager;
5+
use Crm\ApplicationModule\Widget\BaseLazyWidget;
6+
use Crm\ApplicationModule\Widget\LazyWidgetManager;
77
use Nette\Localization\Translator;
88

99
/**
1010
* Widget used for rendering simple single stat widgets in groups.
1111
*
1212
* @package Crm\ApplicationModule\Components
1313
*/
14-
class SingleStatWidget extends BaseWidget
14+
class SingleStatWidget extends BaseLazyWidget
1515
{
1616
private $templateName = 'single_stat_widget.latte';
1717

1818
private $translator;
1919

20-
public function __construct(WidgetManager $widgetManager, Translator $translator)
21-
{
22-
parent::__construct($widgetManager);
20+
public function __construct(
21+
Translator $translator,
22+
LazyWidgetManager $lazyWidgetManager
23+
) {
24+
parent::__construct($lazyWidgetManager);
25+
2326
$this->translator = $translator;
2427
}
2528

src/CrmModule.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Crm\ApplicationModule\Event\EventsStorage;
1414
use Crm\ApplicationModule\Menu\MenuContainerInterface;
1515
use Crm\ApplicationModule\User\UserDataRegistrator;
16+
use Crm\ApplicationModule\Widget\LazyWidgetManagerInterface;
1617
use Crm\ApplicationModule\Widget\WidgetManagerInterface;
1718
use League\Event\Emitter;
1819
use Nette\Application\Routers\RouteList;
@@ -58,6 +59,9 @@ public function registerEventHandlers(Emitter $emitter)
5859
// nothing
5960
}
6061

62+
/**
63+
* @deprecated use registerLazyWidgets() instead
64+
*/
6165
public function registerWidgets(WidgetManagerInterface $widgetManager)
6266
{
6367
// nothing
@@ -142,4 +146,9 @@ public function registerAssets(AssetsManager $assetsManager)
142146
{
143147
// nothing
144148
}
149+
150+
public function registerLazyWidgets(LazyWidgetManagerInterface $lazyWidgetManager)
151+
{
152+
// nothing
153+
}
145154
}

src/Models/ApplicationManager.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Crm\ApplicationModule\Event\EventsStorage;
1313
use Crm\ApplicationModule\Menu\MenuContainer;
1414
use Crm\ApplicationModule\User\UserDataRegistrator;
15+
use Crm\ApplicationModule\Widget\LazyWidgetManager;
1516
use Crm\ApplicationModule\Widget\WidgetManager;
1617
use League\Event\Emitter;
1718
use Tomaj\Hermes\Dispatcher;
@@ -58,6 +59,8 @@ class ApplicationManager
5859

5960
private $assetsManager;
6061

62+
private LazyWidgetManager $lazyWidgetManager;
63+
6164
public function __construct(
6265
Emitter $emitter,
6366
ModuleManager $moduleManager,
@@ -74,7 +77,8 @@ public function __construct(
7477
AccessManager $accessManager,
7578
AssetsManager $assetsManager,
7679
DataProviderManager $dataProviderManager,
77-
EventsStorage $eventsStorage
80+
EventsStorage $eventsStorage,
81+
LazyWidgetManager $lazyWidgetManager
7882
) {
7983
$this->widgetManager = $widgetManager;
8084
$this->emitter = $emitter;
@@ -93,6 +97,7 @@ public function __construct(
9397
$this->eventsStorage = $eventsStorage;
9498
$this->scenariosCriteriaStorage = $scenariosCriteriaStorage;
9599
$this->assetsManager = $assetsManager;
100+
$this->lazyWidgetManager = $lazyWidgetManager;
96101
}
97102

98103
public function registerEventHandlers()
@@ -134,13 +139,23 @@ private function loadFrontendMenu()
134139
}
135140
}
136141

142+
/**
143+
* @deprecated use registerLazyWidget() instead
144+
*/
137145
public function registerWidgets()
138146
{
139147
foreach ($this->moduleManager->getModules() as $module) {
140148
$module->registerWidgets($this->widgetManager);
141149
}
142150
}
143151

152+
public function registerLazyWidget()
153+
{
154+
foreach ($this->moduleManager->getModules() as $module) {
155+
$module->registerLazyWidgets($this->lazyWidgetManager);
156+
}
157+
}
158+
144159
public function registerCommands()
145160
{
146161
foreach ($this->moduleManager->getModules() as $module) {
@@ -256,6 +271,7 @@ public function initialize()
256271
} elseif (!Request::isApi()) {
257272
$this->registerWidgets();
258273
$this->registerLayouts();
274+
$this->registerLazyWidget();
259275
}
260276

261277
$this->registerEventHandlers();

src/Models/ApplicationModuleInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Crm\ApplicationModule\Event\EventsStorage;
1313
use Crm\ApplicationModule\Menu\MenuContainerInterface;
1414
use Crm\ApplicationModule\User\UserDataRegistrator;
15+
use Crm\ApplicationModule\Widget\LazyWidgetManagerInterface;
1516
use Crm\ApplicationModule\Widget\WidgetManagerInterface;
1617
use League\Event\Emitter;
1718
use Nette\Application\Routers\RouteList;
@@ -26,8 +27,11 @@ public function registerFrontendMenuItems(MenuContainerInterface $menuContainer)
2627

2728
public function registerEventHandlers(Emitter $emitter);
2829

30+
/** @deprecated use registerLazyWidgets() instead */
2931
public function registerWidgets(WidgetManagerInterface $widgetManager);
3032

33+
public function registerLazyWidgets(LazyWidgetManagerInterface $lazyWidgetManager);
34+
3135
public function registerCommands(CommandsContainerInterface $commandsContainer);
3236

3337
public function registerApiCalls(ApiRoutersContainerInterface $apiRoutersContainer);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Crm\ApplicationModule\Widget;
4+
5+
use Crm\ApplicationModule\Components\SimpleWidgetFactoryInterface;
6+
use Crm\ApplicationModule\Snippet\Control\SnippetFactory;
7+
use Kdyby\Autowired\AutowireComponentFactories;
8+
use Nette\Application\UI;
9+
use Nette\ComponentModel\IComponent;
10+
use Nette\DI\Resolver;
11+
use Nette\UnexpectedValueException;
12+
13+
abstract class BaseLazyWidget extends UI\Control implements WidgetInterface
14+
{
15+
use AutowireComponentFactories;
16+
17+
protected LazyWidgetManager $widgetManager;
18+
19+
public function __construct(LazyWidgetManager $widgetManager)
20+
{
21+
$this->widgetManager = $widgetManager;
22+
}
23+
24+
public function header()
25+
{
26+
return 'base lazy widget';
27+
}
28+
29+
public function identifier()
30+
{
31+
return 'identifier';
32+
}
33+
34+
protected function createComponentSimpleWidget(SimpleWidgetFactoryInterface $factory)
35+
{
36+
$control = $factory->create();
37+
return $control;
38+
}
39+
40+
protected function createComponentSnippet(SnippetFactory $factory)
41+
{
42+
$control = $factory->create();
43+
return $control;
44+
}
45+
46+
protected function createComponent(string $name): ?IComponent
47+
{
48+
$ucName = ucfirst($name);
49+
$method = 'createComponent' . $ucName;
50+
if ($ucName !== $name && method_exists($this, $method)) {
51+
$reflection = $this->getReflection()->getMethod($method);
52+
if ($reflection->getName() !== $method) {
53+
return null;
54+
}
55+
$parameters = $reflection->getParameters();
56+
57+
58+
$args = [];
59+
if (($first = reset($parameters)) && !$first->getType()) {
60+
$args[] = $name;
61+
}
62+
63+
$getter = function (string $type) {
64+
return $this->getComponentFactoriesLocator()->getByType($type);
65+
};
66+
$args = Resolver::autowireArguments($reflection, $args, $getter);
67+
$component = call_user_func_array([$this, $method], $args);
68+
if (!$component instanceof IComponent && !isset($this->components[$name])) {
69+
throw new UnexpectedValueException("Method $reflection did not return or create the desired component");
70+
}
71+
72+
return $component;
73+
}
74+
75+
$widget = $this->widgetManager->getWidgetByIdentifier($name);
76+
if ($widget) {
77+
if (!isset($this->components[$widget->identifier()])) {
78+
$this->addComponent($widget, $widget->identifier());
79+
}
80+
return $widget;
81+
}
82+
83+
return null;
84+
}
85+
}

src/Models/Widget/BaseWidget.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ abstract class BaseWidget extends UI\Control implements WidgetInterface
1717
/** @var WidgetManager */
1818
protected $widgetManager;
1919

20+
/** @deprecated use BaseLazyWidget instead */
2021
public function __construct(WidgetManager $widgetManager)
2122
{
2223
$this->widgetManager = $widgetManager;

0 commit comments

Comments
 (0)