Changeset 1141
- Timestamp:
- 09/23/08 17:12:55 (2 months ago)
- Files:
-
- trunk/lib/AkActionMailer.php (modified) (5 diffs)
- trunk/lib/AkActionView.php (modified) (1 diff)
- trunk/lib/AkActionView/AkHelperLoader.php (modified) (6 diffs)
- trunk/lib/AkActionView/TemplateEngines/AkSintags/AkSintagsParser.php (modified) (1 diff)
- trunk/test/fixtures/app/models/test_mailer.php (modified) (1 diff)
- trunk/test/fixtures/app/views/test_mailer/message_with_helpers.tpl (added)
- trunk/test/unit/lib/AkActionMailer.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/AkActionMailer.php
r988 r1141 275 275 var $default_mime_version = '1.0'; 276 276 var $default_implicit_parts_order = array('multipart/alternative', 'text/html', 'text/enriched', 'text/plain'); 277 var $helpers = array('mail');278 277 var $Message; 279 278 var $Composer; … … 515 514 * class MyMailer extends AkActionMailer{ 516 515 * function receive($Message){ 517 * parent::rec ieve($Message);516 * parent::receive($Message); 518 517 * ... 519 518 * } … … 540 539 $Message->send(); 541 540 } 542 541 543 542 function getRawMessage() 544 543 { … … 573 572 function deliver($method_name, $parameters = null, $Message = null) 574 573 { 575 if(empty($Message) && 574 if(empty($Message) && 576 575 (empty($this->Message) || (!empty($this->Message) && get_class($this->Message) != get_class($this)))){ 577 576 $this->create($method_name, $parameters); … … 699 698 700 699 /** 700 * Workarround for limited support of helpers on ActionMailer Views 701 * 702 * @todo refactor helpers to be controller agnostic 703 */ 704 function getControllerName() 705 { 706 return $this->getModelName(); 707 } 708 709 /** 710 * This is the url_for version for helpers and emails. 711 * 712 * As we do not have the context of a host being requested, we need to know 713 * the base_url like http://example.com in oder to add it to the generated URL 714 */ 715 function urlFor() 716 { 717 $args = func_get_args(); 718 $base_url = ''; 719 if(isset($args[0]['base_url'])){ 720 $base_url = preg_replace('/^(?!http[s]?:\/\/)(.+)/','http://$1', (isset($args[0]['base_url'])?rtrim($args[0]['base_url'],'/'):Ak::getSetting('mailer', 'base_url', AK_HOST))); 721 unset($args[0]['base_url']); 722 } 723 724 unset($args[0]['only_path'], $args[0]['base_url']); 725 726 return $base_url.call_user_func_array(array('Ak','toUrl'), $args); 727 } 728 729 /** 701 730 * Creates an instance of each available helper and links it into into current mailer. 702 731 * 703 732 * Mailer helpers work as Controller helpers but without the Request context 704 733 */ 705 function &getHelpers() 706 { 707 static $helpers = array(); 708 require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'AkActionViewHelper.php'); 709 710 $mailer_helpers = array_merge(Ak::toArray($this->helpers), array(substr($this->getModelName(),-6))); 711 $mailer_helpers = array_unique(array_map(array('AkInflector','underscore'), $mailer_helpers)); 712 713 foreach ($mailer_helpers as $file => $mailer_helper){ 714 $full_path = preg_match('/[\\\\\/]+/',$file); 715 $helper_class_name = AkInflector::camelize($mailer_helper).'Helper'; 716 $attribute_name = (!$full_path ? AkInflector::underscore($helper_class_name) : substr($file,0,-4)); 717 if(empty($helpers[$attribute_name])){ 718 if($full_path){ 719 include_once($file); 720 }else{ 721 $helper_file_name = $mailer_helper.'_helper.php'; 722 if(file_exists(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.$helper_file_name)){ 723 include_once(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.$helper_file_name); 724 }elseif (file_exists(AK_HELPERS_DIR.DS.$helper_file_name)){ 725 include_once(AK_HELPERS_DIR.DS.$helper_file_name); 726 } 727 } 728 729 if(class_exists($helper_class_name)){ 730 if(empty($helpers[$attribute_name])){ 731 $helpers[$attribute_name] =& new $helper_class_name(&$this); 732 if(method_exists($helpers[$attribute_name],'setController')){ 733 $helpers[$attribute_name]->setController(&$this); 734 } 735 if(method_exists($helpers[$attribute_name],'setMailer')){ 736 $helpers[$attribute_name]->setMailer(&$this); 737 } 738 if(method_exists($helpers[$attribute_name],'init')){ 739 $helpers[$attribute_name]->init(); 740 } 741 } 742 } 743 } 744 } 745 746 return $helpers; 734 function getHelpers() 735 { 736 require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'AkHelperLoader.php'); 737 $HelperLoader = new AkHelperLoader(); 738 $HelperLoader->setHandler(&$this); 739 return $HelperLoader->getHelpersForMailer(); 747 740 } 748 741 trunk/lib/AkActionView.php
r977 r1141 118 118 } 119 119 } 120 121 120 /** 122 121 * Register a class that knows how to handle template files with the given trunk/lib/AkActionView/AkHelperLoader.php
r1140 r1141 1 1 <?php 2 3 require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'AkActionViewHelper.php'); 2 4 3 5 /** … … 11 13 { 12 14 var $_Controller; 15 var $_HelperInstances; 13 16 var $_Handler; 14 17 … … 37 40 * Creates an instance of each available helper and links it into into current handler. 38 41 * 39 * Per example, if a helper TextHelper is located into the file text_helper.php.42 * For example, if a helper TextHelper is located into the file text_helper.php. 40 43 * An instance is created on current controller 41 44 * at $this->text_helper. This instance is also available on the view by calling $text_helper. 42 45 * 43 46 * Helpers can be found at lib/AkActionView/helpers (this might change in a future) 44 */ 45 function instantiateHelpers() 46 { 47 $helpers = $this->getDefaultHelpers(); 48 $helpers = array_merge($helpers, $this->getApplicationHelpers()); 49 $helpers = array_merge($helpers, $this->getPluginHelpers()); 47 * 48 * Retuns an array with helper_name => HerlperInstace 49 */ 50 function &instantiateHelpers() 51 { 52 $this->instantiateHelpersAsHandlerAttributes($this->getHelperNames()); 53 $this->_storeInstantiatedHelperNames(array_keys($this->_HelperInstances)); 54 return $this->_HelperInstances; 55 } 56 57 function instantiateHelpersAsHandlerAttributes($helpers = array()) 58 { 59 foreach ($helpers as $file=>$helper){ 60 $helper_class_name = AkInflector::camelize(AkInflector::demodulize(strstr($helper, 'Helper') ? $helper : $helper.'Helper')); 61 62 if(is_int($file)){ 63 $file = AK_HELPERS_DIR.DS.AkInflector::underscore($helper_class_name).'.php'; 64 } 65 66 $full_path = preg_match('/[\\\\\/]+/',$file); 67 $file_path = $full_path ? $file : AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.$file; 68 if(is_file($file_path)){ 69 include_once($file_path); 70 } 71 72 if(class_exists($helper_class_name)){ 73 $attribute_name = $full_path ? AkInflector::underscore($helper_class_name) : substr($file,0,-4); 74 $this->_Handler->$attribute_name =& new $helper_class_name(&$this->_Handler); 75 if(method_exists($this->_Handler->$attribute_name,'setController')){ 76 $this->_Handler->$attribute_name->setController(&$this->_Handler); 77 }elseif(method_exists($this->_Handler->$attribute_name,'setMailer')){ 78 $this->_Handler->$attribute_name->setMailer(&$this->_Handler); 79 } 80 if(method_exists($this->_Handler->$attribute_name,'init')){ 81 $this->_Handler->$attribute_name->init(); 82 } 83 $this->_HelperInstances[$attribute_name] =& $this->_Handler->$attribute_name; 84 } 85 } 86 } 87 88 /** 89 * Creates an instance of each available helper and links it into into current mailer. 90 * 91 * Mailer helpers work as Controller helpers but without the Request context 92 */ 93 function getHelpersForMailer() 94 { 95 $helper_names = $this->getHelperNames(); 96 $this->instantiateHelpersAsHandlerAttributes($helper_names); 97 $this->_storeInstantiatedHelperNames(array_keys($this->_HelperInstances)); 98 return $this->_HelperInstances; 99 } 100 101 /** 102 * In order to help rendering engines to know which helpers are available 103 * we need to persit them as a static var. 104 */ 105 function _storeInstantiatedHelperNames($helpers) 106 { 107 Ak::setStaticVar('AkActionView::instantiated_helper_names', $helpers); 108 } 109 110 /** 111 * Returns an array of helper names like: 112 * 113 * array('url_helper', 'prototype_helper') 114 */ 115 function getInstantiatedHelperNames() 116 { 117 return Ak::getStaticVar('AkActionView::instantiated_helper_names'); 118 } 119 120 121 function getHelperNames() 122 { 123 $helpers = $this->getDefaultHandlerHelperNames(); 124 $helpers = array_merge($helpers, $this->getApplicationHelperNames()); 125 $helpers = array_merge($helpers, $this->getPluginHelperNames()); 50 126 51 127 if(!empty($this->_Controller)){ … … 54 130 } 55 131 56 $handler =& $this->_Handler; 57 58 require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'AkActionViewHelper.php'); 59 60 $available_helpers = array(); 61 foreach ($helpers as $file=>$helper){ 62 $helper_class_name = AkInflector::camelize(AkInflector::demodulize(strstr($helper, 'Helper') ? $helper : $helper.'Helper')); 63 $full_path = preg_match('/[\\\\\/]+/',$file); 64 $file_path = $full_path ? $file : AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.$file; 65 include_once($file_path); 66 67 if(class_exists($helper_class_name)){ 68 $attribute_name = $full_path ? AkInflector::underscore($helper_class_name) : substr($file,0,-4); 69 $available_helpers[] = $attribute_name; 70 $handler->$attribute_name =& new $helper_class_name(&$handler); 71 if(method_exists($handler->$attribute_name,'setController')){ 72 $handler->$attribute_name->setController(&$handler); 73 } 74 if(method_exists($handler->$attribute_name,'init')){ 75 $handler->$attribute_name->init(); 76 } 77 } 78 } 79 !defined('AK_ACTION_CONTROLLER_AVAILABLE_HELPERS') && define('AK_ACTION_CONTROLLER_AVAILABLE_HELPERS', join(',',$available_helpers)); 80 } 81 82 83 84 function getDefaultHelpers() 132 return $helpers; 133 } 134 135 136 function getDefaultHandlerHelperNames() 85 137 { 86 138 $handler =& $this->_Handler; … … 97 149 $handler->helpers = Ak::toArray($handler->helpers); 98 150 } 151 99 152 return $handler->helpers; 100 153 } 101 154 102 function getApplicationHelper s()155 function getApplicationHelperNames() 103 156 { 104 157 $handler =& $this->_Handler; … … 121 174 } 122 175 123 function getPluginHelper s()176 function getPluginHelperNames() 124 177 { 125 178 $handler =& $this->_Handler; trunk/lib/AkActionView/TemplateEngines/AkSintags/AkSintagsParser.php
r475 r1141 666 666 if(defined('AK_SINTAGS_AVALABLE_HELPERS')){ 667 667 $helpers = unserialize(AK_SINTAGS_AVALABLE_HELPERS); 668 }elseif (defined('AK_ACTION_CONTROLLER_AVAILABLE_HELPERS')){ 669 $underscored_helper_names = Ak::toArray(AK_ACTION_CONTROLLER_AVAILABLE_HELPERS); 670 foreach ($underscored_helper_names as $underscored_helper_name){ 671 $helper_class_name = AkInflector::camelize($underscored_helper_name); 672 if(class_exists($helper_class_name)){ 673 foreach (get_class_methods($helper_class_name) as $method_name){ 674 if($method_name[0] != '_'){ 675 $helpers[$method_name] = $underscored_helper_name; 668 }else{ 669 require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'AkHelperLoader.php'); 670 if($underscored_helper_names = AkHelperLoader::getInstantiatedHelperNames()){ 671 foreach ($underscored_helper_names as $underscored_helper_name){ 672 $helper_class_name = AkInflector::camelize($underscored_helper_name); 673 if(class_exists($helper_class_name)){ 674 foreach (get_class_methods($helper_class_name) as $method_name){ 675 if($method_name[0] != '_'){ 676 $helpers[$method_name] = $underscored_helper_name; 677 } 676 678 } 677 679 } 678 680 } 679 }680 $helpers['render'] = 'controller';681 $helpers['render_partial'] = 'controller';681 $helpers['render'] = 'controller'; 682 $helpers['render_partial'] = 'controller'; 683 } 682 684 } 683 685 $this->available_helpers = $helpers; trunk/test/fixtures/app/models/test_mailer.php
r977 r1141 331 331 )); 332 332 } 333 334 } 333 } 334 335 function message_with_helpers($recipient) 336 { 337 $this->set(array( 338 'recipients' => $recipient, 339 'subject' => "message_with_helpers", 340 'from' => "system@example.com" 341 )); 342 } 343 335 344 336 345 function &receive($raw_email) trunk/test/unit/lib/AkActionMailer.php
r988 r1141 787 787 $this->assertPattern('/==\r\n\r\n--[a-f0-9]{32}\r\nContent-Type: image\/png;/', $rendered_message, 'Two images embeded'); 788 788 } 789 789 790 790 function test_should_deliver_creating_message() 791 791 { … … 794 794 $this->assertPattern('/Subject: Alternative message from template/', $TestMailer->deliveries[0]); 795 795 } 796 797 function test_should_allow_using_text_helper_on_mail_views() 798 { 799 $TestMailer =& new TestMailer(); 800 $Message = $TestMailer->create('message_with_helpers', $this->recipient); 801 $rendered_message = $TestMailer->getRawMessage(); 802 $this->assertPattern('/<a href="http:\/\/example.com\/offers\/">Our offers<\/a>/', $rendered_message); 803 $this->assertNoPattern('/text_helper/', $rendered_message); 804 } 796 805 /**/ 797 806
