Install PHP7 and Composer on Windows 10

PHP7 is a general purpose scripting language well suited for web development. Composer is the defacto package manager for PHP7. This tutorial will show you how to install PHP7 and Composer on Windows 10 for use in a command prompt.

A common misconception is that you need a web server like IIS, Apache, or Nginx to get started with PHP7 development. In fact, PHP7 has its own built in web server that you can invoke at the command prompt. Modern PHP frameworks such as Opulence, Symfony, Cake, Laravel, WordPress, and many more support this.

Installing PHP7

Download the latest PHP7 (non-thread safe version) zip file from http://windows.php.net/

Extract the contents of the zip file into C:\PHP7

Copy C:\PHP7\php.ini-development to C:\PHP7\php.ini

Open the newly copied C:\PHP7\php.ini in a text editor.

Scroll down to “Directory in which the loadable extensions (modules) reside.” and uncomment: extension_dir = “ext”

Notepad++ is great.

Scroll down to the extensions section and uncomment the extensions you want to use.

Note: The syntax used in previous PHP versions (‘extension=’php_*.dll’) may be deprecated in a future PHP major version.

Tweak other settings as needed.

Note: Don’t forget to keep your php.ini file in a safe place when you upgrade in the future!

Add C:\PHP7 to the Windows 10 system path environment variable.

Windows 10 has finally improved this interface, yay!

In a command prompt test that the installation is successful by typing php -v

ConEmu is great.

Installing Composer

On my computer I’ve created a C:\Users\dac\bin directory where I keep miscellaneous executables. This directory is in my user path.

Use a search engine to find a tutorial and do something similar. Optionally install composer in the C:\PHP7 directory you just created as it’s already in your path.

To get composer.phar, drop to a command prompt, cd into your target directory, and run:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"

(Important! Click here for a more secure and up-to-date install snippet.)

Next, create a new composer.bat file alongside the composer.phar file so that Windows 10 can execute it more easily. (Source)

echo @php "%~dp0composer.phar" %*>composer.bat

Test that it’s working by typing composer -V

ConEmu is still great.

MySQL Dump Full Structure, Partial Data, With Triggers & Routines.

You want to do a MySQL dump. You want the entire structure of the database but you want to exclude some tables because they are too big, have sensitive data, or other reasons. Your MySQL database has triggers, routines, and all that good stuff because it’s 2016.

When I went looking for a solution I read a tutorial that wrongly suggested dumping triggers and schema together in the first step. The problem with this approach is when you import your data, the ON INSERT triggers are executed, and this can lead to primary key conflicts or other weird issues. I learned the hard way.

A better way:

  • Schema first
  • Data next
  • Triggers and routines last
mysqldump --no-data --skip-triggers DATABASE > FILE.sql

mysqldump --no-create-db --no-create-info --skip-triggers --ignore-table=TABLE1--ignore-table=TABLE2 DATABASE >> FILE.sql

mysqldump --no-create-db --no-create-info --no-data  --routines --triggers --skip-opt DATABASE >> FILE.sql

Good times.

Autocomplete a Silex Project in PHPStorm

The problem with Silex, and Pimple in general, is that when you do:

$app = new \Pimple\Container();
$app['Foo'] = function () { return new \Acme\Foo(); };
$app['Bar'] = function () { return new \Acme\Bar(); };

PHPStorm has no way of knowing what’s going on in, or how to auto-complete, $app.

I’ve gotten around this in the past by creating an “Inception Proxy” alongside a .phpstorm.meta.php configuration but for a new Silex project I’ve inherited this is not possible.

Pro-tip: If your IDE doesn’t know what’s going on then neither will the poor jerks who inherit your code.

Looking for a solution to this I discovered the PHPStorm Silex Plugin. It’s a bit wonky but it does the job. (sometimes the IDE doesn’t recognize $app and I don’t know why yet.)

For the Silex Plugin to work it requires a manually created configuration file in the project root named “pimple.json”. This file more or less duplicates the functionality of .phpstorm.meta.php but I digress… Pimple.json can be automatically generated using Pimple Dumper.

The format of “pimple.json” looks like:

[
    {
        "name": "routes",
        "type": "class",
        "value": "Symfony\\Component\\Routing\\RouteCollection"
    },
    {
        "name": "request.http_port",
        "type": "int",
        "value": 80
    },
    {
        "name": "charset",
        "type": "string",
        "value": "UTF-8"
    }
]

Once that file is in place, and you jiggle the IDE/Plugin, auto-complete comes alive! Horray for sanity.

Password Protect WordPress Admin With .htaccess

The wp-admin panel is already password protected in that you are required to login. Sometimes that’s not good enough. This tutorial explains how to add an additional layer of authentication to the login process, essentially blocking wp-login.php requests from annoying bots or other malicious users.

Step 1:

Create a `/path/to/.htpasswd` file. (More info.)

Step 2:

Create a `/path/to/your/site/wp-admin/.htacess` file with the following content:

AuthUserFile /path/to/.htpasswd
AuthType basic
AuthName "Restricted Resource"
require valid-user

# Whitelists

<Files "admin-ajax.php" >
   Order allow,deny
   Allow from all
   Satisfy any
</Files>

<Files "*.css" >
   Order allow,deny
   Allow from all
   Satisfy any
</Files>

