-
-
Couldn't load subscription status.
- Fork 3.7k
[4.2] Inject the application into the service provider plugins #38060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
478579d
ff7eb1d
3bb8c80
49c1057
3e42cf1
93c484d
78eca6f
9089d2d
59251c3
5e57aca
0589d45
9f68830
177c86e
8a444fc
0ad75a7
36b7aa5
29943ee
1801c39
2822a33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
|
|
||
| \defined('JPATH_PLATFORM') or die; | ||
|
|
||
| use Joomla\CMS\Application\CMSApplicationInterface; | ||
| use Joomla\CMS\Extension\PluginInterface; | ||
| use Joomla\CMS\Factory; | ||
| use Joomla\Event\AbstractEvent; | ||
|
|
@@ -75,6 +76,15 @@ abstract class CMSPlugin implements DispatcherAwareInterface, PluginInterface | |
| */ | ||
| protected $allowLegacyListeners = true; | ||
|
|
||
| /** | ||
| * The application object | ||
| * | ||
| * @var CMSApplicationInterface | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| private $application; | ||
|
|
||
| /** | ||
| * Constructor | ||
| * | ||
|
|
@@ -120,6 +130,7 @@ public function __construct(&$subject, $config = array()) | |
|
|
||
| if (property_exists($this, 'app')) | ||
| { | ||
| @trigger_error('The application should be injected through setApplication() and requested through getApplication().', E_USER_DEPRECATED); | ||
| $reflection = new \ReflectionClass($this); | ||
| $appProperty = $reflection->getProperty('app'); | ||
|
|
||
|
|
@@ -131,6 +142,7 @@ public function __construct(&$subject, $config = array()) | |
|
|
||
| if (property_exists($this, 'db')) | ||
| { | ||
| @trigger_error('The database should be injected through the DatabaseAwareInterface and trait.', E_USER_DEPRECATED); | ||
| $reflection = new \ReflectionClass($this); | ||
| $dbProperty = $reflection->getProperty('db'); | ||
|
|
||
|
|
@@ -162,7 +174,7 @@ public function loadLanguage($extension = '', $basePath = JPATH_ADMINISTRATOR) | |
| } | ||
|
|
||
| $extension = strtolower($extension); | ||
| $lang = Factory::getLanguage(); | ||
| $lang = $this->getApplication() ? $this->getApplication()->getLanguage() : Factory::getLanguage(); | ||
|
|
||
| // If language already loaded, don't load it again. | ||
| if ($lang->getPaths($extension)) | ||
|
|
@@ -174,6 +186,35 @@ public function loadLanguage($extension = '', $basePath = JPATH_ADMINISTRATOR) | |
| || $lang->load($extension, JPATH_PLUGINS . '/' . $this->_type . '/' . $this->_name); | ||
| } | ||
|
|
||
| /** | ||
| * Translates the given key with the local applications language. If arguments are available, then | ||
| * injects them into the translated string. | ||
| * | ||
| * @param string $key The key to translate | ||
| * @param mixed[] $arguments The arguments | ||
| * | ||
| * @return string The translated string | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| * | ||
| * @see sprintf | ||
| */ | ||
| protected function translate(string $key): string | ||
| { | ||
| $language = $this->getApplication()->getLanguage(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we add some kind of check here? If application is not injected, this line would cause fatal error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be not bad. Can you make a pr? |
||
|
|
||
| $arguments = \func_get_args(); | ||
|
|
||
| if (count($arguments) > 1) | ||
| { | ||
| $arguments[0] = $language->_($key); | ||
|
|
||
| return \call_user_func_array('sprintf', $arguments); | ||
laoneo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return $language->_($key); | ||
| } | ||
|
|
||
| /** | ||
| * Registers legacy Listeners to the Dispatcher, emulating how plugins worked under Joomla! 3.x and below. | ||
| * | ||
|
|
@@ -359,4 +400,30 @@ private function parameterImplementsEventInterface(\ReflectionParameter $paramet | |
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the internal application or null when not set. | ||
| * | ||
| * @return CMSApplicationInterface|null | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| protected function getApplication(): ?CMSApplicationInterface | ||
| { | ||
| return $this->application; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the application to use. | ||
| * | ||
| * @param CMSApplicationInterface $application The application | ||
| * | ||
| * @return void | ||
| * | ||
| * @since __DEPLOY_VERSION__ | ||
| */ | ||
| public function setApplication(CMSApplicationInterface $application): void | ||
| { | ||
| $this->application = $application; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.