Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * ProductsWidget Class file.
- * @author Alin M. Gheorghe - Refactorer extraordinaire
- *
- * This class is primarily used as an extension for widgets that have many render types.
- * It uses the __call magic method in order to invoke inaccessible methods in an object context and try to resolve them.
- * In our case we try to resolve them by checking if our renderType is in the renderType array, append the
- * RENDER_METHOD_SUFFIX constant in order to avoid conflicts with Yii abstractions and try to render that view type.
- *
- * It is important not to forget to return parent::_call() in order to keep Yii abstractions working.
- *
- * @property array $renderTypes The render types array which are the same name as the views.
- */
- class CsWidget extends CWidget
- {
- const RENDER_METHOD_SUFFIX = 'Render';
- protected $renderTypes = [];
- public function init()
- {
- array_walk($this->renderTypes, function (&$typeName) { $typeName .= self::RENDER_METHOD_SUFFIX; });
- }
- public function __call($name, $arguments)
- {
- if (in_array($name, $this->renderTypes)) {
- return $this->render(str_replace(self::RENDER_METHOD_SUFFIX, '', $name));
- } else {
- return parent::__call($name, $arguments);
- }
- }
- /** TODO experimental to further abstract and get rid of renderTypes array, etc.
- * Checks if given view filename exists for 'this' controller.
- * @param string $view_filename basename of the view filename, with no 'file extension' (just as you'd pass to 'render()')
- * @return bool exists or not.
- */
- /*public function isViewFileExists($view_filename) {
- if (!is_readable($this->getViewPath() . '/' . $view_filename . '.php')) {
- return false;
- }
- return true;
- }*/
- /**
- * Renders a view with a layout.
- * This method overrides parent one to introduce check on the view file and throw a 404 in case it doesn't.
- * For complete documentation of this method please see parent implementation.
- *
- * @param string $view name of the view to be rendered. See {@link getViewFile} for details
- * about how the view script is resolved.
- * @param array $data data to be extracted into PHP variables and made available to the view script
- * @param boolean $return whether the rendering result should be returned instead of being displayed to end users.
- * @return string the rendering result. Null if the rendering result is not required.
- * @throws CHttpException
- */
- /*public function render($view, $data = null, $return = false) {
- if (!$this->isViewFileExists($view)) {
- Yii::log("Error: view file doesn't exists: " . $this->getViewPath() . '/' . $view, CLogger::LEVEL_ERROR, __METHOD__);
- throw new CHttpException(404);
- }
- return parent::render($view, $data, $return);
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement