composer


Symlinks on Windows using Composer

I was reminded today that in order to get symlinks to create on a windows machine that you need to “run as administrator” when starting your console, otherwise you’ll get an error like “Warning: symlink(): Cannot create symlink, error code(1314)” . While we’re on the topic, how about I demo how the autoloader is setup in my composer.json file along with the calling of post install and post update scripts.

    "autoload": {
		"psr-4": {
			"Nick\\": "script/"
		}
	},
 
    "require": {
        "php": ">=5.4",
        "wordpress/wordpress": "4.0"
    },
    "scripts": {
        "post-install-cmd": [
		"Nick\\Install::symlinks"
        ],
        "post-update-cmd": [
            "Nick\\Install::symlinks"
        ]
    },

Then, my post install file lives at /script/Install.php

<?php
 
namespace Nick;
 
use Composer\Script\Event;
 
class Install {
 
    public static function symlinks(Event $event) {
    	$rootDir = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR;
		$wordpressDir = $rootDir . 'vendor' . DIRECTORY_SEPARATOR . 'wordpress' . DIRECTORY_SEPARATOR . 'wordpress' . DIRECTORY_SEPARATOR;
 
		//htaccess file
    	if(! file_exists($wordpressDir . '.htaccess')) {
			symlink($rootDir . '.htaccess', $wordpressDir . '.htaccess');
    	}
 
    	//wordpress config file
    	if(! file_exists($wordpressDir . 'wp-config.php')) {
    		symlink($rootDir . 'config' . DIRECTORY_SEPARATOR . 'wp-config.php', $wordpressDir . 'wp-config.php');
    	}
 
    	//wordpress db config file
    	if(! file_exists($wordpressDir . 'db-config.php')) {
    		symlink($rootDir . 'config' . DIRECTORY_SEPARATOR . 'db-config.php', $wordpressDir . 'db-config.php');
    	}
 
    	//wordpress
    	if(! file_exists($rootDir . 'public' . DIRECTORY_SEPARATOR . 'wp')) {
    		symlink($wordpressDir, $rootDir . 'public' . DIRECTORY_SEPARATOR . 'wp');
    	}
 
    }
}

Note that I’ve chosen to separate the database connection from the main wordpress config file so it can be ignored separately in version control.