plugins and making your own
DESCRIPTION
Presentation given on symfonycamp 2008TRANSCRIPT
Plugins and making your own
Lambert BeekhuisLead developer at DOP
Why plugins?
• Re-use common functionality– For yourself...in multiple projects– Or better...for the symfony community• Sharing/learning
– How should I build a Plugin?
Plugins as of today(ok, Thursday 11th of september)
symfony 1.0 symfony 1.1 symfony 1.2
All 231 123 60
Bound to Propel 75 39 19
Bound to Doctrine 6 2 1
Stats269 plugins688 releases134 developers4104 users
Popular pluginsPopular Plugins on 2007/08/14top-20-symfony-plugins
(symfony-project blog top-20-symfony-plugins)
• 1. sfSimpleCMSPlugin• 2. sfGuardPlugin• 3. sfDoctrinePlugin• 4. sfPropelActAsNestedSetBehaviorPlugin• 5. sfMediaLibraryPlugin• 6. sfSimpleBlogPlugin• 7. sfSimpleForumPlugin• 8. sfFeed2Plugin• 9. sfgWidgetsPlugin• 10. sfThumbnailPlugin• 11. sfUJSPlugin• 12. sfControlPanelPlugin• 13. sfFormValidationPlugin• 14. sfMogileFSPlugin• 15. sfLightboxPlugin• 16. sfCaptchaPlugin• 17. sfNiftyPlugin• 18. sfPrototypeWindowPlugin• 19. sfPropelLoadbalancerPlugin• 20. sfSavvyPlugin• 21. sfYzClientSideValidationPlugin
Today (2009/09/11)• sfGuardPlugin
– Authorisation• sfSimpleCMSPlugin
– Content Management• sfDoctrinePlugin
– Doctrine ORM• sfPropelActAsNestedSetBehaviorPlugin
– Propel-trees• sfLucenePlugin
– Searching• sfThumbnailPlugin
– Thumbnails/resizing• sfMediaLibraryPlugin
– Manage web-assets (images, PDF, flash objects)• sfExtjs2Plugin
– Possible to write external JavaScript
Top down development
‘it works’ is not enough! Good structure!
Developing cycles
Development - cycle• Idea or requirements• Design/principle/philosofie• Coding• Testing• Documentation• Maintenance
Comments.... Fun part Prior to coding or ...? Lot of focus (to much?) Do you test systematically? Essential! often ‘forgotten’! Not a fun part...but with the
community is much better!
Talking as a consultant....
Urgent Not urgent
Important
Not important
Used for development
Urgent Not urgent
Important ‘It works’Debugging
Software structureDocumentationRefactoring
Not important Features never used
Plugins
Make a plugin:• Independant of your
application• Upgradable• Customizable
PHPPHP
Symfony
Database
ORM
Your Application
plugin
plugin
customize
customize
plugin
customize
Plugin - basicsA plugin can contains all options grouped, (similar to in your application)• Config
– Config.php• Data
– Fixtures• Lib
– Helper/– Model/
schema.yml:propel: _attributes: { package plugins.lbSaasPlugin.lib.model }
– Others (e.g. Filters)• Modules
– Actions– Templates– Validate– Config
• Module.yml• View.yml
Enable a plugin in your application
• Settings.yml
• Application-config/app.yml
• New in symfony 1.1– Handle plugin dependencies at installation– Customizing the plugin-schema
all: lb_saas_plugin: enable_saas_filter: on # switch the extra Saas-filter on/off saas_profile_class: Client # PHP-object name for Saas-object saas_profile_field_name: saas_id
all: .settings: enabled_modules: [default, sfGuardAuth]
Installing a plugin - basics
symfony 1.0• Install a plugin
– > cd myproject > php symfony plugin-install channelName/pluginName > php symfony cc
• Enable modules in your application:– In: apps/myApplication/config/settings.yml– All:
.settings: enabled_modules:
[ yourPluginModule]
• Follow additional instructions from readme
• Clear cache– > php symfony cc
symfony 1.1• Install a plugin
– > cd myproject > php symfony plugin:install pluginName
Based on the ‘SaasPlugin’• ‘Software as a Service’ authentication
– User has access to one or more Saas-en– Extension on sfGuardPlugin
• Clean URLs e.g. www.application.com\saasOne\module\action(after yesterday: saasOne.application.com\module\action)
• Administration interfaces for – Application admin – ‘saasAdmin’
• Limit data-retrieval for the selected Saas (based on Propel right now)
Customizing a Plugin• Rule– never alter the code found in the plugins/ directory
• use settings – that can be changed in the application app.yml
• Replace functions in your application– create a module of the same name in your own
application.• Allow replacing– Create ‘empty’ actions/models in your plugin, e.g.
• Actions• Model
Replacing model functions
plugins/xxxPlugin/lib/model/om/BasexxxObject.php
plugins/xxxPlugin/lib/model/map/xxxObjectMapBuilder.php
plugins/xxxPlugin/lib/model/xxxObject.php
plugins/xxxPlugin/lib/model/om/BasexxxObject.php
plugins/xxxPlugin/lib/model/map/xxxObjectMapBuilder.php
plugins/xxxPlugin/lib/model/xxxObject.php
plugins/xxxPlugin/lib/model/plugin/PluginxxxObject.php
class pluginxxxObject extends BasexxxObject{ // your plugin functions}
class xxxObject extends pluginxxxObject{ // empty}
Generated by ORM (propel)(do not touch)
Your functions
Can be replaced in your application- module
Replacing plugin functions
• Add empty module in your application– Add a template in this module– Add security.yml in your application– Add actions (requires proper plugin setup)
• Add model-classes in your application– Requires proper plugin-setup
Other ways – routing/filters/...
• Routing ‘injection’, e.g. in ‘user’public static function add_default_saas_routes{ $r = sfRouting::getInstance(); $r->prependRoute('saas_default', /:clientslug/s/:module/:action/*');
• Use an additional filter for global functions(e.g. Filters the URL and checks against allowed Saas)
Your appliction/config/filters.yml
rendering: ~web_debug: ~
saas: class: saasFilter condition: %APP_LB_SAAS_PLUGIN_ENABLE_SAAS_FILTER% security: ~
# generally, you will want to insert your own filters here
cache: ~common: ~
flash: ~execution: ~
Deploying plugins
For own purpose• Use SVN:external for use in
multiple projects
Publish as symfony-project.org• Define the new Plugin at
www.symfony-project.org/plugin/new• Naming conventions
– Propel dependency• E.g sfPropelActAsNestedSetBehaviorPlugin
– First two letters/initials can be your initials (or just use sf)• E.g. dwPrototypeTooltipPlugin
• Define a proper readme!• Select a license• add a package.xml file at the root of the
plug-in directory • Upload your PEAR package
Plugins - conclusions
• Powerfull way to extend the framework functions
• Many plugins available– Usefullness and quality depends– As of today, not easy to find usefullness and
quality symfony-project.org
• Many options for customization– If Plugin is developed correctly!