generated from yiisoft/package-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow use widgets without widget factory initialization + Improve CI (#…
…99) * Allow use widgets without widget factory initialization * improve CI * Update CHANGELOG.md Co-authored-by: Alexey Rogachev <arogachev90@gmail.com> * Add exception * Apply fixes from StyleCI * fix * improve message * improve message * Apply fixes from StyleCI * improve --------- Co-authored-by: Alexey Rogachev <arogachev90@gmail.com> Co-authored-by: StyleCI Bot <bot@styleci.io>
- Loading branch information
1 parent
c1edad7
commit c0372ef
Showing
12 changed files
with
199 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Widget; | ||
|
||
use Throwable; | ||
use Yiisoft\Definitions\Exception\NotInstantiableException as FactoryNotInstantiableException; | ||
use Yiisoft\FriendlyException\FriendlyExceptionInterface; | ||
|
||
final class NotInstantiableException extends FactoryNotInstantiableException implements FriendlyExceptionInterface | ||
{ | ||
public function __construct( | ||
private string $widgetClassName, | ||
private bool $widgetFactoryInitialized, | ||
private Throwable $previous, | ||
) { | ||
$message = 'Failed to create a widget "' . $this->widgetClassName . '". ' . $previous->getMessage(); | ||
if (!$this->widgetFactoryInitialized) { | ||
$message .= ' Perhaps you need to initialize "' . WidgetFactory::class . '" with DI container to resolve dependencies.'; | ||
} | ||
|
||
parent::__construct($message, previous: $previous); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'Failed to create a widget "' . $this->widgetClassName . '". ' . $this->previous->getMessage(); | ||
} | ||
|
||
public function getSolution(): ?string | ||
{ | ||
if ($this->widgetFactoryInitialized) { | ||
return null; | ||
} | ||
|
||
$widgetFactoryClass = WidgetFactory::class; | ||
|
||
return <<<SOLUTION | ||
Perhaps you need to initialize "$widgetFactoryClass" with DI container to resolve dependencies. | ||
To initialize the widget factory call `WidgetFactory::initialize()` before using the widget. | ||
It is a good idea to do that for the whole application. | ||
Example: | ||
```php | ||
/** | ||
* @var Psr\Container\ContainerInterface \$container | ||
*/ | ||
Yiisoft\Widget\WidgetFactory::initialize( | ||
container: \$container, | ||
definitions: [MyWidget::class => new MyWidget(/*...*/)], | ||
themes: [ | ||
'custom' => [ | ||
MyWidget::class => [ | ||
'setValue()' => [42], | ||
], | ||
], | ||
], | ||
validate: true, // Whether definitions need to be validated. | ||
); | ||
``` | ||
See Yii example in the configuration file of this package `config/bootstrap.php`. | ||
SOLUTION; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Widget\Tests\Stubs; | ||
|
||
use Yiisoft\Widget\Widget; | ||
|
||
final class Garage extends Widget | ||
{ | ||
public function __construct(Car $car) | ||
{ | ||
} | ||
|
||
public function render(): string | ||
{ | ||
return 'Car in garage.'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters