oscon 2011 - making your php application easy to customize
TRANSCRIPT
Making Your PHP Application Easy to Customize
John Mertic
@2010 SugarCRM Inc. All rights reserved.
Who Am I?
John Mertichttp://jmertic.wordpress.comTwitter: @[email protected] ( Work )[email protected] ( PHP )Community Manager for SugarCRM
http://www.sugarcrm.comRead our blog at http://developers.sugarcrm.com/wordpress
3/24/2011 @2011 SugarCRM Inc. All rights reserved. 2
My books
3/24/2011 @2011 SugarCRM Inc. All rights reserved. 3
http://amzn.to/enioPVhttp://t.co/UFRHNSO
7/22/2010 @2010 SugarCRM Inc. All rights reserved.
Why should my app be easy to customize?
4Source http://www.flickr.com/photos/duncan/4782911809
So how should you do this?
7/22/2010 @2010 SugarCRM Inc. All rights reserved. 6
Make your code open
Source http://www.flickr.com/photos/igalko/4502271194
7/22/2010 @2010 SugarCRM Inc. All rights reserved. 7
Starting off with a good file structure
Things good to do here
File naming conventionZend Framework approach
Class Zend_Db_Table is at "Zend/Db/Table.php”Abstract class Zend_Controller_Request_Abstract is at “Zend/Controller/Request/Abstract.php”Interface Zend_Validate_Interface is at “Zend/Validate/Interface.php”
Allowing file/class overridesSugarCRM approach
Can drop in a replacement file in the same location in the custom/ directoryExample: Override Contact’s detail view by dropping in a view.detail.php file in the custom/modules/Contacts/views/ directory.
04/13/2023 @2010 SugarCRM Inc. All rights reserved. 8
7/22/2010 @2010 SugarCRM Inc. All rights reserved. 9Source http://www.flickr.com/photos/cype_applejuice/43750657
Code Design
Dependency Injection
class Widgets{ protected $_dbConn; public function __construct() { $this->_dbConn = DBFactory::getInstance(); }}
7/22/2010 @2010 SugarCRM Inc. All rights reserved. 10
Dependency Injection
class Widgets{ protected $_dbConn; public function __construct( DBInstance $dbConn ) { $this->_dbConn = $dbConn; }}
7/22/2010 @2010 SugarCRM Inc. All rights reserved. 11
Dependency Injection
class Widgets{ protected $_dbConn; public function __construct() { } public function setDBConnection(
DBInstance $dbConn ) { $this->_dbConn = $dbConn; }}
7/22/2010 @2010 SugarCRM Inc. All rights reserved. 12
Interfaces and Abstracts
interface LoggerTemplate{ /** * Main method for handling logging a message to the logger * * @param string $level logging level for the message * @param string $message */ public function log( $method, $message );}
7/22/2010 @2010 SugarCRM Inc. All rights reserved. 13
Interfaces and Abstracts
abstract class loc_xml extends source{ public function __parse($file) { $contents = file_get_contents($file); return simplexml_load_string($contents); } public abstract function getItem(
$args=array(), $module=null);
public abstract function getList($args=array(), $module=null);
}
7/22/2010 @2010 SugarCRM Inc. All rights reserved. 14
Factory Loaders
class ControllerFactory{
/** * Obtain an instance of the correct controller. * * @return an instance of SugarController */function getController($module){
$class = ucfirst($module).'Controller';$customClass = 'Custom' . $class;if(file_exists('custom/modules/'.$module.'/controller.php')){
$customClass = 'Custom' . $class;require_once('custom/modules/'.$module.'/
controller.php');if(class_exists($customClass)){
$controller = new $customClass();}else if(class_exists($class)){
$controller = new $class();}
7/22/2010 @2010 SugarCRM Inc. All rights reserved. 15
Factory Loaders
}elseif(file_exists('modules/'.$module.'/controller.php')){
require_once('modules/'.$module.'/controller.php');if(class_exists($customClass)){
$controller = new $customClass();}else if(class_exists($class)){
$controller = new $class();}
}else{if(file_exists('custom/include/MVC/Controller/
SugarController.php')){require_once('custom/include/MVC/Controller/
SugarController.php');}if(class_exists('CustomSugarController')){
$controller = new CustomSugarController();}else{$controller = new SugarController();}
}$controller->setup($module);return $controller;
}}
7/22/2010 @2010 SugarCRM Inc. All rights reserved. 16
7/22/2010 @2010 SugarCRM Inc. All rights reserved. 17
Document it!
Source: http://www.flickr.com/photos/nicecupoftea/3218211407
Ways to do this
Docblock commentsExample codeShort blog posts/articlesForums / Mailing ListWikiArchitectural documentationFull blown developer guide
7/22/2010 @2010 SugarCRM Inc. All rights reserved. 18
7/22/2010 @2010 SugarCRM Inc. All rights reserved. 19Source: http://www.flickr.com/photos/code_martial/4145914957
Learn from your developers
7/22/2010 @2010 SugarCRM Inc. All rights reserved. 20
Questions?
To contact me after my presentation, text 2OR to INTRO (46876)