why you shouldn’t edit silver stripe core files (and how to do it anyway)
TRANSCRIPT
![Page 1: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/1.jpg)
Why you shouldn’tedit SilverStripe
core files(and how to do it anyway)
By Loz Calver / @kinglozzer
![Page 2: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/2.jpg)
What do you mean “editingcore files”?
Modifying someone else’s codeCode outside the scope of “your project”Applies to framework, cms and any other modules
![Page 3: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/3.jpg)
Why would I edit core files?Bugs - SilverStripe isn’t (quite) perfectSecurity
need urgent/immediate fixemail us!
Missing/upcoming features - time delay between minor versionsClients!
![Page 4: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/4.jpg)
Why shouldn’t I edit core files?Other developers don’t know what you’ve doneUpdates 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
![Page 5: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/5.jpg)
How can I avoid editing core?
![Page 6: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/6.jpg)
Workaround 1: ExtensionsExample 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
![Page 7: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/7.jpg)
Workaround 1: ExtensionsExample 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
![Page 8: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/8.jpg)
Workaround 1: ExtensionsPros
Can be used to add things to built-in classesReusable: package them up, release them as modules!
Cons/CaveatsReliant on extension pointsExtensions have limited power
![Page 9: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/9.jpg)
Workaround 2: InjectorExample: 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
![Page 10: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/10.jpg)
Credit to / module@camspiers silverstripe-cacheinclude
![Page 11: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/11.jpg)
Workaround 2: InjectorExample: 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
![Page 12: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/12.jpg)
Workaround 2: InjectorPros
Can “replace” an entire classSuper powerful
Cons/CaveatsWith great power comes great responsibilityMust be instantiated with one of:
Class::create()Injector::inst()->get('Class')Injector::inst()->create('Class')Injector::inst()->createWithArgs('Class')
![Page 13: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/13.jpg)
![Page 14: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/14.jpg)
When all else fails, fork it!
Create a fork, apply your fixUse Git to periodically rebase from “upstream”Composer helps to make this easier!
![Page 15: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/15.jpg)
Examplecomposer.json
{ "require": { "silverstripe/cms": "3.1.15", "silverstripe/framework": "3.1.15" }, "repositories": [ { "type": "vcs", "url": "[email protected]:kinglozzer/sapphire.git" } ]}
![Page 16: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/16.jpg)
Examplecomposer.json
{ "require": { "silverstripe/cms": "3.1.15", "silverstripe/framework": "dev-hotfix" }, "repositories": [ { "type": "vcs", "url": "[email protected]:kinglozzer/sapphire.git" } ]}
![Page 17: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/17.jpg)
Not quite...
![Page 18: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/18.jpg)
Fixed!composer.json
{ "require": { "silverstripe/cms": "3.1.15", "silverstripe/framework": "dev-hotfix" }, "repositories": [ { "type": "vcs", "url": "[email protected]:kinglozzer/sapphire.git" } ]}
![Page 19: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/19.jpg)
Fixed!composer.json
{ "require": { "silverstripe/cms": "3.1.15", "silverstripe/framework": "dev-hotfix as 3.1.15" }, "repositories": [ { "type": "vcs", "url": "[email protected]:kinglozzer/sapphire.git" } ]}
![Page 20: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/20.jpg)
ForkingPros
Easier to:keep track of modifications madere-apply changes during updates - rebase/cherry-pickcreate a PR!
Cons/CaveatsWhile easier, it’s still extra effort!Slows down composer updates
![Page 21: Why you shouldn’t edit silver stripe core files (and how to do it anyway)](https://reader031.vdocument.in/reader031/viewer/2022021922/58ef2f291a28ab8b618b4663/html5/thumbnails/21.jpg)
SummaryUse extensions where possibleFor simple additions/fixes to methods, Injector should be okayFor anything else you can’t work around, fork as last resortUse Composer!