Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Why you shouldn’t
edit SilverStripe
core files
(and how to do it anyway)
By Loz Calver / @kinglozzer
What do you mean “editing
core files”?
Modifying someone else’s code
Code outside the scope of “your project”
Applies to f...
Why would I edit core files?
Bugs - SilverStripe isn’t (quite) perfect
Security
need urgent/immediate fix
email us!
Missin...
Why shouldn’t I edit core files?
Other developers don’t know what you’ve done
Updates are harder
(you should all be keepin...
How can I avoid editing core?
Workaround 1: Extensions
Example 1: adding database fields & extra methods
MyMemberExtension.php
<?php
class MyMemberExten...
Workaround 1: Extensions
Example 2: augmenting database queries
BlogPostFilter.php
<?php
class BlogPostFilter extends Data...
Workaround 1: Extensions
Pros
Can be used to add things to built-in classes
Reusable: package them up, release them as mod...
Workaround 2: Injector
Example: adding an extension hook
ManyManyListWithHook.php
<?php
class ManyManyListWithHook extends...
Credit to / module@camspiers silverstripe-cacheinclude
Workaround 2: Injector
Example: using the extension hook
MemberExtension.php
<?php
class MemberExtension extends Extension...
Workaround 2: Injector
Pros
Can “replace” an entire class
Super powerful
Cons/Caveats
With great power comes great respons...
When all else fails, fork it!
Create a fork, apply your fix
Use Git to periodically rebase from “upstream”
Composer helps ...
Example
composer.json
{
"require": {
"silverstripe/cms": "3.1.15",
"silverstripe/framework": "3.1.15"
},
"repositories": [...
Example
composer.json
{
"require": {
"silverstripe/cms": "3.1.15",
"silverstripe/framework": "dev-hotfix"
},
"repositories...
Not quite...
Fixed!
composer.json
{
"require": {
"silverstripe/cms": "3.1.15",
"silverstripe/framework": "dev-hotfix"
},
"repositories"...
Fixed!
composer.json
{
"require": {
"silverstripe/cms": "3.1.15",
"silverstripe/framework": "dev-hotfix as 3.1.15"
},
"rep...
Forking
Pros
Easier to:
keep track of modifications made
re-apply changes during updates - rebase/cherry-pick
create a PR!...
Summary
Use extensions where possible
For simple additions/fixes to methods, Injector should be okay
For anything else you...
Thanks/questions
Feedback welcomed!
@kinglozzer @kinglozzer lozcalver@bigfork.co.uk
Why you shouldn’t edit silver stripe core files (and how to do it anyway)
Upcoming SlideShare
Loading in …5
×

Why you shouldn’t edit silver stripe core files (and how to do it anyway)

503 views

Published on

Slides from StripeCon15 - London, 16th October 2015

Published in: Software
  • Be the first to comment

  • Be the first to like this

Why you shouldn’t edit silver stripe core files (and how to do it anyway)

  1. 1. Why you shouldn’t edit SilverStripe core files (and how to do it anyway) By Loz Calver / @kinglozzer
  2. 2. What do you mean “editing core files”? Modifying someone else’s code Code outside the scope of “your project” Applies to framework, cms and any other modules
  3. 3. Why would I edit core files? Bugs - SilverStripe isn’t (quite) perfect Security need urgent/immediate fix email us! Missing/upcoming features - time delay between minor versions Clients! security@silverstripe.org
  4. 4. Why shouldn’t I edit core files? Other developers don’t know what you’ve done Updates are harder (you should all be keeping up to date!) eventually you’ll get bored or frustrated and give up You might accidentally introduce other issues
  5. 5. How can I avoid editing core?
  6. 6. Workaround 1: Extensions Example 1: adding database fields & extra methods MyMemberExtension.php <?php class MyMemberExtension extends DataExtension { private static $db = array( 'DateOfBirth' => 'SS_Datetime' ); public function SayHi() { return "Hi " . $this->owner->Name; } } _config.yml Member: extensions: - MyMemberExtension
  7. 7. Workaround 1: Extensions Example 2: augmenting database queries BlogPostFilter.php <?php class BlogPostFilter extends DataExtension { public function augmentSQL(SQLQuery &$query) { $stage = Versioned::current_stage(); if ($stage == 'Live' || !Permission::check('VIEW_DRAFT_CONTENT')) { $sqlNow = Convert::raw2sql(SS_Datetime::now()); $query->addWhere(sprintf('"PublishDate" < '%s'', $sqlNow)); } } } _config.yml BlogPost: extensions: - BlogPostFilter
  8. 8. Workaround 1: Extensions Pros Can be used to add things to built-in classes Reusable: package them up, release them as modules! Cons/Caveats Reliant on extension points Extensions have limited power
  9. 9. Workaround 2: Injector Example: adding an extension hook ManyManyListWithHook.php <?php class ManyManyListWithHook extends ManyManyList { public function add($item, $extraFields = null) { parent::add($item, $extraFields); if($item instanceof DataObject) { $item->extend('onAfterManyManyAdd', $this); } } } _config.yml Injector: ManyManyList: class: ManyManyListWithHook
  10. 10. Credit to / module@camspiers silverstripe-cacheinclude
  11. 11. Workaround 2: Injector Example: using the extension hook MemberExtension.php <?php class MemberExtension extends Extension { public function onAfterManyManyAdd(ManyManyList $list) { Debug::log('Member added to many_many relation'); } } _config.yml Member: extensions: - MemberExtension
  12. 12. Workaround 2: Injector Pros Can “replace” an entire class Super powerful Cons/Caveats With great power comes great responsibility Must be instantiated with one of: Class::create() Injector::inst()->get('Class') Injector::inst()->create('Class') Injector::inst()->createWithArgs('Class')
  13. 13. When all else fails, fork it! Create a fork, apply your fix Use Git to periodically rebase from “upstream” Composer helps to make this easier!
  14. 14. Example composer.json { "require": { "silverstripe/cms": "3.1.15", "silverstripe/framework": "3.1.15" }, "repositories": [ { "type": "vcs", "url": "git@github.com:kinglozzer/sapphire.git" } ] }
  15. 15. Example composer.json { "require": { "silverstripe/cms": "3.1.15", "silverstripe/framework": "dev-hotfix" }, "repositories": [ { "type": "vcs", "url": "git@github.com:kinglozzer/sapphire.git" } ] }
  16. 16. Not quite...
  17. 17. Fixed! composer.json { "require": { "silverstripe/cms": "3.1.15", "silverstripe/framework": "dev-hotfix" }, "repositories": [ { "type": "vcs", "url": "git@github.com:kinglozzer/sapphire.git" } ] }
  18. 18. Fixed! composer.json { "require": { "silverstripe/cms": "3.1.15", "silverstripe/framework": "dev-hotfix as 3.1.15" }, "repositories": [ { "type": "vcs", "url": "git@github.com:kinglozzer/sapphire.git" } ] }
  19. 19. Forking Pros Easier to: keep track of modifications made re-apply changes during updates - rebase/cherry-pick create a PR! Cons/Caveats While easier, it’s still extra effort! Slows down composer updates
  20. 20. Summary Use extensions where possible For simple additions/fixes to methods, Injector should be okay For anything else you can’t work around, fork as last resort Use Composer!
  21. 21. Thanks/questions Feedback welcomed! @kinglozzer @kinglozzer lozcalver@bigfork.co.uk

×