PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Zikula/Framework/AbstractPlugin.php

https://github.com/antoniom/core
PHP | 604 lines | 449 code | 26 blank | 129 comment | 5 complexity | 50b3f288423df88af48a3782a71f20ce MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, MIT
  1. <?php
  2. /**
  3. * Copyright 2010 Zikula Foundation.
  4. *
  5. * This work is contributed to the Zikula Foundation under one or more
  6. * Contributor Agreements and licensed to You under the following license:
  7. *
  8. * @license GNU/LGPLv3 (or at your option, any later version).
  9. * @package Zikula
  10. * @subpackage \Zikula\Core\Core
  11. *
  12. * Please see the NOTICE file distributed with this source code for further
  13. * information regarding copyright and licensing.
  14. */
  15. namespace Zikula\Framework;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\EventDispatcher\EventDispatcher;
  18. use Zikula\Common\I18n\TranslatableInterface;
  19. use Zikula\Framework\Controller\AbstractPlugin as AbstractControllerPlugin;
  20. use \ZLanguage;
  21. use \PluginUtil;
  22. /**
  23. * AbstractPlugin abstract class.
  24. */
  25. abstract class AbstractPlugin extends AbstractEventHandler implements TranslatableInterface
  26. {
  27. /**
  28. * Module plugin identifier.
  29. *
  30. * @var constant
  31. */
  32. const TYPE_MODULE = 1;
  33. /**
  34. * Systemwide plugin identifier.
  35. *
  36. * @var constant
  37. */
  38. const TYPE_SYSTEM = 2;
  39. /**
  40. * EventDispatcher.
  41. *
  42. * @var EventDispatcher
  43. */
  44. protected $dispatcher;
  45. /**
  46. * Dependency Injection container.
  47. *
  48. * @var ContainerBuilder
  49. */
  50. protected $container;
  51. /**
  52. * Has this plugin booted.
  53. *
  54. * @var boolean
  55. */
  56. protected $booted = false;
  57. /**
  58. * Plugin meta data.
  59. *
  60. * @var array
  61. */
  62. protected $meta;
  63. /**
  64. * Plugin type.
  65. *
  66. * @var integer
  67. */
  68. protected $pluginType;
  69. /**
  70. * Service ID.
  71. *
  72. * @var string
  73. */
  74. protected $serviceId;
  75. /**
  76. * Class name.
  77. *
  78. * @var string
  79. */
  80. protected $className;
  81. /**
  82. * Translation domain.
  83. *
  84. * @var string|null
  85. */
  86. protected $domain;
  87. /**
  88. * Module name.
  89. *
  90. * @var string
  91. */
  92. protected $moduleName;
  93. /**
  94. * Plugin name.
  95. *
  96. * @var string
  97. */
  98. protected $pluginName;
  99. /**
  100. * Base dir.
  101. *
  102. * @var string
  103. */
  104. protected $baseDir;
  105. /**
  106. * Module info.
  107. *
  108. * @var array
  109. */
  110. protected $modinfo;
  111. /**
  112. * This object's own reflection.
  113. *
  114. * @var ReflectionObject
  115. */
  116. protected $reflection;
  117. /**
  118. * Plugin controller class.
  119. *
  120. * @var AbstractControllerPlugin
  121. */
  122. protected $controllerClass;
  123. /**
  124. * Constructor.
  125. *
  126. * @param ContainerBuilder $container ServiceManager.
  127. *
  128. * @throws \InvalidArgumentException If getMeta() is not implemented correctly.
  129. */
  130. public function __construct(ContainerBuilder $container)
  131. {
  132. $this->container = $container;
  133. $this->dispatcher = $this->container->get('event_dispatcher');
  134. $this->_setup();
  135. $meta = $this->getMeta();
  136. if (!isset($meta['displayname']) && !isset($meta['description']) && !isset($meta['version'])) {
  137. throw new \InvalidArgumentException(sprintf('%s->getMeta() must be implemented according to the abstract. See docblock in Zikula_AbstractPlugin for details', get_class($this)));
  138. }
  139. // Load any handlers if they exist
  140. if ($this->getReflection()->hasMethod('setupHandlerDefinitions')) {
  141. $this->setupHandlerDefinitions();
  142. }
  143. }
  144. /**
  145. * Optional setup of handler definitions.
  146. *
  147. * Overriding the AbstractEventHandler interface which required this.
  148. *
  149. * <samp>
  150. * $this->addHandlerDefinition('some.event', 'handler', 10);
  151. * $this->addHandlerDefinition('some.event', 'handler2', 10);
  152. * </samp>
  153. *
  154. * @return void
  155. */
  156. protected function setupHandlerDefinitions()
  157. {
  158. }
  159. /**
  160. * Get this reflection.
  161. *
  162. * @return ReflectionObject
  163. */
  164. public function getReflection()
  165. {
  166. if (!is_null($this->reflection)) {
  167. return $this->reflection;
  168. }
  169. $this->reflection = new \ReflectionObject($this);
  170. return $this->reflection;
  171. }
  172. /**
  173. * Internal setup.
  174. *
  175. * @throws \LogicException If plugin is not named correctly.
  176. *
  177. * @return void
  178. */
  179. private function _setup()
  180. {
  181. $this->className = get_class($this);
  182. $this->serviceId = \PluginUtil::getServiceId($this->className);
  183. $this->baseDir = dirname($this->getReflection()->getFileName());
  184. // Split class name into parts - commented in if statement below.
  185. $class = $this->className;
  186. $p = strpos($class, '_') ? explode('_', $class) : explode('\\', $class);
  187. if (strpos($this->serviceId, 'moduleplugin') === 0) {
  188. // ModulePlugin_{ModuleName}_{PluginName}_Plugin
  189. // $p[1] = ModuleName, $p[2] = PluginName
  190. $this->moduleName = $p[1];
  191. $this->pluginName = $p[2];
  192. $this->pluginType = self::TYPE_MODULE;
  193. $this->domain = ZLanguage::getModulePluginDomain($this->moduleName, $this->pluginName);
  194. ZLanguage::bindModulePluginDomain($this->moduleName, $this->pluginName);
  195. } elseif (strpos($this->serviceId, 'systemplugin') === 0) {
  196. // SystemPlugin_{PluginName}_Plugin
  197. // $p[1] = ModuleName
  198. $this->moduleName = 'zikula';
  199. $this->pluginName = $p[1];
  200. $this->pluginType = self::TYPE_SYSTEM;
  201. $this->domain = ZLanguage::getSystemPluginDomain($this->pluginName);
  202. ZLanguage::bindSystemPluginDomain($this->pluginName);
  203. } else {
  204. throw new \LogicException(sprintf('This class %s does not appear to be named correctly. System plugins should be named {SystemPlugin}_{Name}_Plugin, module plugins should be named {ModulePlugin}_{ModuleName}_{PluginName}_Plugin.', $this->className));
  205. }
  206. $this->meta = $this->getMeta();
  207. }
  208. /**
  209. * Get plugin meta data.
  210. *
  211. * Should return an array like this:
  212. * <sample>
  213. * $meta = array('displayname' => $this->__('Display name'),
  214. * 'description' => $this->__('Description goes here'),
  215. * 'version' => '1.0.0'
  216. * );
  217. *
  218. * return $meta;
  219. * </sample>
  220. *
  221. * @return array
  222. */
  223. abstract protected function getMeta();
  224. /**
  225. * Get meta display name.
  226. *
  227. * @return string
  228. */
  229. public function getMetaDisplayName()
  230. {
  231. return $this->meta['displayname'];
  232. }
  233. /**
  234. * Get meta description.
  235. *
  236. * @return string
  237. */
  238. public function getMetaDescription()
  239. {
  240. return $this->meta['description'];
  241. }
  242. /**
  243. * Get meta version number.
  244. *
  245. * @return string
  246. */
  247. public function getMetaVersion()
  248. {
  249. return $this->meta['version'];
  250. }
  251. /**
  252. * Get module info.
  253. *
  254. * @return array
  255. */
  256. public function getModInfo()
  257. {
  258. if (!$this->modinfo) {
  259. // this is deliberate lazy load for dependency.
  260. $this->modinfo = \ModUtil::getInfoFromName($this->moduleName);
  261. }
  262. return $this->modinfo;
  263. }
  264. /**
  265. * Return basedir.
  266. *
  267. * @return string
  268. */
  269. public function getBaseDir()
  270. {
  271. return $this->baseDir;
  272. }
  273. /**
  274. * Get this plugin type.
  275. *
  276. * @return integer
  277. */
  278. public function getPluginType()
  279. {
  280. return $this->pluginType;
  281. }
  282. /**
  283. * Translate.
  284. *
  285. * @param string $msgid String to be translated.
  286. *
  287. * @return string
  288. */
  289. public function __($msgid)
  290. {
  291. return __($msgid, $this->domain);
  292. }
  293. /**
  294. * Translate with sprintf().
  295. *
  296. * @param string $msgid String to be translated.
  297. * @param string|array $params Args for sprintf().
  298. *
  299. * @return string
  300. */
  301. public function __f($msgid, $params)
  302. {
  303. return __f($msgid, $params, $this->domain);
  304. }
  305. /**
  306. * Translate plural string.
  307. *
  308. * @param string $singular Singular instance.
  309. * @param string $plural Plural instance.
  310. * @param string $count Object count.
  311. *
  312. * @return string Translated string.
  313. */
  314. public function _n($singular, $plural, $count)
  315. {
  316. return _n($singular, $plural, $count, $this->domain);
  317. }
  318. /**
  319. * Translate plural string with sprintf().
  320. *
  321. * @param string $sin Singular instance.
  322. * @param string $plu Plural instance.
  323. * @param string $n Object count.
  324. * @param string|array $params Sprintf() arguments.
  325. *
  326. * @return string
  327. */
  328. public function _fn($sin, $plu, $n, $params)
  329. {
  330. return _fn($sin, $plu, $n, $params, $this->domain);
  331. }
  332. /**
  333. * Get this service ID.
  334. *
  335. * @return string
  336. */
  337. public function getServiceId()
  338. {
  339. return $this->serviceId;
  340. }
  341. /**
  342. * Return the translation domain property.
  343. *
  344. * @return string|null
  345. */
  346. public function getDomain()
  347. {
  348. return $this->domain;
  349. }
  350. /**
  351. * Get module name this belongs to.
  352. *
  353. * @return string
  354. */
  355. public function getModuleName()
  356. {
  357. return $this->moduleName;
  358. }
  359. /**
  360. * Get this plugin name.
  361. *
  362. * @return string
  363. */
  364. public function getPluginName()
  365. {
  366. return $this->pluginName;
  367. }
  368. /**
  369. * Pre intialise hook.
  370. *
  371. * @return void
  372. */
  373. public function preInitialize()
  374. {
  375. }
  376. /**
  377. * Initialize plugin.
  378. *
  379. * @return void
  380. */
  381. public function initialize()
  382. {
  383. }
  384. /**
  385. * Post intialise hook.
  386. *
  387. * @return void
  388. */
  389. public function postInitialize()
  390. {
  391. }
  392. /**
  393. * Post enable handler.
  394. *
  395. * @return void
  396. */
  397. public function postEnable()
  398. {
  399. }
  400. /**
  401. * Post disable handler.
  402. *
  403. * @return void
  404. */
  405. public function postDisable()
  406. {
  407. }
  408. /**
  409. * Has booted check.
  410. *
  411. * @return boolean
  412. */
  413. public function hasBooted()
  414. {
  415. return $this->booted;
  416. }
  417. /**
  418. * Flag booted.
  419. *
  420. * @return void
  421. */
  422. public function setBooted()
  423. {
  424. $this->booted = true;
  425. }
  426. /**
  427. * Whether or not the plugin is enabled.
  428. *
  429. * @return boolean
  430. */
  431. public function isEnabled()
  432. {
  433. if ($this instanceof \Zikula\Framework\Plugin\AlwaysOnInterface) {
  434. return true;
  435. }
  436. $plugin = \PluginUtil::getState($this->serviceId, \PluginUtil::getDefaultState());
  437. return ($plugin['state'] === PluginUtil::ENABLED) ? true : false;
  438. }
  439. /**
  440. * Whether or not the plugin is installed.
  441. *
  442. * @return boolean
  443. */
  444. public function isInstalled()
  445. {
  446. if ($this instanceof \Zikula\Framework\Plugin\AlwaysOnInterface) {
  447. return true;
  448. }
  449. $plugin = PluginUtil::getState($this->serviceId, PluginUtil::getDefaultState());
  450. return ($plugin['state'] === PluginUtil::NOTINSTALLED) ? false : true;
  451. }
  452. /**
  453. * Pre install handler.
  454. *
  455. * @return boolean
  456. */
  457. public function preInstall()
  458. {
  459. return true;
  460. }
  461. /**
  462. * Install.
  463. *
  464. * @return boolean
  465. */
  466. public function install()
  467. {
  468. return true;
  469. }
  470. /**
  471. * Post install handler.
  472. *
  473. * @return boolean
  474. */
  475. public function postInstall()
  476. {
  477. return true;
  478. }
  479. /**
  480. * Pre uninstall handler.
  481. *
  482. * @return boolean
  483. */
  484. public function preUninstall()
  485. {
  486. return true;
  487. }
  488. /**
  489. * Uninstall.
  490. *
  491. * @return boolean
  492. */
  493. public function uninstall()
  494. {
  495. return true;
  496. }
  497. /**
  498. * Post uninstall handler.
  499. *
  500. * @return boolean
  501. */
  502. public function postUninstall()
  503. {
  504. return true;
  505. }
  506. /**
  507. * Pre upgrade handler.
  508. *
  509. * @param string $oldversion Old version.
  510. *
  511. * @return boolean
  512. */
  513. public function preUpgrade($oldversion)
  514. {
  515. return true;
  516. }
  517. /**
  518. * Upgrade
  519. *
  520. * @param string $oldversion Old version.
  521. *
  522. * @return boolean
  523. */
  524. public function upgrade($oldversion)
  525. {
  526. return true;
  527. }
  528. /**
  529. * Post upgrade handler.
  530. *
  531. * @return boolean
  532. */
  533. public function postUpgrade()
  534. {
  535. return true;
  536. }
  537. }