SPF version 5.0.0 released.

  • Due to the nature of this update most object constructor signatures have been changed and will need to be updated
  • Slugs are no longer generated automatically but required on object instantiation
  • Meta boxes have been completely reworked (see documentation for more information)
    • Now supports multiple fields per meta box
    • Fixed saving for checkboxes
    • Fields are created for the meta box by using static methods in the meta box’s view file
  • Moved isValid() method to Capbilitiy class
  • Removed Enum class

Check it out on GitHub.

SPF version 4.0.0 released.

The big change in this release is changing the View class’ render() method to be static. This means that instead of instantiating a view file and passing it into a WordPressObject you now pass the path to the view file and any view data in. This is not a backwards compatible change so any WordPressObject implementation will need to be adjusted.

A Helper class was created to house static methods that do not fit in with the scope of the other objects. makeSingular() was moved to Helper from the WordPressObject base class.

Check it out on GitHub.

Creating a fake enum in PHP.

In a current project I needed to solve a problem where I wanted a class to only accept a set list of string values in a setter. In other languages you could use an enum to easily solve this. Unfortunately, PHP does not have anything like an enum natively. I needed to come up with a solution myself. I had two requirements:

  1. Limit input to a set list of strings
  2. Code completion of the values

Doing some research there were a number of developers looking for something like a enum and just as many solutions.

I ended up combining a few tricks I saw from my research. I wrote an abstract class with constants named and set to the strings I wanted to limit input to. Then I wrote a static method that checks if a string is a valid member of the class by seeing if there is a constant defined with that name.

Here’s a simplified version of the class:

Now I could setup my setter on the other class like this:

public function setSomething($value)
{
    if (FakeEnum::isValid($value)) {
        $this->something = $value;
    }
}

Then call the setter like this:

$myClass->setSomething(FakeEnum::value_one);

Typing FakeEnum:: in my IDE would use auto-completion to pull up a list of all the constants in the FakeEnum class.

WordPress Plugin Framework version 2.0.0 released.

The framework has almost been completely rewritten with this release to make the creation of WordPress objects more strongly typed and automatic.

WordPress objects now have supporting classes for arguments. This cuts down on having to type in optional parameters on construction.

The Settings class now has supporting classes for SettingsSection and SettingsField.

The View class is no longer a static and each WordPress object will have its own View saved as a property.

The View’s render() method now echos the output instead of returns it.

Check out the documentation and view the repo on Github.

Vagrant

Vagrant and Puppet, Part 4: Writing a Module

A module is a collection of Puppet resource blocks that work together to carry out a related task. All the blocks are grouped together in a class that is included in your main manifest.

This article will go through creating a very simple module for the Nginx web server.

Step 1: File Structure

Inside the puppet/modules folder we created in Part 3 create a folder named nginx. Inside the nginx folder create a folder named manifests and a folder named files. Inside the nginx/manifests folder create a file called init.pp.

module-file-structure

This is the basic file structure for a Puppet module. The module folder will have the same name as your module. Inside it you have a directory for the module’s manifest. The init.pp file in the nginx/manifests folder is the main manifest file for the module.

The files directory is used to store any files that you might call from your manifest. We will not be using it in this article but I want to make you aware of it.

Step 2: Define the Class

Open the puppet/modules/nginx/manifests/init.pp file in an editor and add the following code.

This defines the Nginx module. All your resource blocks will be placed inside this class.

Step 3: Add Resources

Inside the Nginx class we’ll add the Puppet resource blocks we need to completely install Nginx.

The first block will be a package block telling Puppet to make sure the “nginx” package is installed. Place the following code inside the nginx class.

We define the package we want to install by naming the block nginx. We ensure the package is present and we require the update-package-list exec block we defined in our main manifest to be run before this block.

Next we add a service block to make sure the Nginx service is running.

We use the service name as the name for the block. We ensure the service is running and we require the nginx package block to be run first.

This is all you need for a very simple module to setup Nginx with all the default settings and virtual host.

Step 4: Include the Module

Open the puppet/manifests/init.pp (main manifest) file in an editor.

At the bottom of the file add the following line of code.

This will let Puppet know that we want to include the nginx module. Puppet will look in the module path defined in our Vagrantfile for a folder named nginx then will run the resource blocks found in the init.pp file under the modules manifests directory.

Run vagrant provision. Puppet will see the new blocks and apply the changes. We now have Nginx running on our virtual machine.

Next Steps

This concludes my series on using Vagrant and Puppet. I have only covered very simple tasks to keep it easy to follow.

If you would like to learn more here are some good resources to get you going.

The Puppet online documentation is a good place to start. It will take you through all the different resource blocks and directives you can use as well as more advanced Puppet features. They also offer a free course with a VM to guide you through lessons.

The Forge is a repository where people can submit and download Puppet modules.

You can also view more modules I have written and use in my Puppet-Modules repository on GitHub.

Vagrant

Vagrant and Puppet, Part 3: Introduce Puppet to Vagrant

After finishing Part 2 we now have a working virtual machine ready to use. The only problem now is there is barely anything on it to use, it is just a basic Ubuntu install.

At this point you normally go through the manual process of adding repositories, installing packages, setting up services, etc. Then if you wanted to reuse this machine you’d have to go through the process again. This is what Puppet fixes.

Puppet allows you to write a manifest of your virtual machine’s desired configuration and have it happen automatically. Let’s get started.

Step 1: Directories and Files

Puppet Directory StructureCreate a new folder in your project and name it puppet.

Create a new folder named manifests and one named modules inside the puppet folder.

Inside manifests create a new text file named init.pp. This will be our main Puppet manifest. A manifest has the Puppet blocks that provision the machine.

We’ll go into Puppet modules in another article.

Step 2: Tell Vagrant About Puppet

Open your Vagrantfile and add the follow lines below the static IP configuration line.

This tells Vagrant that we are using Puppet and let’s it know where the manifests, modules, and main manifest file are.

Step 3: Tell Puppet to Update the Package List

If you vagrant up now you nothing is different than before we added the Puppet config. We still need to tell Puppet to do something.

The first thing we want to do on a new Linux install is update the package list. Open init.pp in an editor and enter the following code.

This is a basic Puppet block, in this case an execution block specified by exec.

Inside the block we give it a name update-package-list. The next line tells Puppet what command to execute, in this case sudo apt-get update. Then we specify the path of the executibles.

Now run vagrant provision from the command-line. This checks for and applies changes to an already running Vagrant box.

If everything is correct you should see the following line in the Vagrant output.

notice: /Stage[main]//Exec[update-package-list]/returns: executed successfully

We now have Vagrant and Puppet talking to each other and we are ready to further automate the provisioning of our virtual machine.

In the next article we will use Puppet to install and setup the Nginx web server by creating a module.