如何将LESS集成到ZendFramework 2中

时间:2013-04-15 11:24:00

标签: namespaces zend-framework2 less

我找到this tutorial用于 ZendFramework 1 。我下载较少,并将其放在 project / vendor / 下。

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

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());
}

不幸的是,我收到了以下错误

  

致命错误:第53行的/Users/jslim/public_html/littlepinktree/module/Application/Module.php中找不到“Leafo \ Less \ Lessc”类

我在这里有几个问题:

  1. 如何将第三方库集成到ZF2(如果第三方库未使用命名空间)?
  2. 是否有任何示例显示如何将LESS集成到ZF2?

2 个答案:

答案 0 :(得分:2)

我通过将整个导演放在 ./ module / Application / src / Less

中解决了这个问题

注意:我使用了原始结构,如下所示

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

在应用程序模块中使用类映射 ./ 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__,
            ),
        ),
    );
}

然后生成 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',
);

最后,我可以使用它

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());
}

答案 1 :(得分:0)

您需要在自动加载器中注册Leafo命名空间。假设你有这棵树:

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

你必须像这样注册命名空间:

    Zend\Loader\AutoloaderFactory::factory(array(
        'Zend\Loader\StandardAutoloader' => array(
            'autoregister_zf' => true,
            'namespaces' => array(
                'Leafo' => '/MyProject/vendor/Leafo',
            ),
        )
    ));

如果您使用ZendSkeletonApplication,可以在init_autoloader.php文件中更改此内容。