cakephpstudy5 hacks
DESCRIPTION
TRANSCRIPT
コアライブラリのエレガントなハック
第 5 回 CakePHP 勉強会@Tokyo
ON 2010/5/29( 土 )
Files
http://bit.ly/cakestudy_hiromi
About me Name: 清水 紘己( Shimizu Hiroki ) Call me hiromi ! ( @hiromi2424 ) Action about CakePHP
TranslationNew features reportTransitionComponentFew Patches/Tickets
I love singing, Ramen and Beer!
INDEX Introduction Hacks
ComponentsHelpersModelsCakeRouteEngines
Introduction
Why hack?
Embedding Application Policy
Why hack?
Deep Debugging
Why hack?
For Stub (Mock object)
How hack? Hook object mapping like alias
This session focuses this Provided hook
Userland import App/libs/* Copy the file from Core to App Replace, done
Dirty Hack
Impossible/Non-elegance Static Classes Basic functions
Use Lithium
Hacks
Hack components
Components - ACL Provided hook Configure::write('Acl.classname‘, ‘MyAcl’) MyAclComponent extends AclBase Implements methods
… lots of.
Components - Others Replaces mapped objects Component::_loaded[$key]
Reference to object Hook timing
Initialize()
Components - Others
init() initialize() startup()
Configuration
Component class flow
Mapping objects
Startingprocess
Hook
Components - Others
Auth::__construct() Auth::initialize()
$loaded[‘Session’] Existent
Existent$loaded[‘Auth’] Undefined
$loaded[‘Security’] Undefined
Session initialized NO YES
Auth initialized NO Initializing now
Security initialized NO NO
var $components = (‘Session’, ’Auth’, ‘Security’);
Components - Others $Component->enabled = boolean SessionComponent::__construct($base) Workaround double initialized Import original settings
AliasComponent
Hack helpers
Helpers Replaces mapped objects $view::loaded[$key]
Reference to object Hook timing
beforeRender() View::_loadHelpers()
AliasHelper
Hacks Models
Models ClassRegistry::init() alias option Example
ClassRegistry::init(array(‘class’ => ‘MockPost’,‘alias’ => ‘Post’,
)) Initialization timing
Before Controller::constructClasses
Hacks CakeRoute
CakeRoute From 1.3 (Plugin | App)/libs/routes/my_route.php App::import(‘Lib’, ‘routes/MyRoute’) Router::connect(…, …, array(‘routeClass’ => ‘MyRoute’))
class MyRoute extends CakeRoute Implement methods
parse()match()ETC…
CakeRoute parse()
Parsing given URL to parameters.Helping additional parameters
CakeRoute match()
Convert parameters to proper URL.Reverse RoutingHelping generation of URL
CakeRoute Example
Mark Story wrote great example on his BLOG I added reverse routing
Engines
Cache (Plugin | App)/libs/cache/my_cache.php Cache::config(‘default’, array(‘engine’ => MyCache))
class MyCache extends CacheEngine Implement methods
write()read()delete()
CakeLog From 1.3 (Plugin | App)/libs/log/my_logger.php CakeLog::config(‘my’, array(‘engine’ => MyCache))
class MyLogger Implement methods
write() only Example
if($type == ‘error’) file logging and mail to admin
Conclusion
Hacks rarely needed? CakePHP is realy needed? Learn Possible/Impossible CakePHP2.0 features -> alias