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

如何在我自己的系统是供应商时编写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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题