PHPBelgium Arteveldehogeschool Campus Mariakerke meeting 20-08-2008 Extending Zend Framework
Why Zend Framework ? most adopted framework by enterprises (new media) start-ups governments it's growing community simple approach for complicated tasks
Components in ZF
Why extending components ? no rewrite or re-invention of the wheel re-use functionality that's already made modify only the action you need rely on the framework better code management when upgrading ZF when upgrading own code-base
Setting up your library Having Zend Framework in /approot/library/Zend Your own library should be in /approot/library/Mycomp
Example Extending the Zend_Translator adapter to use a database backend extending Zend_Translate_Adapter
Optimizing own library When extending Zend Framework use same directory structure as ZF approot/ Mycomp/ Translate/ Adapter/ Db.php Zend/ ..
Understand the component Zend_Translate loads all translations for a locale multiple locales can be added can be used with caching interacts with other components (e.g. Zend_Form) extends on Zend_Translate_Adapter
How do we start ? <?php /** * @see Zend_Translate_Adapter */ require_once 'Zend/Translate/Adapter.php'; /** * MyComp_Translate_Adapter_Db * Translation adapter for using a database back-end * */ class Mycomp_Translate_Adapter_Db extends Zend_Translate_Adapter { }
load from database /** * Loads the translation data into the Adapter * @param string|array $data – DB related settings *  - dbAdapter: adapter to connect with DB *  - tableName: storage of the translation *  - options: filter on the translations * @param string $locale – The locale this translation is for * @param array $options – general adapter options */ protected function _loadTranslationData($data, $locale, $options) { ... }
our custom logic // we create a select statement first $select = $options['dbAdapter']->select(); $select->from($data['tableName'], array($data['keyField'], $data['valueField'])) ->where('language = ?', $locale); $stmt = $select->query(); // we store our whole translation into $trData $trData = $stmt->fetchAll(); // we create a scheme to generate a Zend_Translate_Array type $trScheme = array (); if (!empty ($trData)) { foreach ($trData as $trEl) { $trScheme[$trEl[$data['keyField']]] = $trEl[$data['valueField']]; } }
putting it all back // we put everything back into data so it can be handled properly  // by the translation adapter. $data = $trScheme; // we need to check if we don't call a clean up routine $options = array_merge($this->_options, $options); if (($options['clear'] == true) ||  !isset($this->_translate[$locale])) { $this->_translate[$locale] = array(); } // now we merge our data back to the translate adapter $this->_translate[$locale] = array_merge($this->_translate[$locale], $data); /**  * NOTE: $this->_options and $this->_translate are properties *  from the parent class Zend_Translate_Adapter */
finishing touch /** * returns the adapters name * * @return string */ public function toString() { return &quot;Db&quot;; }
using the new adapter // Setting up the translation parameters $data = array( 'dbAdapter'  => $dbAdapter, // already set in the bootstrap 'tableName' => 'translation', 'localeField' => 'language', 'keyField' => 'key', 'valueField' => 'value', ); $options = array (); try { $translate = new Zend_Translate('MyComp_Translate_Adapter_Db', $data, 'en_US', $options); $translate->addTranslation($data, 'nl_BE', $options); $translate->addTranslation($data, 'fr_BE', $options); } catch (Zend_Translate_Exception $zte) { echo “<h1>” . $zte->getMessage() . “</h1>” . PHP_EOL; }
convenient view helper class MyComp_View_Helper_Tr { protected $_view; public function setView(Zend_View_Interface $view) { $this->_view = $view; } }
helper magic public function Tr($label) { $default = &quot;en_US&quot;; $string = null; if (Zend_Register::isRegistered('translator')) { $translator = Zend_Register::get('translator'); if ($translator->isTranslated($label)) { $string = $translator->_($label); } else { $translator->setLocale(new Zend_Locale($default)); $string = $translator->_($label); } } else { $string = $label; } return $string; }
Summary Extending ZF compontents modify the bare minimum follow general guidelines comment your code as much as possible put your extension in YOUR library Can also be applied to general classes
Thank you... Questions ?
Support our cause PHPBelgium needs your help suggest topics for next meetings give a presentation at our meetings interact with us on  irc (freenode) #php_bnl twitter.com/PHPBelgium small donations are welcome :-p

