简体   繁体   English

使用公共资产为Composer创建PHP库

[英]Creating a PHP library for Composer with public assets

I'm creating a Template library for a client. 我正在为客户端创建模板库。 The goal of the library is to provide a standardized shell for all the websites the company owns. 该库的目标是为公司拥有的所有网站提供标准化的外壳。 Other developers should be able to import the library with Composer and create a base template with a few lines of code, eg: 其他开发人员应该能够使用Composer导入库并使用几行代码创建基本模板,例如:

$Template = new Template;
$Template->setContent('Hello world!');
$Template->render();

This will render the content inside the standardized shell. 这会将内容呈现在标准化外壳中。 This is a simplified example, the library provides much more functionality such as standardized sliders, menu's etc... 这是一个简化的示例,该库提供了更多功能,例如标准化的滑块,菜单等。

This library depends on Twig, but it also depends on a bunch of front-end assets; 该库依赖于Twig,但也依赖于许多前端资产。 like custom images, javascripts, css and 3rd party stuff like Jquery and Bootstrap CSS. 例如自定义图片,javascript,css和第三方工具(例如Jquery和Bootstrap CSS)。 Those assets are stored in the /assets/ folder. 这些资产存储在/ assets /文件夹中。

After installing the library with Composer, everything is stored in the /vendors/ folder, which is in the root of the website. 使用Composer安装库之后,所有内容都存储在网站根目录的/ vendors /文件夹中。 But the /assets/ folder needs to be in the /public_html/ to be accessible. 但是/ assets /文件夹需要位于/ public_html /中才能访问。

The solution for now: just copy the /assets/ folder to the /public_html/. 现在的解决方案:只需将/ assets /文件夹复制到/ public_html /。 But I'm sure there are more repositories depending on front-end assets, what is the most common way to make those assets available in /public_html/? 但是我确定还有更多存储库,具体取决于前端资产,在/ public_html /中使这些资产可用的最常用方法是什么?

The closes thing which comes into my mind is the Symfony2 Bundle system. 我想到的关门就是Symfony2 Bundle系统。 Each bundle you define must be registered in the AppKernel. 您定义的每个捆绑包都必须在AppKernel中注册。 If the bundle contains assets that will be linked or copied under the /web/bundles/short_bundle_name/ folder. 如果捆绑包包含将链接或复制到/ web / bundles / short_bundle_name /文件夹下的资产。

So to be able to follow up your dependencies' assets you will need some kind of registry mechanism, so you can register which libs has assets what needs to be copied over / linked under the public_html folder. 因此,为了能够跟踪依赖项的资产,您将需要某种注册表机制,以便可以注册哪些libs具有资产,这些资产需要通过public_html文件夹/链接复制。

Also consider using assetic to process your plain assets (merge, rewrite and other filters) with cache-busting capabilities. 还可以考虑使用资产管理功能通过缓存清除功能来处理您的普通资产(合并,重写和其他过滤器)。

You can use a combination of Composer for backend dependencies and Bower for your front end, and Grunt to move and process files. 您可以将Composer用于后端依赖性,将Bower用于前端,将Grunt用于移动和处理文件。

with Composer you add the following to your composer.json 使用Composer您可以将以下内容添加到composer.json

"scripts": {
    "post-install-cmd": [
        "bower install",
        "grunt"
    ],
    "post-update-cmd": [
        "bower install",
        "grunt"
    ]
}

And then you have you basic Bower script bowser.json 然后,您有了基本的Bower脚本bowser.json

{
    "name": "my/project",
    "dependencies": {
        "jquery": "*",
        "bootstrap": "*"
    }
}

And then you have you basic Grunt script gruntfile.js where you could use this ( https://github.com/gruntjs/grunt-contrib-copy ) like so. 然后,您有了基本的Grunt脚本gruntfile.js ,可以在其中像这样使用它( https://github.com/gruntjs/grunt-contrib-copy )。

copy: {
  main: {
    expand: true,
    src: 'src/*',
    dest: 'dest/',
  },
},

Now you just have to run your composer commands to get all of your files installed or updated. 现在,您只需要运行composer命令即可安装或更新所有文件。

Note: you don't need to use Bower if all of your assets are in Composer 注意:如果所有资产都在Composer则无需使用Bower

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM