Building a Craft CMS Installer
Reducing 7 steps into 1 - here's how to start a new project with Craft CMS in the most streamlined and friction-free way.
Manually downloading and installing Craft CMS
You know the drill: You open your browser, visit craftcms.com, click the download button, accept the licence terms and click download. Then you extract the zip file in your downloads folder and move everything into your project folder.
At this point, you’ll most likely configure general settings and the database connection via the config files; you might even want to restructure the default Craft directory structure to match your other projects.
Simple, right? I guess so.
But we felt it could be easier still. We wanted to try and reduce those 7 steps into 1.
Where to start?
Before we started writing any code we wanted to decide on the installation process that would provide the least friction when starting a new project with Craft CMS.
During this process, we decided to expand the installer so that we could start a new Craft project and include our project bootstrap code – Kickoff – at the same time. This would mean all of our default Gulp tasks, metafiles etc. would be included by default, saving time.
We wanted our install process to be as easy as typing the following into our terminal window:
kickoff new craft
We now had to make it work.
Fortunately, having used Laravel on a lot of client projects, we had utilised the Laravel Installer – a simple Symfony console app, written in PHP. We send hours a day writing PHP scripts, so using it to build our installer would be very much in our comfort zone.
Writing the code
We got the basics of the console command code together by referencing the Symfony Console component docs.
Once the foundation was in place we established that the general tasks of the installer would be:
- Download the latest version of Craft CMS as a zip file.
- Extract the contents to a temporary location
- Clean up the downloaded zip file
- Run Craft CMS specific installation tasks
It was immediately clear that the first 3 steps could work for multiple platforms/frameworks, so we created an abstract class that handled these tasks and could be easily extended to allow us to support the installation of other platforms in the future.
The basic signature of our base installer class looks like this:
<?php
abstract class Installer
{
protected function download() { //... }
protected function extract() { //... }
protected function cleanUp() { //... }
abstract protected function process();
public function install()
{
$this->download()->extract()->cleanUp()->process();
}
}
With the completed class in place, we'd just need to make sure our framework-specific installers provided their own process()
function that is responsible for running the commands needed to configure that particular framework.
Next, in order to get Craft and Kickoff working together we had to create commands that would:
- Update the default Craft directory structure
- Update index.php so Craft knows where the directories have moved to
- Download and configure PHPDotEnv
- Update database configuration file to use values in our .env file
This was as relatively simple, as we were able to just use basic bash commands via the Symfony Process component. Take a look at the Craft CMS installer code to see exactly what bash commands we ended up using.
Keeping it clean
While we initially built the installer to make our own projects a little easier to start, we wanted to provide a way for others to use it too.
By default, our installer sets up Craft CMS the way we like it, but that’s not to say everyone else structures their projects the same way.
We’re working towards making the installer configurable, but until that’s ready we’ve added a ‘clean’ option. Running kickoff new craft --clean
will download the latest version of Craft and extract it to your current directory with no additional changes - just as it would be if you downloaded directly from craftcms.com.
Has it been useful?
Sure! This was a fun little project for us and while it's still a very basic tool it has already saved us time and allowed us to cut out some repetitive tasks.
We're sure once it's configurable you'll find it much more useful too, so be sure to keep an eye on our progress by watching the repo on GitHub.
Try it for yourself
You can find the full source code and usage instructions for our Craft CMS Installer on Github.
We'd love to hear any feedback you might have!
We also encourage you to take a look at any repetitive tasks in your workflow and see if you can find a way to make them easier to complete - or better still - automate them entirely.