Getting started with MyBB 1.9 development - Composer
In MyBB 1.9, we're introducing Composer to help us manage third party libraries and autoload classes. In the past, we copied third party dependencies into the inc/3rdparty folder. This made managing library updates difficult.

What is Composer?

Composer is a tool to manage dependencies in PHP projects. It allows us to define a list of packages that MyBB depends on and then manages installing and updating those dependencies automatically.

Installing the Composer command line tool on your system

To start working with Composer and installing/updating packages, you need to install the Composer tool on your system. You can either install Composer in a path local to a copy of the MyBB repository that you're working on, or you can install it globally. In this post, we'll be installing it locally as it's useful to have a single copy that you can use across multiple projects.

The download and install process is fully documented on the Composer website. The easiest way is to run the commands from the top of that page, which we haven't copied below as the file hash of the Composer package changes with every new version release they make.

Installing the MyBB dependencies

Now that you've installed Composer on your system, you can install the libraries that MyBB 1.9 depends on using it. Dependencies are installed in the composer.json file found in the root of the MyBB directory. At the time of writing, we depend upon the following libraries:
  • PHP version 7.0 or newer
  • Laravel's container package (illuminate/container)
  • Laravel's events package (illuminate/events)
  • Laravel's routing package (lluminate/routing)
  • Twig (twig/twig)

To install all of these dependencies, run the following command from the MyBB root:

php composer.phar install --no-suggest --optimize-autoloader

This will create the directory inc/vendor and fill it with several files and folders including inc/vendor/autoload.php and inc/vendor/twig/.

The version that is installed will be the same for all developers working on MyBB 1.9 thanks to the composer.lock file which specifies exact versions of packages to use.

Adding a new dependency

The easiest way to add a new dependency is to do so via the command line. First of all, find the name of the dependency on Packagist. For the purpose of this post, I've chosen to install the Laravel translation package illuminate/translation. You can easily install the dependency by running the command found below the package title on the Packagist page. For example:

composer require illuminate/translation

This will add the dependency to the composer.json file and actually download the package itself into the inc/vendor folder for you. The library will be available via the autoloader (inc/vendor/autoload.php) and available to use straight away with no further effort.

This page is also available on the MyBB GitHub Wiki, and can be found here

Messages In This Thread
Getting started with MyBB 1.9 development - Composer - by Euan T - 2018-01-14, 04:35 PM

Forum Jump:

Users browsing this thread: 1 Guest(s)