PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Zikula/Framework/AbstractBase.php

https://github.com/antoniom/core
PHP | 784 lines | 681 code | 17 blank | 86 comment | 1 complexity | 8dae34a6ef1a6d9e5e083e3a7c901bf7 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\ContainerInterface;
  17. use Symfony\Component\DependencyInjection\ContainerBuilder;
  18. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  19. use \Zikula\Common\I18n\TranslatableInterface;
  20. /**
  21. * AbstractBase class for module abstract controllers and apis.
  22. */
  23. abstract class AbstractBase implements TranslatableInterface, ContainerAwareInterface
  24. {
  25. /**
  26. * Base path of module.
  27. *
  28. * @var string
  29. */
  30. protected static $path;
  31. /**
  32. * Name.
  33. *
  34. * @var string
  35. */
  36. protected $name;
  37. /**
  38. * Base dir.
  39. *
  40. * @var string
  41. */
  42. protected $baseDir;
  43. /**
  44. * System basedir.
  45. *
  46. * @var string
  47. */
  48. protected $systemBaseDir;
  49. /**
  50. * Modinfo.
  51. *
  52. * @var array
  53. */
  54. protected $modinfo;
  55. /**
  56. * Translation domain.
  57. *
  58. * @var string|null
  59. */
  60. protected $domain = null;
  61. /**
  62. * ServiceManager.
  63. *
  64. * @var \Zikula\Component\DependencyInjection\ContainerBuilder
  65. */
  66. protected $container;
  67. /**
  68. * EventManager.
  69. *
  70. * @var \Symfony\Component\EventDispatcher\EventDispatcher
  71. */
  72. protected $dispatcher;
  73. /**
  74. * Doctrine EntityManager.
  75. *
  76. * @var \Doctrine\ORM\EntityManager
  77. */
  78. protected $entityManager;
  79. /**
  80. * Request.
  81. *
  82. * @var \Symfony\Component\HttpFoundation\Request
  83. */
  84. protected $request;
  85. /**
  86. * Session.
  87. *
  88. * @var \Symfony\Component\HttpFoundation\Session\Session
  89. */
  90. protected $session;
  91. /**
  92. * This object's reflection.
  93. *
  94. * @var \ReflectionObject
  95. */
  96. protected $reflection;
  97. /**
  98. * Constructor.
  99. *
  100. * @param ContainerBuilder $container ContainerBuilder instance.
  101. */
  102. public function __construct(ContainerBuilder $container = null)
  103. {
  104. if (null !== $container) {
  105. $this->setContainer($container);
  106. }
  107. }
  108. public function setContainer(ContainerInterface $container = null)
  109. {
  110. $this->container = $container;
  111. $this->dispatcher = $this->container->get('event_dispatcher');
  112. $this->request = $this->container->get('request');
  113. $this->session = $this->request->getSession();
  114. $this->entityManager = $this->container->get('doctrine')->getEntityManager();
  115. $this->_configureBase();
  116. $this->initialize();
  117. $this->postInitialize();
  118. }
  119. /**
  120. * Configure base properties, invoked from the constructor.
  121. *
  122. * @return void
  123. */
  124. protected function _configureBase()
  125. {
  126. $this->getPath();
  127. $this->systemBaseDir = realpath('.');
  128. $class = get_class($this);
  129. $parts = strpos($class, '_') ? explode('_', $class) : explode('\\', $class);
  130. $this->name = $parts[0];
  131. $baseDir = \ModUtil::getModuleBaseDir($this->name);
  132. $this->baseDir = realpath("{$this->systemBaseDir}/$baseDir/" . $this->name);
  133. if ($baseDir == 'modules') {
  134. $this->domain = \ZLanguage::getModuleDomain($this->name);
  135. }
  136. }
  137. /**
  138. * Initialize: called from constructor.
  139. *
  140. * Intended for initialising base classes.
  141. *
  142. * @return void
  143. */
  144. protected function initialize()
  145. {
  146. }
  147. /**
  148. * Post initialise: called from constructor.
  149. *
  150. * Intended for child classes.
  151. *
  152. * @return void
  153. */
  154. protected function postInitialize()
  155. {
  156. }
  157. /**
  158. * Gets the base path of the module.
  159. *
  160. * @return string
  161. */
  162. public static function getPath()
  163. {
  164. if (null !== self::$path) {
  165. return self::$path;
  166. }
  167. $reflection = new \ReflectionClass(get_called_class());
  168. self::$path = dirname($reflection->getFileName());
  169. return self::$path;
  170. }
  171. /**
  172. * Get reflection of this object.
  173. *
  174. * @return \ReflectionObject
  175. */
  176. public function getReflection()
  177. {
  178. if (!$this->reflection) {
  179. $this->reflection = new \ReflectionObject($this);
  180. }
  181. return $this->reflection;
  182. }
  183. /**
  184. * Get entitymanager.
  185. *
  186. * @return \Doctrine\ORM\EntityManager
  187. */
  188. public function getEntityManager()
  189. {
  190. return $this->entityManager;
  191. }
  192. /**
  193. * Get translation domain.
  194. *
  195. * @return string|null
  196. */
  197. public function getDomain()
  198. {
  199. return $this->domain;
  200. }
  201. /**
  202. * Get name.
  203. *
  204. * @return $string Name.
  205. */
  206. public function getName()
  207. {
  208. return $this->name;
  209. }
  210. /**
  211. * Get the ServiceManager.
  212. *
  213. * @return ServiceManager
  214. */
  215. public function getContainer()
  216. {
  217. return $this->container;
  218. }
  219. /**
  220. * Get the EventManager.
  221. *
  222. * @return \Symfony\Component\EventDispatcher\EventDispatcher
  223. */
  224. public function getDispatcher()
  225. {
  226. return $this->dispatcher;
  227. }
  228. /**
  229. * Get module info.
  230. *
  231. * @return array
  232. */
  233. public function getModInfo()
  234. {
  235. if (!$this->modinfo) {
  236. $this->modinfo = \ModUtil::getInfoFromName($this->name);
  237. }
  238. return $this->modinfo;
  239. }
  240. /**
  241. * Get base directory of this component.
  242. *
  243. * @return string
  244. */
  245. public function getBaseDir()
  246. {
  247. return $this->baseDir;
  248. }
  249. /**
  250. * Get top basedir of the component (modules/ system/ etc)/.
  251. *
  252. * @return string
  253. */
  254. public function getSystemBaseDir()
  255. {
  256. return $this->systemBaseDir;
  257. }
  258. /**
  259. * Translate.
  260. *
  261. * @param string $msgid String to be translated.
  262. *
  263. * @return string
  264. */
  265. public function __($msgid)
  266. {
  267. return __($msgid, $this->domain);
  268. }
  269. /**
  270. * Translate with sprintf().
  271. *
  272. * @param string $msgid String to be translated.
  273. * @param string|array $params Args for sprintf().
  274. *
  275. * @return string
  276. */
  277. public function __f($msgid, $params)
  278. {
  279. return __f($msgid, $params, $this->domain);
  280. }
  281. /**
  282. * Translate plural string.
  283. *
  284. * @param string $singular Singular instance.
  285. * @param string $plural Plural instance.
  286. * @param string $count Object count.
  287. *
  288. * @return string Translated string.
  289. */
  290. public function _n($singular, $plural, $count)
  291. {
  292. return _n($singular, $plural, $count, $this->domain);
  293. }
  294. /**
  295. * Translate plural string with sprintf().
  296. *
  297. * @param string $sin Singular instance.
  298. * @param string $plu Plural instance.
  299. * @param string $n Object count.
  300. * @param string|array $params Sprintf() arguments.
  301. *
  302. * @return string
  303. */
  304. public function _fn($sin, $plu, $n, $params)
  305. {
  306. return _fn($sin, $plu, $n, $params, $this->domain);
  307. }
  308. /**
  309. * Register status message.
  310. *
  311. * Causes a status message to be stored in the session and displayed next pageload.
  312. *
  313. * @param string $message Message.
  314. *
  315. * @throws \Zikula\Framework\Exception If no message is set.
  316. *
  317. * @return object This object.
  318. */
  319. protected function registerStatus($message)
  320. {
  321. if (!isset($message) || empty($message)) {
  322. throw new \Zikula\Framework\Exception\FatalException($this->__f('Empty [%s] received.', 'message'));
  323. }
  324. \LogUtil::addStatusPopup($message);
  325. return $this;
  326. }
  327. /**
  328. * Register status message if $condition.
  329. *
  330. * Causes a status message to be stored in the session and displayed next pageload.
  331. *
  332. * @param boolean $condition Condition.
  333. * @param string $message Message.
  334. *
  335. * @throws \Zikula\Framework\Exception If no message is set.
  336. *
  337. * @return object This object.
  338. */
  339. protected function registerStatusIf($condition, $message)
  340. {
  341. if ($condition) {
  342. return $this->registerStatus($message);
  343. }
  344. return $this;
  345. }
  346. /**
  347. * Register status message if $condition.
  348. *
  349. * Causes a status message to be stored in the session and displayed next pageload.
  350. *
  351. * @param boolean $condition Condition.
  352. * @param string $message Message.
  353. *
  354. * @throws \Zikula\Framework\Exception If no message is set.
  355. *
  356. * @return object This object.
  357. */
  358. protected function registerStatusUnless($condition, $message)
  359. {
  360. if (!$condition) {
  361. return $this->registerStatus($message);
  362. }
  363. return $this;
  364. }
  365. /**
  366. * Register error message.
  367. *
  368. * Causes a error message to be stored in the session and displayed next pageload.
  369. *
  370. * @param string $message Message.
  371. * @param integer $type Type.
  372. * @param mixed $debug Debug.
  373. *
  374. * @throws \Zikula\Framework\Exception\FatalException If no message is set.
  375. *
  376. * @return object This object.
  377. */
  378. protected function registerError($message, $type=null, $debug=null)
  379. {
  380. if (!isset($message) || empty($message)) {
  381. throw new \Zikula\Framework\Exception\FatalException($this->__f('Empty [%s] received.', 'message'));
  382. }
  383. \LogUtil::addErrorPopup($message);
  384. return $this;
  385. }
  386. /**
  387. * Register error message if $condition.
  388. *
  389. * Causes a error message to be stored in the session and displayed next pageload.
  390. *
  391. * @param boolean $condition Condition.
  392. * @param string $message Message.
  393. * @param integer $type Type.
  394. * @param mixed $debug Debug.
  395. *
  396. * @throws \Zikula\Framework\Exception If no message is set.
  397. *
  398. * @return object This object.
  399. */
  400. protected function registerErrorIf($condition, $message, $type=null, $debug=null)
  401. {
  402. if ($condition) {
  403. return $this->registerError($message, $type, $debug);
  404. }
  405. return $this;
  406. }
  407. /**
  408. * Register error message if $condition.
  409. *
  410. * Causes a error message to be stored in the session and displayed next pageload.
  411. *
  412. * @param boolean $condition Condition.
  413. * @param string $message Message.
  414. * @param integer $type Type.
  415. * @param mixed $debug Debug.
  416. *
  417. * @throws \Zikula\Framework\Exception If no message is set.
  418. *
  419. * @return object This object.
  420. */
  421. protected function registerErrorUnless($condition, $message, $type=null, $debug=null)
  422. {
  423. if (!$condition) {
  424. return $this->registerError($message, $type, $debug);
  425. }
  426. return $this;
  427. }
  428. /**
  429. * Convenience Module SetVar.
  430. *
  431. * @param string $key Key.
  432. * @param mixed $value Value, default empty.
  433. *
  434. * @return object This.
  435. */
  436. public function setVar($key, $value='')
  437. {
  438. \ModUtil::setVar($this->name, $key, $value);
  439. return $this;
  440. }
  441. /**
  442. * Convenience Module SetVars.
  443. *
  444. * @param array $vars Array of key => value.
  445. *
  446. * @return object This.
  447. */
  448. public function setVars(array $vars)
  449. {
  450. \ModUtil::setVars($this->name, $vars);
  451. return $this;
  452. }
  453. /**
  454. * Convenience Module GetVar.
  455. *
  456. * @param string $key Key.
  457. * @param boolean $default Default, false if not found.
  458. *
  459. * @return mixed
  460. */
  461. public function getVar($key, $default=false)
  462. {
  463. return \ModUtil::getVar($this->name, $key, $default);
  464. }
  465. /**
  466. * Convenience Module GetVars for all keys in this module.
  467. *
  468. * @return mixed
  469. */
  470. public function getVars()
  471. {
  472. return \ModUtil::getVar($this->name);
  473. }
  474. /**
  475. * Convenience Module DelVar.
  476. *
  477. * @param string $key Key.
  478. *
  479. * @return object This.
  480. */
  481. public function delVar($key)
  482. {
  483. \ModUtil::delVar($this->name, $key);
  484. return $this;
  485. }
  486. /**
  487. * Convenience Module DelVar for all keys for this module.
  488. *
  489. * @return object This.
  490. */
  491. public function delVars()
  492. {
  493. \ModUtil::delVar($this->name);
  494. return $this;
  495. }
  496. /**
  497. * Check Csrf token.
  498. *
  499. * @param string $token The token, if not set, will pull from $_POST['csrftoken'].
  500. *
  501. * @throws \Zikula\Framework\Exception\ForbiddenException If check fails.
  502. *
  503. * @return void
  504. */
  505. public function checkCsrfToken($token=null)
  506. {
  507. if (is_null($token)) {
  508. $token = $this->request->request->get('csrftoken', false);
  509. }
  510. $tokenValidator = $this->container->get('token.validator');
  511. if (\System::getVar('sessioncsrftokenonetime') && $tokenValidator->validate($token, false, false)) {
  512. return;
  513. }
  514. if ($tokenValidator->validate($token)) {
  515. return;
  516. }
  517. // Should we expire the session also? drak.
  518. throw new \Zikula\Framework\Exception\ForbiddenException(__('Security token validation failed'));
  519. }
  520. /**
  521. * Convenience to get a service.
  522. *
  523. * @param string $id Service Name.
  524. *
  525. * @return mixed Service or null.
  526. */
  527. protected function get($id)
  528. {
  529. return $this->container->get($id);
  530. }
  531. /**
  532. * Convenience hasService shortcut.
  533. *
  534. * @param string $id Service name.
  535. *
  536. * @return boolean
  537. */
  538. protected function has($id)
  539. {
  540. return $this->container->has($id);
  541. }
  542. /**
  543. * Throw Zikula_Exception_NotFound exception.
  544. *
  545. * Used to immediately halt execution.
  546. *
  547. * @param string $message Default ''.
  548. * @param string $code Default 0.
  549. * @param string|array $debug Debug information.
  550. *
  551. * @throws \Zikula\Framework\Exception\NotFoundException Exception.
  552. *
  553. * @return void
  554. */
  555. protected function throwNotFound($message='', $code=0, $debug=null)
  556. {
  557. throw new \Zikula\Framework\Exception\NotFoundException($message, $code, $debug);
  558. }
  559. /**
  560. * Throw Zikula_Exception_NotFound exception if $condition.
  561. *
  562. * Used to immediately halt execution if $condition.
  563. *
  564. * @param bool $condition Condition.
  565. * @param string $message Default ''.
  566. * @param string $code Default 0.
  567. * @param string|array $debug Debug information.
  568. *
  569. * @throws \Zikula\Framework\Exception\NotFoundException Exception.
  570. *
  571. * @return void
  572. */
  573. protected function throwNotFoundIf($condition, $message='', $code=0, $debug=null)
  574. {
  575. if ($condition) {
  576. $this->throwNotFound($message, $code, $debug);
  577. }
  578. }
  579. /**
  580. * Throw Zikula_Exception_NotFound exception unless $condition.
  581. *
  582. * Used to immediately halt execution unless $condition.
  583. *
  584. * @param bool $condition Condition.
  585. * @param string $message Default ''.
  586. * @param string $code Default 0.
  587. * @param string|array $debug Debug information.
  588. *
  589. * @throws \Zikula\Framework\Exception\NotFoundException Exception.
  590. *
  591. * @return void
  592. */
  593. protected function throwNotFoundUnless($condition, $message='', $code=0, $debug=null)
  594. {
  595. if (!$condition) {
  596. $this->throwNotFound($message, $code, $debug);
  597. }
  598. }
  599. /**
  600. * Throw Zikula_Exception_Forbidden exception.
  601. *
  602. * Used to immediately halt execution.
  603. *
  604. * @param string $message Default ''.
  605. * @param string $code Default 0.
  606. * @param string|array $debug Debug information.
  607. *
  608. * @throws \Zikula\Framework\Exception\ForbiddenException Exception.
  609. *
  610. * @return void
  611. */
  612. protected function throwForbidden($message='', $code=0, $debug=null)
  613. {
  614. throw new \Zikula\Framework\Exception\ForbiddenException($message, $code, $debug);
  615. }
  616. /**
  617. * Throw Zikula_Exception_Forbidden exception if $condition.
  618. *
  619. * Used to immediately halt execution if condition.
  620. *
  621. * @param bool $condition Condition.
  622. * @param string $message Default ''.
  623. * @param string $code Default 0.
  624. * @param string|array $debug Debug information.
  625. *
  626. * @throws \Zikula\Framework\Exception\ForbiddenException Exception.
  627. *
  628. * @return void
  629. */
  630. protected function throwForbiddenIf($condition, $message='', $code=0, $debug=null)
  631. {
  632. if ($condition) {
  633. $this->throwForbidden($message, $code, $debug);
  634. }
  635. }
  636. /**
  637. * Throw Zikula_Exception_Forbidden exception unless $condition.
  638. *
  639. * Used to immediately halt execution unless condition.
  640. *
  641. * @param bool $condition Condition.
  642. * @param string $message Default ''.
  643. * @param string $code Default 0.
  644. * @param string|array $debug Debug information.
  645. *
  646. * @throws \Zikula\Framework\Exception\ForbiddenException Exception.
  647. *
  648. * @return void
  649. */
  650. protected function throwForbiddenUnless($condition, $message='', $code=0, $debug=null)
  651. {
  652. if (!$condition) {
  653. $this->throwForbidden($message, $code, $debug);
  654. }
  655. }
  656. /**
  657. * Cause redirect by throwing exception which passes to front controller.
  658. *
  659. * @param string $url Url to redirect to.
  660. * @param integer $type Redirect code, 302 default.
  661. *
  662. * @throws \Zikula\Framework\Exception\RedirectException Causing redirect.
  663. *
  664. * @return void
  665. */
  666. protected function redirect($url, $type = 302)
  667. {
  668. throw new \Zikula\Framework\Exception\RedirectException($url, $type);
  669. }
  670. /**
  671. * Cause redirect if $condition by throwing exception which passes to front controller.
  672. *
  673. * @param boolean $condition Condition.
  674. * @param string $url Url to redirect to.
  675. * @param integer $type Redirect code, 302 default.
  676. *
  677. * @throws \Zikula\Framework\Exception\RedirectException Causing redirect.
  678. *
  679. * @return void
  680. */
  681. protected function redirectIf($condition, $url, $type = 302)
  682. {
  683. if ($condition) {
  684. $this->redirect($url, $type);
  685. }
  686. }
  687. /**
  688. * Cause redirect unless $condition by throwing exception which passes to front controller.
  689. *
  690. * @param boolean $condition Condition.
  691. * @param string $url Url to redirect to.
  692. * @param integer $type Redirect code, 302 default.
  693. *
  694. * @throws \Zikula\Framework\Exception\RedirectException Causing redirect.
  695. *
  696. * @return void
  697. */
  698. protected function redirectUnless($condition, $url, $type = 302)
  699. {
  700. if (!$condition) {
  701. $this->redirect($url, $type);
  702. }
  703. }
  704. }