|
14 | 14 | use Joomla\CMS\Event\Result\ResultAwareInterface; |
15 | 15 | use Joomla\CMS\Extension\PluginInterface; |
16 | 16 | use Joomla\CMS\Factory; |
| 17 | +use Joomla\CMS\Language\Language; |
17 | 18 | use Joomla\CMS\Language\LanguageAwareInterface; |
18 | 19 | use Joomla\CMS\Language\LanguageAwareTrait; |
19 | 20 | use Joomla\Event\AbstractEvent; |
|
32 | 33 | * Plugin Class |
33 | 34 | * |
34 | 35 | * @since 1.5 |
| 36 | + * |
| 37 | + * @TODO Starting from 7.0 the class will no longer implement DispatcherAwareInterface and LanguageAwareInterface |
35 | 38 | */ |
36 | 39 | abstract class CMSPlugin implements DispatcherAwareInterface, PluginInterface, LanguageAwareInterface |
37 | 40 | { |
38 | | - use DispatcherAwareTrait; |
39 | | - use LanguageAwareTrait; |
| 41 | + use DispatcherAwareTrait { |
| 42 | + setDispatcher as traitSetDispatcher; |
| 43 | + getDispatcher as traitGetDispatcher; |
| 44 | + } |
| 45 | + use LanguageAwareTrait { |
| 46 | + setLanguage as traitSetLanguage; |
| 47 | + getLanguage as traitGetLanguage; |
| 48 | + } |
40 | 49 |
|
41 | 50 | /** |
42 | 51 | * A Registry object holding the parameters for the plugin |
@@ -101,15 +110,31 @@ abstract class CMSPlugin implements DispatcherAwareInterface, PluginInterface, L |
101 | 110 | /** |
102 | 111 | * Constructor |
103 | 112 | * |
104 | | - * @param DispatcherInterface $dispatcher The event dispatcher |
105 | | - * @param array $config An optional associative array of configuration settings. |
106 | | - * Recognized key values include 'name', 'group', 'params', 'language' |
107 | | - * (this list is not meant to be comprehensive). |
| 113 | + * @param array $config An optional associative array of configuration settings. |
| 114 | + * Recognized key values include 'name', 'group', 'params', 'language' |
| 115 | + * (this list is not meant to be comprehensive). |
108 | 116 | * |
109 | 117 | * @since 1.5 |
110 | 118 | */ |
111 | | - public function __construct(DispatcherInterface $dispatcher, array $config = []) |
| 119 | + public function __construct($config = []) |
112 | 120 | { |
| 121 | + if ($config instanceof DispatcherInterface) { |
| 122 | + @trigger_error( |
| 123 | + \sprintf( |
| 124 | + 'Passing an instance of %1$s to %2$s() will not be supported in 7.0. ' |
| 125 | + . 'Starting from 7.0 CMSPlugin class will no longer implement DispatcherAwareInterface.', |
| 126 | + DispatcherInterface::class, |
| 127 | + __METHOD__ |
| 128 | + ), |
| 129 | + \E_USER_DEPRECATED |
| 130 | + ); |
| 131 | + |
| 132 | + // Set the dispatcher we are to register our listeners with |
| 133 | + $this->setDispatcher($config); |
| 134 | + |
| 135 | + $config = \func_num_args() > 1 ? func_get_arg(1) : []; |
| 136 | + } |
| 137 | + |
113 | 138 | // Get the parameters. |
114 | 139 | if (isset($config['params'])) { |
115 | 140 | if ($config['params'] instanceof Registry) { |
@@ -153,9 +178,6 @@ public function __construct(DispatcherInterface $dispatcher, array $config = []) |
153 | 178 | $this->db = Factory::getDbo(); |
154 | 179 | } |
155 | 180 | } |
156 | | - |
157 | | - // Set the dispatcher we are to register our listeners with |
158 | | - $this->setDispatcher($dispatcher); |
159 | 181 | } |
160 | 182 |
|
161 | 183 | /** |
@@ -390,4 +412,83 @@ public function setApplication(CMSApplicationInterface $application): void |
390 | 412 | $this->setLanguage($application->getLanguage()); |
391 | 413 | } |
392 | 414 | } |
| 415 | + |
| 416 | + /** |
| 417 | + * Set the language to use. |
| 418 | + * |
| 419 | + * @param Language $language The language to use |
| 420 | + * |
| 421 | + * @return void |
| 422 | + * |
| 423 | + * @since __DEPLOY_VERSION__ |
| 424 | + * |
| 425 | + * @deprecated 5.2 will be removed in 7.0 |
| 426 | + * Plugin should use the language from Application, and only after the app is initialised |
| 427 | + */ |
| 428 | + public function setLanguage(Language $language): void |
| 429 | + { |
| 430 | + $this->traitSetLanguage($language); |
| 431 | + } |
| 432 | + |
| 433 | + /** |
| 434 | + * Get the Language. |
| 435 | + * |
| 436 | + * @return Language |
| 437 | + * |
| 438 | + * @throws \UnexpectedValueException May be thrown if the language has not been set. |
| 439 | + * |
| 440 | + * @since __DEPLOY_VERSION__ |
| 441 | + * |
| 442 | + * @deprecated 5.2 will be removed in 7.0 |
| 443 | + * Plugin should use the language from Application, and only after the app is initialised. |
| 444 | + */ |
| 445 | + protected function getLanguage(): Language |
| 446 | + { |
| 447 | + @trigger_error( |
| 448 | + __CLASS__ . ': Use of LanguageAwareInterface over CMSPlugin will be removed in 7.0.', |
| 449 | + \E_USER_DEPRECATED |
| 450 | + ); |
| 451 | + |
| 452 | + return $this->traitGetLanguage(); |
| 453 | + } |
| 454 | + |
| 455 | + /** |
| 456 | + * Set the dispatcher to use. |
| 457 | + * |
| 458 | + * @param DispatcherInterface $dispatcher The dispatcher to use. |
| 459 | + * |
| 460 | + * @return $this |
| 461 | + * |
| 462 | + * @since __DEPLOY_VERSION__ |
| 463 | + * |
| 464 | + * @deprecated 5.2 will be removed in 7.0 |
| 465 | + * Plugin should implement DispatcherAwareInterface on its own, when it is needed. |
| 466 | + */ |
| 467 | + public function setDispatcher(DispatcherInterface $dispatcher) |
| 468 | + { |
| 469 | + @trigger_error( |
| 470 | + __CLASS__ . ': Use of DispatcherAwareInterface over CMSPlugin will be removed in 7.0.' |
| 471 | + . ' Plugin should implement DispatcherAwareInterface on its own, when it is needed.', |
| 472 | + \E_USER_DEPRECATED |
| 473 | + ); |
| 474 | + |
| 475 | + return $this->traitSetDispatcher($dispatcher); |
| 476 | + } |
| 477 | + |
| 478 | + /** |
| 479 | + * Get the event dispatcher. |
| 480 | + * |
| 481 | + * @return DispatcherInterface |
| 482 | + * |
| 483 | + * @throws \UnexpectedValueException May be thrown if the dispatcher has not been set. |
| 484 | + * |
| 485 | + * @since __DEPLOY_VERSION__ |
| 486 | + * |
| 487 | + * @deprecated 5.2 will be removed in 7.0 |
| 488 | + * Plugin should implement DispatcherAwareInterface on its own, when it is needed. |
| 489 | + */ |
| 490 | + public function getDispatcher() |
| 491 | + { |
| 492 | + return $this->traitGetDispatcher(); |
| 493 | + } |
393 | 494 | } |
0 commit comments