I have 2 packages on Packagist.
https://packagist.org/packages/erayalakese/envato-market-api (A)
https://packagist.org/packages/erayalakese/envato-update-checker (B)
B requires A.
Now I'm using B package on my projects. But I'm getting Class 'erayalakese\Envato_Update_Checker' (package B) not found error.
This is my composer.json file
{
"name": "",
"description": "",
"require": {
"erayalakese/envato-update-checker": "^1.3"
},
"authors": ...
}
And my project file:
<?php
require_once(__DIR__.'/vendor/autoload.php');
new erayalakese\Envato_Update_Checker(...);
When I add this to my composer.json as temporary solution, it's working :
"autoload": {
"classmap": ["vendor/"]
}
But I'm not sure I really need to add vendor folder to autoload . I was expecting it will autoload my vendors automatically.
Can you tell me what's I'm missing?
You have to change the composer.json of both packages.
Both packages need to define a autoload section.
Referencing: https://getcomposer.org/doc/04-schema.md#classmap
erayalakese/envato-market-api
https://github.com/erayalakese/envato-market-api/blob/master/composer.json
{
"name": "erayalakese/envato-market-api",
"description": "Envato Market API to verify and download Envato purchases",
"authors": [
{
"name": "Eray Alakese",
"email": "erayalakese#gmail.com"
}
],
"require": {},
"license": "GPL v2",
"autoload": {
"classmap": ["Envato_Market_API.php"]
}
}
Now this package has a autoload classmap definition, which consists of one PHP file. When you composer install, the autoload definition of the package will be added to the Composer Autoloader.
Same game for the other package:
erayalakese/envato-update-checker
https://github.com/erayalakese/envato-update-checker/blob/master/composer.json
{
"name": "erayalakese/envato-update-checker",
"description": "Checks Envato WordPress plugins' updates and download its if any update available",
"require": {
"erayalakese/envato-market-api": "^1.0"
},
"authors": [
{
"name": "Eray Alakese",
"email": "erayalakese#gmail.com"
}
],
"license": "GPL v2",
"autoload": {
"classmap": ["Envato_Update_Checker.php"]
}
}
In your main project:
require the "updater" package in the composer.json of your main project
the updater packages included the api package via it's require section (so you get both)
add require_once(__DIR__ . '/vendor/autoload.php'); to the project bootstrap
enjoy Class via Composers Autoloader: new erayalakese\Envato_Update_Checker(...);
Remove this line:
https://github.com/erayalakese/envato-update-checker/blob/master/Envato_Update_Checker.php#L11
Related
I have created a package to separate out business logic into easier to distribute modules. The composer file looks like this:
{
"name": "aggiq/johnny-cash",
"description": "A collection of controllers, models, migrations, and tests for a phonebanking backend.",
"license": "MIT",
"authors": [ ... ],
"require": {
"illuminate/database": ">=5.5"
},
"require-dev": {
"fzaninotto/faker": "~1.4"
},
"autoload": {
"psr-4": {
"Johnny\\Phonebanking\\": "src/"
}
}
}
And our source files are indeed in src/:
src/Controllers/PhonebankController.php
src/Models/Phonebank.php
...
I saved and pushed this to our gitlab repo, and then included it as a dependency in a test project:
{
...,
"repositories": [{
"type": "package",
"package": {
"name": "aggiq/johnny-cash",
"version": "0.1",
"type": "package",
"source": {
"url": "gitlab url",
"type": "git",
"reference": "dev"
}
}
}],
"require": {
"aggiq/johnny-cash": "*",
},
...
}
And when I do composer update, it successfully grabs the project and downloads it into the vendor folder:
vendor/aggiq/johnny-cash/Controllers/PhonebankController.php
...
However, when I look in the test project's autoload_psr4.php, it's not there. Is there a step I missed?
Edit: updates the directories to have capital letters to match the namespaces, and here is the generated PSR4 php file:
<?php
// autoload_psr4.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
);
You have registered autoloading at your package's composer.json correctly:
"autoload": {
"psr-4": {
"Johnny\\Phonebanking\\": "src/"
}
}
This means, any class in Johnny\Phonebanking namespace will be in src directory. E.g.:
Johnny\Phonebanking\SomeClass => src/SomeClass.php
Johnny\Phonebanking\SomeNamespace\AnotherClass => src/SomeNamespace\AnotherClass.php
As you can see, it has to respect CapitalLetters.
Saying that, you should correct first letters of your directories, from:
src/controllers/PhonebankController.php
src/models/Phonebank.php
to
src/Controllers/PhonebankController.php
src/Models/Phonebank.php
I have solved it. We needed to do two things.
The repository type specified in the parent pacakge should be vcs not package since we are loading from a git server:
"repositories": [{
"type": "vcs",
"url": "git#xxx.git"
}]
The package type in the child package should be library:
"type": "library"
Once those two changes were made, composer update installed not only the child package but also its dependencies, proving it is being recognized by composer.
My problem is that a privately made repo's composer.json seems to be broken when trying to use it as a package elsewhere.
I have a private repo with code needed for other projects. The repo's composer.json looks like this:
{
"name": "somevendor/global",
"require": {
"nesbot/carbon": "^1.21"
},
"autoload": {
"psr-4": {
"" : "src/"
},
"files": [
"somedir/somefile.php"
]
}
}
The src is in the base directory of the repo, and contains PSR-4 namespaced classes. I have namespace folders within that, e.g. a Foo directory with classes in the Foo namespace:
-- src
-- Foo
// some Foo\... classes
// some global namespace classes
-- somedir
somefile.php // A file with helper functions
In the project folder, I'm accessing the somevendor/global repo via a composer.json file:
{
"require": {
"somevendor/global-folder": "dev-master"
},
"repositories": [
{
"type": "package",
"package": {
"name": "somevendor/global",
"version": "dev-master",
"type": "package",
"source": {
"url": "git#bitbucket.org/somevendor/global.git",
"type": "git",
"reference": "master"
}
}
}
]
}
Running composer install in the project folder seems to work at first. I have installed SSH keys properly so it can access the private repo on Bitbucket and grab the files:
$ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing somevendor/global (dev-master master)
Cloning master
Writing lock file
Generating autoload files
And then in the project's PHP code I require vendor/autoload.php, but none of the classes are being autoloaded, including the Carbon package specified in the first repo's composer.json file:
Fatal error: Uncaught Error: Class 'Foo\Foo' not found in...
I've clearly made a mistake here, have I structured the first repo wrongly?
I "solved" this by taking out all of the "require" entries from the remote repo's composer.json file and moving them to the local website's composer.json file.
This is what the files looked like:
The remote private repo's composer.json:
{
"name": "somevendor/global",
"license": "proprietary",
"autoload": {
"psr-4": {
"" : "src/"
},
"files": [
"functions/functions.php"
]
}
}
The local website's composer.json:
{
"require": {
"nesbot/carbon": "^1.21",
"somevendor/global": "dev-master"
},
"repositories": [
{
"type": "vcs",
"url": "git#bitbucket.org:somevendor/global.git"
}
]
}
It kept throwing Composer\Repository\InvalidRepositoryException because I forgot to put the name into the remote repo's composer.json file, so don't forget that bit!
Also remember to set up your git ssh keys if you've set them up. I used this Bitbucket tutorial to do this.
I want to make a composer package. However, I am still in the development phase, would or would but the earlier test out.
I have an empty vendor folder with the autoloader from composer:
/vendor
/composer
autoload.php
So now I've tried my package "simulate" and creates my folder structure and composer.json:
/vendor
/composer
/me
/package
/src
/tests
composer.json
autoload.php
This is my composer.json:
{
"name": "me/package",
"description": "",
"license": "",
"authors": [
{
"name": "",
"email": ""
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.4.0"
},
"autoload": {
"psr-4": {
"Me\\Package\\": "src/"
}
}
}
And here is my class:
namespace Me\Package;
class Test {
// ...
}
If I want to call it:
if(file_exists('vendor/autoload.php')) require 'vendor/autoload.php';
$test = new \Me\Package\Test();
i become Fatal error: Class 'Me\Package\Test' not found.
Of course, I also inserted a composer.json in the root directory, but I can still bad at require my package state since it was not published, right? But how do I test it then and say to composer he should autoload my package?
If you want to use composer to include a package that is not listed on http://Packagist.org/ you would add a 'repositories' stanza into the composer.json (project root file). This reads the project, and gets the composer.json from it, using the name for the main-'requires' section.
"repositories": [
{
"type": "vcs",
"url": "https://github.com/example/private-repo.git"
}
}
The 'url' part, can also in fact be any valid URL for a git, SVN or HG repository - even a file:// based reference.
I'm struggling to work out how to correctly build and autoload my first composer package. I understand that if I follow the PSR-0 naming convention, autoloading should "just work". I am trying to set up with:
vendor: Programster
package name: CoreLibs
I have managed to debug a project that includes the package to the point where I am outputting the search path by putting a print in the findFileWithExtension() function of the ClassLoader.php file
/home/stuart/Desktop/test-project/vendor/Programster/CoreLibs/Programster/Programster/CoreLibs/Core.php
I know that the file is actually located at:
/home/stuart/Desktop/test-project/vendor/Programster/CoreLibs/Core.php
My Package's composer.json file:
{
"name": "Programster/CoreLibs",
"type": "library",
"description": "Core libraries for PHP 5.3+",
"keywords": ["core","library"],
"homepage": "http://svn.yadda-yadda.com/php/core-libs",
"license": "none",
"authors": [
{
"name": "xxxx",
"email": "my.email#email.com",
"homepage": "http://mywebsite.com/",
"role": "Developer"
}
],
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-0": {
"Programster": "Programster"
}
}
}
The following is a tiny script in a project that includes the package to test if is working:
<?php
require_once(__DIR__ . '/../vendor/autoload.php');
$loader = new \Composer\Autoload\ClassLoader();
$loader->register();
use \Programster\CoreLibs as programster;
programster\Core::println("hello world");
The project's composer.json file to include the package:
{
"repositories": [ { "type": "composer", "url": "http://satis.mydomain.com/" } ],
"require": {
"Programster/CoreLibs": "dev-trunk"
}
}
Is my package's config wrong, or do I really need to create Programster/CoreLibs/ subdirectories within the CoreLibs repository source code?
use \Programster\CoreLibs as programster;
programster\Core::println("hello world");
Your class name evaluates to \Programster\CoreLibs\Core.
With this PSR-0 autoloading,
"psr-0": {
"Programster": "Programster"
}
the class name will be completely converted into a path name like "Programster/CoreLibs/Core.php", and this path will be searched inside the directory "Programster" relative to your libraries composer.json location.
Using PSR-4 autoloading,
"psr-4": {
"Programster\\CoreLibs\\": ""
}
the prefix in this definition will be removed from the classname, the remainder will be converted to a path (i.e. "Core.php") and be searched in the path mentioned (in this case, in the main directory of the library, because "" + "Core.php" is pointing to a file in without any sub directory).
Your second definition is better in some regards. It uses a longer prefix, which is relevant if you include more than one library using the same prefix, because then Composer would have to search more than one path to find the class. And it uses a shorter path, which also allows for slightly faster disk I/O operations (I haven't done any performance measurements, though).
I managed to get it working by using the PSR-4 standard that others suggested in the comments and updating my package's composer.json file (see the autoload section):
{
"name": "Programster/CoreLibs",
"type": "library",
"description": "Core libraries for PHP 5.3+",
"keywords": ["core","library"],
"homepage": "http://svn.mydomain/core-libs",
"license": "none",
"authors": [
{
"name": "MY name",
"email": "my.email#email.com",
"homepage": "http://my-website.com/",
"role": "Developer"
}
],
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-4": {
"Programster\\CoreLibs\\": ""
}
}
}
This is my script in the project that has the package installed:
require_once(__DIR__ . '/../vendor/autoload.php');
\Programster\CoreLibs\Core::println("hello world");
#sectus was right, I did not need to use $loader = new \Composer\Autoload\ClassLoader();$loader->register();
I'm trying to create a composer package but I'm getting a class not found error. I'm using Laravel 4 but I'm trying to create a generic PHP package.
I created the package in my vendor/ directory.
vendor/
theninthnode/
defaqto/
src/
Defaqto.php
vendor/
composer.json
Here is my composer.json file
{
"name": "theninthnode/defaqto",
"description": "",
"authors": [
{
"name": "Billy Jones",
"email": "billyjones26#gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.3.0"
},
"autoload": {
"classmap": ["src"]
}
}
And my class:
<?php
class Defaqto
{
....
}
When I try to call the package class from my app like so
$defaqto = new Defaqto();
I just get
Class 'Defaqto' not found
I've tried composer dump-autoload from within my package and also my applications root.
I also tried adding theninthnode/defaqto to my root composer.json file.
Am I missing something?