简体   繁体   中英

How to write composer.json while my own system is a vendor?

I've started learning composer and I am trying to build a composer.json file where I want to put all my base classes (ie 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!' . "\n";
        }
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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