Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

 

SilverStripe automation with Composer

Using Composer to avoid manual tasks and speed up development First of all, if your not…

Read post

Using Composer to avoid manual tasks and speed up development

First of all, if your not using composer to manage your projects dependencies yet then you should go read this!
It's a really simple tool that will help you a lot in all projects.

I work at Plato Creative, we make a lot of awesome SilverStripe websites. Quite often each site uses a lot from the last site you built e.g. same modules, frameworks etc...

I was getting sick of doing the same old manual tasks for every site I developed. Some of the tasks I quite often repeated were:

  • creating a git repository on the server
  • cloning it to my local environment
  • downloading the latest SilverStripe build
  • copying the modules required for the build from our local file server e.g. SortableGridField, UserForms
  • downloading and Installing foundation
  • moving foundation into the appropriate areas
  • creating common pages types e.g. HomePage, ContactPage

This works but is slow and there would be no easy way to upgrade a specific module without just deleting it and adding in a new version... this is were composer comes in.

I spent a few hours looking into how you can automate a process like the one outlined above. Composer seemed like the obvious place to do this. I researched a bit more until I found an article on composer scripts.

Scripts let you run pretty much anything from PHP functions to terminal commands. I played around for a while until I had the script doing everything I was doing manually for me.

Now I can simply do the following:

  • create a git repository on server
  • clone it to my local environment
  • clone the composer script into the project directory
  • run $ composer install

That's it!

Composer will downloaded all the required modules and dependencies, it then runs a command line exec command that gets git to clone a repository containing our SilversStripe quickstart files. For example the 'mysite' folder with pages types and decorators and the theme directory. The reason this is not downloaded like a module is because we do a bit of shuffle around and renaming of these files. We want it to be in the root directory, not in a folder named something like "RepoName".

Then next function is all PHP, it renames and moves the files from the repository above and puts them in their appropriate directory.

After all files have been moved, composer downloads the CSS framework Foundation (This could easily be replaced with another CSS framework), Foundation has an easy install CLI which makes this process really simple.

Once Foundation has been installed, one more PHP function gets called. Similar to the first function, it basically just renames and moves files/folder form the Foundation folder into the theme directory instead.

And that's it... if I went to that site directory I'd have a working SilverStripe site with a CSS framework installed and ready for rapid development.

When you are ready to test on the server you push your changes and then pull on the server and run the composer update. Composer wont run the scripts on update, only install in this case (note this could be adjusted to run on update if required). There is also a small function that checks whether the themes directory exists and if so it tells composer to install without scripts (just incase someone else tries to run composer install).

There is plenty more you could do to this script which is why I'm sharing. It would be great if the community shared basic recipes like this!

Feel free to let me know what you think in your comments or if you have any suggestions on how this could be improved!

Resources:

About the author
Myles Beardsmore

Web Developer/Artisan. Apple Fanboy. IoT enthusiast. Runner. Space geek.