1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage Plugin
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13
14 /**
15 * JPlugin Class
16 *
17 * @since 1.5
18 */
19 abstract class JPlugin extends JEvent
20 {
21 /**
22 * A Registry object holding the parameters for the plugin
23 *
24 * @var Registry
25 * @since 1.5
26 */
27 public $params = null;
28
29 /**
30 * The name of the plugin
31 *
32 * @var string
33 * @since 1.5
34 */
35 protected $_name = null;
36
37 /**
38 * The plugin type
39 *
40 * @var string
41 * @since 1.5
42 */
43 protected $_type = null;
44
45 /**
46 * Affects constructor behavior. If true, language files will be loaded automatically.
47 *
48 * @var boolean
49 * @since 3.1
50 */
51 protected $autoloadLanguage = false;
52
53 /**
54 * Constructor
55 *
56 * @param object &$subject The object to observe
57 * @param array $config An optional associative array of configuration settings.
58 * Recognized key values include 'name', 'group', 'params', 'language'
59 * (this list is not meant to be comprehensive).
60 *
61 * @since 1.5
62 */
63 public function __construct(&$subject, $config = array())
64 {
65 // Get the parameters.
66 if (isset($config['params']))
67 {
68 if ($config['params'] instanceof Registry)
69 {
70 $this->params = $config['params'];
71 }
72 else
73 {
74 $this->params = new Registry($config['params']);
75 }
76 }
77
78 // Get the plugin name.
79 if (isset($config['name']))
80 {
81 $this->_name = $config['name'];
82 }
83
84 // Get the plugin type.
85 if (isset($config['type']))
86 {
87 $this->_type = $config['type'];
88 }
89
90 // Load the language files if needed.
91 if ($this->autoloadLanguage)
92 {
93 $this->loadLanguage();
94 }
95
96 if (property_exists($this, 'app'))
97 {
98 $reflection = new ReflectionClass($this);
99
100 if ($reflection->getProperty('app')->isPrivate() === false && $this->app === null)
101 {
102 $this->app = JFactory::getApplication();
103 }
104 }
105
106 if (property_exists($this, 'db'))
107 {
108 $reflection = new ReflectionClass($this);
109
110 if ($reflection->getProperty('db')->isPrivate() === false && $this->db === null)
111 {
112 $this->db = JFactory::getDbo();
113 }
114 }
115
116 parent::__construct($subject);
117 }
118
119 /**
120 * Loads the plugin language file
121 *
122 * @param string $extension The extension for which a language file should be loaded
123 * @param string $basePath The basepath to use
124 *
125 * @return boolean True, if the file has successfully loaded.
126 *
127 * @since 1.5
128 */
129 public function loadLanguage($extension = '', $basePath = JPATH_ADMINISTRATOR)
130 {
131 if (empty($extension))
132 {
133 $extension = 'Plg_' . $this->_type . '_' . $this->_name;
134 }
135
136 $extension = strtolower($extension);
137 $lang = JFactory::getLanguage();
138
139 // If language already loaded, don't load it again.
140 if ($lang->getPaths($extension))
141 {
142 return true;
143 }
144
145 return $lang->load($extension, $basePath, null, false, true)
146 || $lang->load($extension, JPATH_PLUGINS . '/' . $this->_type . '/' . $this->_name, null, false, true);
147 }
148 }
149