Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Miscellany
Saturday, June 9th, 2007 at 5:24:41pm MDT 

  1. <?php
  2. /**
  3. * Assign.php
  4. *
  5. * @category   Naneau
  6. * @package    Naneau_Action_Helper
  7. * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
  8. * @version    0.2
  9. */
  10.  
  11. /**
  12. * action helper base class
  13. */
  14. require_once 'Zend/Controller/Action/Helper/Abstract.php';
  15.  
  16. /**
  17. * Naneau_Action_Helper_Assign
  18. *
  19. *
  20. * Action helper for assigining fields to the action controllers. Has built in
  21. * option to assign all action helpers as fields.
  22. *
  23. * @category   Naneau
  24. * @package    Naneau_Action_Helper
  25. * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
  26. */
  27. class Naneau_Action_Helper_Assign extends Zend_Controller_Action_Helper_Abstract {
  28.  
  29.     /**
  30.      * use underscore?
  31.      *
  32.      * Using underscores is the proper way of doing it, given the coding
  33.      * standard for the Zend Framework.
  34.      *
  35.      * @var bool
  36.      */
  37.     private $_useUnderscore = true;
  38.  
  39.     /**
  40.      * assign all helpers in preDispatch?
  41.      *
  42.      * @var bool
  43.      */
  44.     private $_autoFindHelpers = true;
  45.  
  46.     /**
  47.      * array of plugins
  48.      *
  49.      * @var Zend_Controller_Action_Helper_Abstract[]
  50.      */
  51.     private $_plugins = array();
  52.  
  53.     /**
  54.      * constructor
  55.      *
  56.      * @see assign()
  57.      * @param bool $autoFindHelpers automatically assign _all_ action helpers
  58.      * @param bool $useUnderscore use an underscore
  59.      */
  60.     public function __construct($autoFindHelpers = true, $useUnderscore = true) {
  61.         $this->_autoFind = $autoFind;
  62.         $this->_useUnderscore = $useUnderscore;
  63.     }
  64.  
  65.     /**
  66.      * on every dispatch assign helpers
  67.      * @return void
  68.      */
  69.     public function preDispatch() {
  70.         if ($this->_autoFindHelpers) {
  71.             //find all helpers
  72.             foreach (Zend_Controller_Action_HelperBroker::getExistingHelpers() as $helper) {
  73.                 $name = $helper->getName();
  74.                 $this->assign($name, $helper);
  75.             }
  76.         }
  77.  
  78.         $controller = $this->getActionController();
  79.         //current action controller
  80.         foreach($this->_plugins as $name => $plugin) {
  81.             $controller->$name = $plugin;
  82.         }
  83.     }
  84.  
  85.     /**
  86.      * add an object to be added
  87.      *
  88.      * @param string $name
  89.      * @param object $obj
  90.      * @return void
  91.      */
  92.     public function assign($name, $obj)
  93.     {
  94.         if ($this->_useUnderscore) {
  95.             $name = '_'ltrim($name, '_');
  96.             //remove any possible underscores that are there and add one
  97.         }
  98.  
  99.         $this->_plugins[$name] = $obj;
  100.         return $this;
  101.         //return self for chaining
  102.     }
  103. }

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right
fantasy-obligation