Search code examples
namespaceszend-framework2less

How to integrate LESS into ZendFramework 2


I have found this tutorial which is for ZendFramework 1. I download less and put it under project/vendor/.

Leafo
└── Less
    ├── Lessc.php
    └── Lessify.php

In project/module/Application/Module.php

...
public function onBootstrap(MvcEvent $e)
{
    ...
    $this->compileLess();
}
...
public function compileLess()
{
    if (APPLICATION_ENV == 'production') {
        return;
    }
    require_once PROJECT_PATH . '/vendor/Leafo/Less/Lessc.php';

    $less_file = PROJECT_PATH . '/public/less/style.less';
    $css_file = PROJECT_PATH . '/public/css/style.css';

    $lessc = new \Leafo\Less\Lessc($less_file);
    file_put_contents($css_file, $lessc->parse());
}

Unfortunately, I get the error below

Fatal error: Class 'Leafo\Less\Lessc' not found in /Users/jslim/public_html/littlepinktree/module/Application/Module.php on line 53

I have a few questions here:

  1. How do I integrate 3rd party library to ZF2 (if the 3rd party library is not using namespace)?
  2. Is there any example showing how to integrate LESS to ZF2?

Solution

  • I have solved this by putting the whole director in ./module/Application/src/Less

    NOTE: I used back the original structure as follow

    src
    ├── Application
    │   └── Controller
    │       └── IndexController.php
    └── Less
        ├── LICENSE
        ├── Makefile
        ├── README.md
        ├── composer.json
        ├── docs
        │   └── docs.md
        ├── lessc.inc.php
        ├── lessify
        ├── lessify.inc.php
        ├── package.sh
        ├── plessc
        └── tests
            ├── ApiTest.php
            ├── InputTest.php
            ├── README.md
            ├── bootstrap.sh
            ├── inputs
            ├── outputs
            └── sort.php
    

    Use class map in Application module ./module/Application/Module.php

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
    

    Then generate autoload_classmap.php

    <?php
    // Generated by ZF2's ./bin/classmap_generator.php
    return array(
        'Application\Module'                     => __DIR__ . '/Module.php',
        'Application\Controller\IndexController' => __DIR__ . '/src/Application/Controller/IndexController.php',
        'lessc'                                  => __DIR__ . '/src/Less/lessc.inc.php',
        'lessc_parser'                           => __DIR__ . '/src/Less/lessc.inc.php',
        'lessc_formatter_classic'                => __DIR__ . '/src/Less/lessc.inc.php',
        'lessc_formatter_compressed'             => __DIR__ . '/src/Less/lessc.inc.php',
        'lessc_formatter_lessjs'                 => __DIR__ . '/src/Less/lessc.inc.php',
        'easyparse'                              => __DIR__ . '/src/Less/lessify.inc.php',
        'tagparse'                               => __DIR__ . '/src/Less/lessify.inc.php',
        'nodecounter'                            => __DIR__ . '/src/Less/lessify.inc.php',
        'lessify'                                => __DIR__ . '/src/Less/lessify.inc.php',
        'ApiTest'                                => __DIR__ . '/src/Less/tests/ApiTest.php',
        'InputTest'                              => __DIR__ . '/src/Less/tests/InputTest.php',
        'lesscNormalized'                        => __DIR__ . '/src/Less/tests/sort.php',
        'SortingFormatter'                       => __DIR__ . '/src/Less/tests/sort.php',
    );
    

    Finally, I can use it

    public function compileLess()
    {
        if (APPLICATION_ENV == 'production') {
            return;
        }
        $less_file = PROJECT_PATH . '/public/less/style.less';
        $css_file = PROJECT_PATH . '/public/css/style.css';
    
        $lessc = new \lessc($less_file);
        file_put_contents($css_file, $lessc->parse());
    }