1 <?php
  2 
  3 /**
  4  * Class for plugins.
  5  *
  6  * @author gharlan
  7  *
  8  * @package redaxo\core\packages
  9  */
 10 class rex_plugin extends rex_package implements rex_plugin_interface
 11 {
 12     /**
 13      * Parent addon.
 14      *
 15      * @var rex_addon
 16      */
 17     private $addon;
 18 
 19     /**
 20      * Constructor.
 21      *
 22      * @param string    $name  Name
 23      * @param rex_addon $addon Parent addon
 24      */
 25     public function __construct($name, rex_addon $addon)
 26     {
 27         parent::__construct($name);
 28         $this->addon = $addon;
 29     }
 30 
 31     /**
 32      * Returns the plugin by the given name.
 33      *
 34      * @param string $addon  Name of the addon
 35      * @param string $plugin Name of the plugin
 36      *
 37      * @return self
 38      *
 39      * @throws InvalidArgumentException
 40      */
 41     public static function get($addon, $plugin = null)
 42     {
 43         if ($plugin === null) {
 44             throw new InvalidArgumentException('Missing Argument 2 for ' . self::class . '::' . __METHOD__ . '()');
 45         }
 46         if (!is_string($addon)) {
 47             throw new InvalidArgumentException('Expecting $addon to be string, but ' . gettype($addon) . ' given!');
 48         }
 49         if (!is_string($plugin)) {
 50             throw new InvalidArgumentException('Expecting $plugin to be string, but ' . gettype($plugin) . ' given!');
 51         }
 52         return rex_addon::get($addon)->getPlugin($plugin);
 53     }
 54 
 55     /**
 56      * Returns if the plugin exists.
 57      *
 58      * @param string $addon  Name of the addon
 59      * @param string $plugin Name of the plugin
 60      *
 61      * @return bool
 62      */
 63     public static function exists($addon, $plugin = null)
 64     {
 65         return rex_addon::exists($addon) && rex_addon::get($addon)->pluginExists($plugin);
 66     }
 67 
 68     /**
 69      * {@inheritdoc}
 70      */
 71     public function getAddon()
 72     {
 73         return $this->addon;
 74     }
 75 
 76     /**
 77      * {@inheritdoc}
 78      */
 79     public function getPackageId()
 80     {
 81         return $this->getAddon()->getName() . '/' . $this->getName();
 82     }
 83 
 84     /**
 85      * {@inheritdoc}
 86      */
 87     public function getType()
 88     {
 89         return 'plugin';
 90     }
 91 
 92     /**
 93      * {@inheritdoc}
 94      */
 95     public function getPath($file = '')
 96     {
 97         return rex_path::plugin($this->getAddon()->getName(), $this->getName(), $file);
 98     }
 99 
100     /**
101      * {@inheritdoc}
102      */
103     public function getAssetsPath($file = '')
104     {
105         return rex_path::pluginAssets($this->getAddon()->getName(), $this->getName(), $file);
106     }
107 
108     /**
109      * {@inheritdoc}
110      */
111     public function getAssetsUrl($file = '')
112     {
113         return rex_url::pluginAssets($this->getAddon()->getName(), $this->getName(), $file);
114     }
115 
116     /**
117      * {@inheritdoc}
118      */
119     public function getDataPath($file = '')
120     {
121         return rex_path::pluginData($this->getAddon()->getName(), $this->getName(), $file);
122     }
123 
124     /**
125      * {@inheritdoc}
126      */
127     public function getCachePath($file = '')
128     {
129         return rex_path::pluginCache($this->getAddon()->getName(), $this->getName(), $file);
130     }
131 
132     /**
133      * {@inheritdoc}
134      */
135     public function isAvailable()
136     {
137         return $this->getAddon()->isAvailable() && parent::isAvailable();
138     }
139 
140     /**
141      * {@inheritdoc}
142      */
143     public function isSystemPackage()
144     {
145         return in_array($this->getName(), (array) $this->addon->getProperty('system_plugins', []));
146     }
147 
148     /**
149      * {@inheritdoc}
150      */
151     public function i18n($key)
152     {
153         $args = func_get_args();
154         $key = $this->getAddon()->getName() . '_' . $this->getName() . '_' . $key;
155         if (rex_i18n::hasMsgOrFallback($key)) {
156             $args[0] = $key;
157             return call_user_func_array('rex_i18n::msg', $args);
158         }
159         return call_user_func_array([$this->getAddon(), 'i18n'], $args);
160     }
161 
162     /**
163      * Returns the registered plugins of the given addon.
164      *
165      * @param string $addon Addon name
166      *
167      * @return self[]
168      */
169     public static function getRegisteredPlugins($addon)
170     {
171         return rex_addon::get($addon)->getRegisteredPlugins();
172     }
173 
174     /**
175      * Returns the installed plugins of the given addons.
176      *
177      * @param string $addon Addon name
178      *
179      * @return self[]
180      */
181     public static function getInstalledPlugins($addon)
182     {
183         return rex_addon::get($addon)->getInstalledPlugins();
184     }
185 
186     /**
187      * Returns the available plugins of the given addons.
188      *
189      * @param string $addon Addon name
190      *
191      * @return self[]
192      */
193     public static function getAvailablePlugins($addon)
194     {
195         return rex_addon::get($addon)->getAvailablePlugins();
196     }
197 
198     /**
199      * Returns the system plugins of the given addons.
200      *
201      * @param string $addon Addon name
202      *
203      * @return self[]
204      */
205     public static function getSystemPlugins($addon)
206     {
207         return rex_addon::get($addon)->getSystemPlugins();
208     }
209 }
210