Extending Zend Framework

  • 1.
    PHPBelgium Arteveldehogeschool CampusMariakerke meeting 20-08-2008 Extending Zend Framework
  • 2.
    Why Zend Framework? most adopted framework by enterprises (new media) start-ups governments it's growing community simple approach for complicated tasks
  • 3.
  • 4.
    Why extending components? no rewrite or re-invention of the wheel re-use functionality that's already made modify only the action you need rely on the framework better code management when upgrading ZF when upgrading own code-base
  • 5.
    Setting up yourlibrary Having Zend Framework in /approot/library/Zend Your own library should be in /approot/library/Mycomp
  • 6.
    Example Extending theZend_Translator adapter to use a database backend extending Zend_Translate_Adapter
  • 7.
    Optimizing own libraryWhen extending Zend Framework use same directory structure as ZF approot/ Mycomp/ Translate/ Adapter/ Db.php Zend/ ..
  • 8.
    Understand the componentZend_Translate loads all translations for a locale multiple locales can be added can be used with caching interacts with other components (e.g. Zend_Form) extends on Zend_Translate_Adapter
  • 9.
    How do westart ? <?php /** * @see Zend_Translate_Adapter */ require_once 'Zend/Translate/Adapter.php'; /** * MyComp_Translate_Adapter_Db * Translation adapter for using a database back-end * */ class Mycomp_Translate_Adapter_Db extends Zend_Translate_Adapter { }
  • 10.
    load from database/** * Loads the translation data into the Adapter * @param string|array $data – DB related settings * - dbAdapter: adapter to connect with DB * - tableName: storage of the translation * - options: filter on the translations * @param string $locale – The locale this translation is for * @param array $options – general adapter options */ protected function _loadTranslationData($data, $locale, $options) { ... }
  • 11.
    our custom logic// we create a select statement first $select = $options['dbAdapter']->select(); $select->from($data['tableName'], array($data['keyField'], $data['valueField'])) ->where('language = ?', $locale); $stmt = $select->query(); // we store our whole translation into $trData $trData = $stmt->fetchAll(); // we create a scheme to generate a Zend_Translate_Array type $trScheme = array (); if (!empty ($trData)) { foreach ($trData as $trEl) { $trScheme[$trEl[$data['keyField']]] = $trEl[$data['valueField']]; } }
  • 12.
    putting it allback // we put everything back into data so it can be handled properly // by the translation adapter. $data = $trScheme; // we need to check if we don't call a clean up routine $options = array_merge($this->_options, $options); if (($options['clear'] == true) || !isset($this->_translate[$locale])) { $this->_translate[$locale] = array(); } // now we merge our data back to the translate adapter $this->_translate[$locale] = array_merge($this->_translate[$locale], $data); /** * NOTE: $this->_options and $this->_translate are properties * from the parent class Zend_Translate_Adapter */
  • 13.
    finishing touch /*** returns the adapters name * * @return string */ public function toString() { return &quot;Db&quot;; }
  • 14.
    using the newadapter // Setting up the translation parameters $data = array( 'dbAdapter' => $dbAdapter, // already set in the bootstrap 'tableName' => 'translation', 'localeField' => 'language', 'keyField' => 'key', 'valueField' => 'value', ); $options = array (); try { $translate = new Zend_Translate('MyComp_Translate_Adapter_Db', $data, 'en_US', $options); $translate->addTranslation($data, 'nl_BE', $options); $translate->addTranslation($data, 'fr_BE', $options); } catch (Zend_Translate_Exception $zte) { echo “<h1>” . $zte->getMessage() . “</h1>” . PHP_EOL; }
  • 15.
    convenient view helperclass MyComp_View_Helper_Tr { protected $_view; public function setView(Zend_View_Interface $view) { $this->_view = $view; } }
  • 16.
    helper magic publicfunction Tr($label) { $default = &quot;en_US&quot;; $string = null; if (Zend_Register::isRegistered('translator')) { $translator = Zend_Register::get('translator'); if ($translator->isTranslated($label)) { $string = $translator->_($label); } else { $translator->setLocale(new Zend_Locale($default)); $string = $translator->_($label); } } else { $string = $label; } return $string; }
  • 17.
    Summary Extending ZFcompontents modify the bare minimum follow general guidelines comment your code as much as possible put your extension in YOUR library Can also be applied to general classes
  • 18.
  • 19.
    Support our causePHPBelgium needs your help suggest topics for next meetings give a presentation at our meetings interact with us on irc (freenode) #php_bnl twitter.com/PHPBelgium small donations are welcome :-p