duanjue7745 2014-09-09 05:28
浏览 62
已采纳

如何在我自己的系统是供应商时编写composer.json?

I've started learning composer and I am trying to build a composer.json file where I want to put all my base classes (i.e. a custom framework) as a vendor itself with other dependencies.

The scenario is that I am building a small framework with Slim, Twig and Laravel ORM etc and I want to put my framework (with all of it's dependencies) in a folder outside of the document root as it can be used for several projects by a custom PHP included path either set in PHP.ini or by set_include_path(). I am using a VPS.

My folder structure is as follows:

/path/to/my/own/framework
 |----> composer.json
 |----> composer.lock
 |----> vendor
         |----> slim
         |----> illuminate
         |----> twig
         |----> om
                 |----> OmFramework (this is my own framework)
                          |----> Factory
                                  |----> BaseController.php
 ....

I have written the following composer.json so far:

{
    "name": "....",
    "description": "....",
    "keywords": ["..."],
    "license": "MIT",
    "authors": [
        {
            ....
        }
    ],
    "require": {
        "php": ">= 5.3.0",
        "slim/slim": "2.4.*",
        "slim/views":"0.1.2",
        "twig/twig": "1.*",
        "twig/extensions": "*",
        "itsgoingd/slim-facades": "dev-master",
        "illuminate/database" : "4.*",
        "cartalyst/sentry" : "2.*",
        "ircmaxell/password-compat": "1.0.*"
    },
    "autoload": {
        "psr-0": { "OmFramework": "." }
    }
}

What should I write into the autoload section in order to load all my classes from om/OmFramework through the composer.json file? Any guidance would be highly appreciated.

The BaseController.php file:

<?php
namespace OmFramework\Factory;

class BaseController {
        public function __construct()
        {
                //TO-DO:
        }

        public function sayHello() 
        {
                echo 'Hello World!' . "
";
        }
}

If I have missed any information that would help you to guide me, I will be happy to share those.

Please note that I am developing the framework locally and not using github for now. When it is ready, I will push it to github.

  • 写回答

1条回答 默认 最新

  • dqmchw0071 2014-09-09 09:28
    关注

    The framework package should ship it's own code in a directory of choice (excluding the vendor directory itself), and then point the autoloader definition to it:

    /path/to/my/own/framework
     |----> composer.json
     |----> composer.lock
     |----> om
     |------> OmFramework
     |---------> Factory
     |------------> BaseController.php
     |----> vendor
             |----> slim
             |----> illuminate
             |----> twig
    

    The "autoload" section of the composer.json will be simple:

    {
      "autoload": {
          "psr-4": {
              "OmFramework\\": "om/OmFramework"
           } 
       }
    }
    

    PSR-4 is preferred to be used when dealing with namespaced classes. PSR-0 should be used for older classes without namespaces.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示