<Files ~ "\.(jpg|jpeg|png|gif)$">
   Order deny,allow
   Allow from all
   Satisfy any
</Files>

Change `/path/to/` your files accordingly.

Important! Under Whitelists I have added entries for admin-ajax.php, *.css, and a regular expression for images. This unblocks WordPress’ AJAX functionality used by certain plugins, as well as CSS and image files certain themes may be importing. Without these you risk breaking your site.

Step 3:

Append the following to your existing WordPress .htaccess file one parent folder up (Ie. /path/to/your/site/.htaccess):

<Files wp-login.php>
  AuthUserFile /path/to/.htpasswd
  AuthType basic
  AuthName "Restricted Resource"
  require valid-user
</Files>

Change `/path/to/` your files accordingly.

Fix CSS Path Errors by Setting Resource Root Folders In PHPStorm

While developing a plugin for WordPress I was having trouble linting CSS files in PHPStorm. One file in particular was giving hundreds of false positives for errors related to paths:

False positives
The right side gutter is all red but most of these errors are wrong.

This bothered me. I wanted to fix the reported errors but the reporting was wrong. Most of the time there was nothing to fix. After much fiddling I discovered files under a folder marked as Resource Root can be referenced as relative:

Resource Root
To fix the above, set Resource Root(s) in PHPStorm

Low and behold the errors became real! Oh crud, time to fix.

Real errors
Now the right side gutter shows actual errors (that I should fix!)

Source:

https://www.jetbrains.com/phpstorm/help/configuring-folders-within-a-content-root.html

Setting up Coveralls.io (with Travis CI and PHPUnit)

Step 1)

Register you GitHub repo with Travis CI and Coveralls.IO.

Step 2)

In your .travis.yml file, add:

before_install:
- composer require phpunit/phpunit:4.8.* satooshi/php-coveralls:dev-master
- composer install --dev

script:
- ./vendor/bin/phpunit --coverage-clover ./tests/logs/clover.xml

after_script:
- php vendor/bin/coveralls -v

Where:

  • before_install: Calls composer and installs PHPUnit 4.8.* + satooshi/php-coveralls.
  • script: Calls the installed version of PHPUnit and generates a clover.xml file in ./tests/logs/clover.xml. (This XML file will be used by PHP-Coveralls.)
  • after_script: Launches satooshi/php-coveralls in verbose mode.

Step 3)

Create a .coveralls.yml file that looks like:

coverage_clover: tests/logs/clover.xml
json_path: tests/logs/coveralls-upload.json
service_name: travis-ci

Where:

  • coverage_clover: Is the path to the PHPUnit generated clover.xml.
  • json_path: Is where to output a json_file that will be uploaded to the Coveralls API.
  • service_name: Use either travis-ci or travis-pro.

Step 4)

Add badges to your GitHub README.md file.

[![Build Status](https://travis-ci.org/NAMESPACE/REPO.svg?branch=master)](https://travis-ci.org/NAMESPACE/REPO)
[![Coverage Status](https://coveralls.io/repos/NAMESPACE/REPO/badge.svg?branch=master&service=github)](https://coveralls.io/github/NAMESPACE/REPO?branch=master)

Replace NAMESPACE and REPO to match your GitHub repo.

Get cwRsync Working With Vagrant On Windows 10

Getting cwRsync to work with Vagrant on Windows 10 is a pain.

This tutorial is for people who have:

Reading comprehension 101:

cwRsync is a standalone version of rsync for Windows that doesn’t require Cygwin to be installed. I don’t have Cygwin installed because Git For Windows includes Git Bash and this is “good enough.” With a regular standalone cwRsync installation Cygwin will never be in the PATH and Vagrant will never add the required /cygdrive prefix.

Howto fix:

Add C:Program Files (x86)cwRsync (or wherever you installed) to your path. To avoid problems make sure this string is placed before C:Program FilesGitcmd and/or C:Program FilesGitmingw64bin;C:Program FilesGitusrbin

Add cwRsync to your path.
Add “C:Program Files (x86)cwRsync” to your path.

Add the following system variable: CYGWIN = nodosfilewarning

CYGWIN = nodosfilewarning
Add “CYGWIN = nodosfilewarning” as a system variable.

Edit (hack!)C:HashiCorpVagrantembeddedgemsgemsvagrant-1.7.4pluginssynced_foldersrsynchelper.rb

Change line ~43 from:

hostpath = Vagrant::Util::Platform.cygwin_path(hostpath)

To:

hostpath = "/cygdrive" + Vagrant::Util::Platform.cygwin_path(hostpath)
Edit rsynchelper.rb
Edit Vagrant’s rsynchelper.rb file

Restart your shells to apply changes. Fiddle with your Vagrantfile. Tada!

Troubleshooting:

Git for Windows is based on MinGw. cwRsync is based on Cygwin. You cannot run Vagrant & cwRsync from Git Bash because cwRsync includes it’s own incompatible SSH binary. If you try you will get the following error:

rsync error: error in rsync protocol data stream (code 12) at io.c(226) [Receive r=3.1.0]

Instead, when launching Vagrant use Microsoft PowerShell.

Sources:

[1] http://auxmem.com/2010/03/17/how-to-squelch-the-cygwin-dos-path-warning/
[2] https://github.com/mitchellh/vagrant/issues/3230
[3] https://github.com/mitchellh/vagrant/issues/4586