Here is a guest post from Emmanuel Dyan from iNET Process, an Elite SugarCRM Partner, and an active Sugar Developer community member as well as an open source advocate.
Are you interested in posting on the Sugar Developer Blog? Contact email@example.com with your idea.
Managing libraries and dependencies within Sugar
Sugar Partners sometimes need to share libraries between various Sugar development projects. It is also required that we use the custom directory structure for Sugar customization. Sometimes developers will just copy and paste libraries or other dependencies into the custom directory that they need to use for each of their Sugar projects. But there are better ways to manage your code’s dependencies.
Composer is the best way to manage your software dependencies for PHP, so much so that it is de facto standard that PHP developers are adopting (including the team at SugarCRM). In this blog post, we will explore how you can use Composer to easily manage libraries used with Sugar development.
If you haven’t used Composer before, then check out Composer’s Getting Started guide for installation instructions.
Installing libraries using Composer
Let us suppose we have a project where we need to read YAML files. The best component to do with PHP is symfony/yaml. So, instead of downloading it and unzipping it into any directory, we can use Composer by running :
$ cd custom/
$ composer require symfony/yaml
This will generate a composer.json file that contains our new dependency and download the symfony/yaml library into the vendor directory. However, we also need to make some additional edits to this file.
Here is an example of a complete config file that we can use with Sugar.
Importantly, using the config.platform.php setting above allows us to ensure everything downloaded is compatible with PHP 5.4 as required by the Sugar version we are using.
Since we want to follow PHP best practices, we can also add a folder to the autoloader to load our own PSR compliant classes and methods.
For example, we may create code with the Inet\MyLib namespace that we store at custom/include/lib/Inet/MyLib.
Also in the above example, we have included a dev dependency for inetprocess/libsugarcrm. This is an open source library that is helpful for writing Sugar unit tests for our customizations.
Adding Composer’s Autoloader using Utils Extension
Finally, we’ll ask Sugar to use Composer’s autoloader by using Sugar’s Utils Extension to include an additional autoloader.
require_once(__DIR__ . '/../../../vendor/autoload.php');
With this in place we can share everything between modules!
Using sugarcli to quickly set up Composer
If you want to automatically setup Composer for your Sugar instance, just use … sugarcli (https://github.com/inetprocess/sugarcli) !
Follow instructions on Github to download it, install it, and launch:
./sugarcli.phar code:setupcomposer --path <sugarcrm_path> --do
# Or, if it's in the path: sugarcli code:setupcomposer --path <sugarcrm_path> --do
That will check if the file exist, complete the installation or create both the files. It can even reinstall Composer if you want to start from scratch.