Create your own Laravel 5 package – Part 2

Create your own Laravel 5 package – Part 2

In a previous article we begun to look into developing our own simple package for Laravel 5 and in this article we will continue where we left off and finish our little Gravatar package, to begin we’ll summarize what we’ve done so far in Create your own Laravel 5 package Part 1.

Great, we’re now up to speed on our package development, and it’s finally time for the best part – the actual coding! Remember that this package is a simple tutorial and it could really need some improvement, and feel free to fork my project and update me on your improvements!

Contents

  1. Finishing up our Laravel package
  2. Prepare to publish our package
    1. Update the package composer.json
    2. Create a README file
    3. Upload to GitHub
    4. Register and Upload on Packagist
  3. Conclusion

Check out the Github Page if you’re impatient

Finishing up our Laravel package

As a refresher we made our Facade and linked it to a file that we have not yet created, the PHP class Image and we know we’re going to need the Config facade from Laravel 5. Keep in mind that I’m going to break up this file into sections, we’ll begin with the first method of the file.

<?php namespace Jejje\Gravatar;

use Illuminate\Support\Facades\Config;

/**
 * Class Image
 *
 * @package Jejje\Gravatar
 */
class Image {

    /**
     * Get the global size if none is passed as argument
     *
     * Don't forget to vendor:publish
     * @return int
     */
    public function getGlobalSize()
    {
        $size = Config::get('jejje.gravatar.config.default_gravatar_size');

        return $size;
    }

Here we’re making use of our configuration file we made in the previous article, we only have one setting and it’s the default_gravatar_size which we set to 100 if we do not supply and override it as a second argument.

    /**
     * Method to reuse the Gravatar image url
     *
     * @param $id
     * @param $size
     * @return string
     */
    private function gravatarImage($id, $size)
    {
        return "http://www.gravatar.com/avatar/$id?s=$size";
    }

    /**
     * Method to reuse Gravatar profile url
     *
     * @param $id
     * @return string
     */
    private function gravatarProfile($id)
    {
        return "http://www.gravatar.com/$id";
    }

These methods is only used as an part of easy access of the Gravatar url depending if we need the avatar or the profile. These methods are private and is not accessible via the Facade.

    /**
     * This method is used to retrive url to users Gravatar
     * Will use default size from config file unless size
     * argument is passed.
     *
     * @param $email
     * @param null $size
     * @return string
     */
    public function getImageUrl($email, $size = null)
    {
        if(is_null($size))
        {
            $size = $this->getGlobalSize();
        }

        $gravatar_hash = $this->getHash($email);

        return $this->gravatarImage($gravatar_hash, $size);
    }

This is one of the main methods that we would be using, it takes two parameters the email of the user which is required and the optional size argument. As you can see if the none is provided, we’ll fetch it from our getGlobalSize() which get it from the config file. But wait a minute, there’s one method used here that we don’t have – yes, we do not have it just yet, we’ll get there.

    /**
     * Will return url to requested user Gravatar.
     *
     * @param $email
     * @return string
     */
    public function getProfileUrl($email)
    {
        $gravatar_hash = $this->getHash($email);

        return $this->gravatarProfile($gravatar_hash);
    }

This just returns the URL of the users profile, no particular magic here, and we could expand this method to actually return a tiny hCard – but it’s out of the scope of this package and tutorial.

    /**
     * Will return a Gravatar image with link
     * to profile of the requested user.
     *
     * @param $email
     * @param null $size
     * @return string
     */
    public function getImageWithLinkToProfile($email, $size = null)
    {
        if(is_null($size))
        {
            $size = $this->getGlobalSize();
        }

        $gravatar_hash = $this->getHash($email);

        $image = $this->gravatarImage($gravatar_hash, $size);
        $link = $this->gravatarProfile($gravatar_hash);

        $image_with_link = "<a href='$link'><img src='$image'></a>";

        return $image_with_link;
    }

This nifty method returns an image of the user with a link to their profile, this method could also get the second argument just as the getImageUrl can, but this actually returns the HTML for the image as well.

    /**
     * Returns the hash of an e-mail trimmed
     * if there was any spaces in the submitted
     * e-mail.
     *
     * @param $email
     * @return string
     */
    public function getHash($email)
    {
        $gravatar_hash = md5(strtolower(trim($email)));

        return $gravatar_hash;
    }

This is the last part of our Image class, and as I mentioned before, it gets the Hashed version of the users e-mail. We’re not to far from the finish line, of course we could add lots of more features, tests and so much more but I won’t deprive you of that.

Prepare to publish our package

We’ve tried our package out, and it works (hopefully) and we now plan to publish our Laravel 5 package for others to use and enjoy, or even for personal use in our next project! So what more do we need to do? Let’s make a list to make sure we don’t miss anything.

  1. Update our composer.json with information about our package
  2. Add a README.md file
  3. Upload it to GitHub
  4. Publish it to Packagist

Update the package composer.json

Workbench did all of the heavy lifting for us but we need to enter more information if we’re going to publish our Gravatar package.

{
    "name": "jejje/gravatar",
    "description": "A very simple package to get a Gravatar image or Profile. Made for education purposes for http://jejje.net",
    "license": "MIT",
    "homepage": "http://jejje.net/php/laravel-5-gravatar-package/",
    "authors": [
        {
            "name": "Jimmy JejjE Nelsing",
            "email": "[email protected]",
            "homepage": "http://jejje.net",
            "role": "developer"
        }
    ],
    "require": {
        "php": ">=5.4.0",
        "illuminate/support": "5.1.*"
    },
    "require-dev": {

    },
    "autoload": {
        "psr-4": {
            "Jejje\\Gravatar\\": "src/Jejje/Gravatar"
        }

    },
    "minimum-stability": "dev"
}

I’ve added a description of our little package, license and homepage for our package and not to forget – more information about me, what role and website I have.

Create a README file

The convention for readme files is to use Markdown, which is a simple way to add html to a readme with markdown syntax. These files has a few requirements, you need to inform people on how to install it and how to use it, and we’ll add this to the root of our package.

Upload to Github

Create your repository at Github, I’ve created it and I called it like our package Gravatar.

Create our Github repository to upload our package to

Time to do a git init, which we usually do at the beginning of our package but since this is such a small package I decided to do it at the same time as I’m writing this article.

git init

git remote add origin https://github.com/jejje/gravatar.git

git add .

git commit -m "First commit"

git push -u origin master 

If you didn’t encounter any errors, great! I did encounter a few, but a few Google searches fixed that just as usual.

Register on Packagist and upload

If you haven’t already, register on Packagist so that we may upload our fresh package for others to use. When and if you’ve registered it’s simple to add the package, just add the github url for your package.

Our package is now up at packagist for others to use, congratulations!

As you can see on this image, Packagist also suggest that we enable auto update – which is really nice. All you have to do is get the key when clicking the link GitHub Service hook, go into your GitHub settings panel and enter Packagist under the Webhooks & Services tab.

Conclusion

Congratulations, you’ve now made your very own package for Laravel 5 from start to publish. I hope that you’ve learned something new, and will keep on learning and contribute to the PHP community – and if you do, feel free to leave a comment and show me!

 

Jimmy Nelsing's Picture

About Jimmy Nelsing

I'm a computer enthusiast who loves the web and web development, I've used Wordpress for a long time on and off for different web pages for many years. I also like to develop in PHP, especially with Laravel and have also started with Java programming as side projects - testing out Bukkit and Android.

http://jejje.net/

Comments