|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Widget; |
|
6
|
|
|
|
|
7
|
|
|
use Throwable; |
|
8
|
|
|
use Yiisoft\Definitions\Exception\NotInstantiableException as FactoryNotInstantiableException; |
|
9
|
|
|
use Yiisoft\FriendlyException\FriendlyExceptionInterface; |
|
10
|
|
|
|
|
11
|
|
|
final class NotInstantiableException extends FactoryNotInstantiableException implements FriendlyExceptionInterface |
|
12
|
|
|
{ |
|
13
|
2 |
|
public function __construct( |
|
14
|
|
|
private string $widgetClassName, |
|
15
|
|
|
private bool $widgetFactoryInitialized, |
|
16
|
|
|
private Throwable $previous, |
|
17
|
|
|
) { |
|
18
|
2 |
|
$message = 'Failed to create a widget "' . $this->widgetClassName . '". ' . $previous->getMessage(); |
|
19
|
2 |
|
if (!$this->widgetFactoryInitialized) { |
|
20
|
1 |
|
$message .= ' Perhaps you need to initialize "' . WidgetFactory::class . '" with DI container to resolve dependencies.'; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
2 |
|
parent::__construct($message, previous: $previous); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
2 |
|
public function getName(): string |
|
27
|
|
|
{ |
|
28
|
2 |
|
return 'Failed to create a widget "' . $this->widgetClassName . '". ' . $this->previous->getMessage(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
2 |
|
public function getSolution(): ?string |
|
32
|
|
|
{ |
|
33
|
2 |
|
if ($this->widgetFactoryInitialized) { |
|
34
|
1 |
|
return null; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
$widgetFactoryClass = WidgetFactory::class; |
|
38
|
|
|
|
|
39
|
1 |
|
return <<<SOLUTION |
|
40
|
1 |
|
Perhaps you need to initialize "$widgetFactoryClass" with DI container to resolve dependencies. |
|
41
|
|
|
|
|
42
|
|
|
To initialize the widget factory call `WidgetFactory::initialize()` before using the widget. |
|
43
|
|
|
It is a good idea to do that for the whole application. |
|
44
|
|
|
|
|
45
|
|
|
Example: |
|
46
|
|
|
|
|
47
|
|
|
```php |
|
48
|
|
|
/** |
|
49
|
|
|
* @var Psr\Container\ContainerInterface \$container |
|
50
|
|
|
*/ |
|
51
|
|
|
|
|
52
|
|
|
Yiisoft\Widget\WidgetFactory::initialize( |
|
53
|
|
|
container: \$container, |
|
54
|
|
|
definitions: [MyWidget::class => new MyWidget(/*...*/)], |
|
55
|
|
|
themes: [ |
|
56
|
|
|
'custom' => [ |
|
57
|
|
|
MyWidget::class => [ |
|
58
|
|
|
'setValue()' => [42], |
|
59
|
|
|
], |
|
60
|
|
|
], |
|
61
|
|
|
], |
|
62
|
|
|
validate: true, // Whether definitions need to be validated. |
|
63
|
|
|
); |
|
64
|
|
|
``` |
|
65
|
|
|
|
|
66
|
|
|
See Yii example in the configuration file of this package `config/bootstrap.php`. |
|
67
|
1 |
|
SOLUTION; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|