Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. <?php
  2. /**
  3. * ProductsWidget Class file.
  4. * @author Alin M. Gheorghe - Refactorer extraordinaire
  5. *
  6. * This class is primarily used as an extension for widgets that have many render types.
  7. * It uses the __call magic method in order to invoke inaccessible methods in an object context and try to resolve them.
  8. * In our case we try to resolve them by checking if our renderType is in the renderType array, append the
  9. * RENDER_METHOD_SUFFIX constant in order to avoid conflicts with Yii abstractions and try to render that view type.
  10. *
  11. * It is important not to forget to return parent::_call() in order to keep Yii abstractions working.
  12. *
  13. * @property array $renderTypes The render types array which are the same name as the views.
  14. */
  15.  
  16. class CsWidget extends CWidget
  17. {
  18. const RENDER_METHOD_SUFFIX = 'Render';
  19.  
  20. protected $renderTypes = [];
  21.  
  22. public function init()
  23. {
  24. array_walk($this->renderTypes, function (&$typeName) { $typeName .= self::RENDER_METHOD_SUFFIX; });
  25. }
  26.  
  27.  
  28. public function __call($name, $arguments)
  29. {
  30. if (in_array($name, $this->renderTypes)) {
  31. return $this->render(str_replace(self::RENDER_METHOD_SUFFIX, '', $name));
  32. } else {
  33. return parent::__call($name, $arguments);
  34. }
  35. }
  36.  
  37. /** TODO experimental to further abstract and get rid of renderTypes array, etc.
  38. * Checks if given view filename exists for 'this' controller.
  39. * @param string $view_filename basename of the view filename, with no 'file extension' (just as you'd pass to 'render()')
  40. * @return bool exists or not.
  41. */
  42. /*public function isViewFileExists($view_filename) {
  43. if (!is_readable($this->getViewPath() . '/' . $view_filename . '.php')) {
  44. return false;
  45. }
  46. return true;
  47. }*/
  48.  
  49. /**
  50. * Renders a view with a layout.
  51. * This method overrides parent one to introduce check on the view file and throw a 404 in case it doesn't.
  52. * For complete documentation of this method please see parent implementation.
  53. *
  54. * @param string $view name of the view to be rendered. See {@link getViewFile} for details
  55. * about how the view script is resolved.
  56. * @param array $data data to be extracted into PHP variables and made available to the view script
  57. * @param boolean $return whether the rendering result should be returned instead of being displayed to end users.
  58. * @return string the rendering result. Null if the rendering result is not required.
  59. * @throws CHttpException
  60. */
  61. /*public function render($view, $data = null, $return = false) {
  62. if (!$this->isViewFileExists($view)) {
  63. Yii::log("Error: view file doesn't exists: " . $this->getViewPath() . '/' . $view, CLogger::LEVEL_ERROR, __METHOD__);
  64. throw new CHttpException(404);
  65. }
  66. return parent::render($view, $data, $return);
  67. }*/
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement