Does exist a plan to start using PSR-4 autoloading?

97 views
Skip to first unread message

g4b0

unread,
Jul 21, 2015, 3:23:00 AM7/21/15
to silverst...@googlegroups.com
It should be really useful to start using composer for external dependecy like in Symfony, Silex etc.
Actually for use a standard component like HtmlPurifier I have to pack it into a module: https://github.com/g4b0/silverstripe-htmlpurifier just for autoload it.

Another good, and maybe more simple, task would be to switch to PSR-2 coding style, just to have a worldwide accepted standard and don't become crazy switching from a project to another :D

g4b0

Loz Calver

unread,
Jul 21, 2015, 4:35:03 AM7/21/15
to silverst...@googlegroups.com
I believe the current plan is to tackle PSR-4 for core code at the same time as namespacing, as we need to be namespaced to conform to PSR-4, ready for 4.0. You should already be able to use PSR-4/PSR-0 autoloading in your projects and modules, though, providing you’re using composer (we’ll be using composer’s autoloader for core code). It’s usually just a case of pointing to it in composer.json.

It looks like the package you’re using isn’t officially available via composer, so that may make things a bit more tricky. There does seem to be one available that looks fairly popular: https://github.com/ezyang/htmlpurifier, perhaps you could require that in your module as it will take care of the autoloading for you.

As for PSR-2, it always seems to cause a lot of controversy for some reason. I personally would prefer it as there are many more code sniffer rules and automated tools for fixing to that standard than there are if we keep rolling our own. If we’re going to switch to that, or any other, coding style, it makes sense to do it while we’re playing around with namespacing and our directory structure :)

Loz

g4b0

unread,
Jul 21, 2015, 8:48:23 AM7/21/15
to silverst...@googlegroups.com
Ok, so PSR-4 is planned, that's a good news! Is the 4.0 code already published on github, or it's just an internal work?

HtmlPurifier was just an example of how I wrap external components into SS just to use it's autoloading feature. The module is working like a charm in 3.x, so for now it's ok.

g4b0

Marcus Dalgren

unread,
Jul 21, 2015, 9:09:20 AM7/21/15
to silverst...@googlegroups.com
This module from Cam will autoload composer modules for you: http://addons.silverstripe.org/add-ons/camspiers/autoloader-composer-silverstripe
You don't actually have to create a new module for each package you want to autoload.

Loz Calver

unread,
Jul 21, 2015, 9:22:22 AM7/21/15
to silverst...@googlegroups.com
That module was created a long time ago. SilverStripe will now automatically include vendor/autoload.php if it exists, so that module shouldn’t be required any more.

g4b0, we’re planning on making core code PSR-4 compliant and loading it with composer’s autoloader - not writing our own PSR-4 autoloader. Because we’re including vendor/autoload.php (as mentioned above), SilverStripe will already autoload any code that’s registered with composer’s autoloader.

As Marcus said, you don’t need to create a module just to take advantage of this - any PSR-4 or PSR-0 compliant packages that you add to your main project’s composer.json will be autoloaded automatically. For example, if you add "ezyang/htmlpurifier": "*" to your project’s requirements and run a composer update, it will be autoloaded.

tldr; we already support this :)

g4b0

unread,
Jul 21, 2015, 9:28:24 AM7/21/15
to silverst...@googlegroups.com
Thanks for the hint Loz and Marcus, I will definetly try it.

So core code will be organized in different mode than the usual framework, cms, mysite etc folder structure? I was asking if core code 4.x is already published on github, just to have a look :D

g4b0

Loz Calver

unread,
Jul 21, 2015, 9:50:25 AM7/21/15
to silverst...@googlegroups.com
The directory structure will probably change a bit, yeah. One of the lower priority goals for 4.0 is to move core code out of the document root, so if that goes ahead then things will probably start to look a bit different.

Sam recently gave a talk about some objectives for 4.0 that touches on this subject, you might find that interesting :)

Conrad Dobbs

unread,
Jul 22, 2015, 4:38:39 AM7/22/15
to SilverStripe Core Development
I believe there was another topic discussing this (https://groups.google.com/forum/#!searchin/silverstripe-dev/autoloading/silverstripe-dev/wKVrc0vQ3Zc/xB9annF_askJ). Also, I think there was some work done on PSR-0 autoloading (mentioned in the other topic). I think there has also been other discussions about using composer to load thirdparty libraries in core.

Would be good to also implement the PSR coding conventions/standards to bring SilverStripe inline with other frameworks.

Another piece of work which might tie into autloading nicely would be making the framework more modular. The ORM, routing etc could become their own standalone packages.
Reply all
Reply to author
Forward
0 new messages