Reputation: 3986
I've just added my first composer.json package to an API I created.
https://github.com/elvanto/api-php
Here's a copy of my file
{
"name": "elvanto/api-php",
"type": "library",
"description": "API PHP Library for elvanto church management software.",
"keywords": ["elvanto", "api"],
"homepage": "https://github.com/elvanto/api-php/",
"license": "MIT",
"authors": [
{
"name": "Ben Sinclair",
"homepage": "http://elvanto.com",
"role": "Developer"
}
],
"require": {
"php": ">=5.3.0"
},
"autoload": {
"classmap": [
"."
]
}
}
When I tried to install the package using Composer I get this in Terminal
-$ composer install
Loading composer repositories with package information
Installing dependencies (including require-dev)
Nothing to install or update
Generating autoload files
And in my vendor/ folder, it loads the autoload files but does not include my class file:
autoload.php
composer/autoload_classmap.php
composer/autoload_namespaces.php
composer/autoload_real.php
composer/ClassLoader.php
Have I done something wrong? I tried changing the composer.json file to the following (which I've seen other packages do) but I got an entirely different error:
"autoload": {
"classmap": [
"elvanto_API.php"
]
}
The error was:
[RuntimeException]
Could not scan for classes inside "elvanto_API.php" which does not appear to be a file nor a folder
I think I'm close I just need a little direction on this one :)
Upvotes: 0
Views: 4673
Reputation: 6517
You got it right the first time. The classmap section is just a list of directories where classes that you want to be autoloaded can be found. You don't need to specify individual class names in this part.
If you look at your generated vendor/composer/autoload_classmap.php file you will see it picked it up:
$ cat vendor/composer/autoload_classmap.php
<?php
// autoload_classmap.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'elvanto_API' => $baseDir . '/elvanto_API.php',
);
You can now use this class in an autoloaded style:
<?php
require_once 'vendor/autoload.php';
$test = new elvanto_API();
Upvotes: 1