I've tried everything classmap, psr-0, psr-4 but can't seem to get autoload to work for the when using a package from a git repository
{
"repositories": [
{
"type": "package",
"package": {
"name": "michaeljs1990/bitcoin-php-api",
"version": "dev-master",
"source": {
"url": "https://github.com/michaeljs1990/Bitcoin-PHP-API",
"type": "git",
"reference": "master"
},
"autoload": {
"psr-0": {"Bitcoin": "src/Bitcoin"}
}
}
}
],
"require": {
"michaeljs1990/bitcoin-php-api": "dev-master"
}
}
The class is defined in src/Bitcoin directory using namespace Bitcoin but this always fails
<?php
include 'vendor/autoload.php';
$test = new \Bitcoin\Bitcoin($null);
?>
The repository you are referring already has a composer.json file, so there is no need to use type:package for your entry in repositories, it is easier to simply use:
{
"type": "vcs",
"url": "https://github.com/michaeljs1990/Bitcoin-PHP-API"
}
This will use the Composer data directly from that repository, and it will work, because you declare the autoloading wrong:
"autoload": {
"psr-0": {"Bitcoin": "src/Bitcoin"}
},
The original one:
"autoload": {
"psr-0": { "": "src/" }
},
For optimal performance, this should be used:
"autoload": {
"psr-0": { "Bitcoin": "src/" }
},
What's the difference? PSR-0 needs the prefix it should try to search, and the directory from which to start searching for the complete classname converted into a pathname. A class named Bitcoin\Bitcoin will be expected in the relative path Bitcoin/Bitcoin.php.
Your autoloading told Composer that classes with Bitcoin could be found in src/Bitcoin, which is wrong for this class: src/Bitcoin/ + Bitcoin/Bitcoin.php does not exist.
The original autoloading tells Composer that ANY class may be found in src/, which is also wrong for most of them, but true for that Bitcoin class. Even though this works, it will try to search for plenty of other classes inside that directory before searching in different directories, thus wasting disk I/O.
My suggested optimum restricts this directory to classes starting with Bitcoin.
Related
In my following composer.json I am requiring extensions, which are in the same Git repository as the whole project. So I add in the repositories section and later I do composer req vendor/site_package:#dev in order to require my local extension.
Now I realized, that some classes of the extension are not autoloaded.
Do I need to additional add the autoload part as shown below in the composer.json of the project?
{
"name": "site-package",
"description": "Base composer.json",
"repositories": [
{
"type": "path",
"url": "./packages/*"
}
],
"require": {
"typo3/cms-backend": "^10.4",
"typo3/cms-belog": "^10.4",
"typo3/cms-beuser": "^10.4",
"typo3/cms-core": "^10.4",
...
"vendor/site_package": "#dev",
"georgringer/news": "^8",
...
},
"autoload": {
"classmap": [
"public/typo3conf/ext/site_package/Classes"
],
"psr-4": {
"Vendor\\SitePackage\\": "public/typo3conf/ext/site_package/Classes"
}
},
"extra": {
"typo3/cms": {
"root-dir": "public",
"web-dir": "public"
}
},
"config": {
"vendor-dir": "vendor",
"bin-dir": "bin"
},
"scripts": {
"typo3-cms-scripts": [
"typo3cms install:generatepackagestates",
"typo3cms install:fixfolderstructure"
],
"post-autoload-dump": [
"#typo3-cms-scripts"
]
}
}
In ext:site_package I have the following autoload section as well:
"autoload": {
"psr-4": {
"Vendor\\SitePackage\\": "Classes",
}
},
Do I need both? Why?
You should never mix those 2 composer.json.
As you already follow the approach of having a directory packages (which is a good thing) each extension inside there needs an own composer.json file which of course also needs a section
"psr-4": {
"Vendor\\MyExt\\": "Classes"
}
By requiring this package, this autoload information will be used.
If you would still have the custom extension inside typo3conf/ext/my_ext, that composer.jsonfile would not be taken into account and you need something like
"psr-4": {
"Vendor\\MyExt\\": "typo3conf/ext/myext/Classes"
}
The autoload part is only needed in your site package composer.json. It's not necessary to put it also in the composer.json in your root folder.
Please refer to the documentation how the composer.json of your site package should look like.
If you still have problems with autoloading, try composer dump-autoload or remove the extension and require it again. And make sure to check the upper-/lowercase of your namespace. It's case sensitive. If you change that after you required the site package, you need to remove and require the package again.
In my composer.json I have written:
"autoload": {
"psr-4": {
"Pmochine\\MyOwnPackage\\": "src/"
},
"files": [
"src/helpers.php"
]
},
But somehow even after composer dump-autoload the functions are not loaded. I get "Call to undefined function". To create the package I used a package generator. Maybe it has something to do that it creates a symlink in the vendor folder?
Inside helpers I have written
<?php
if (! function_exists('myowntest')) {
function myowntest()
{
return 'test';
}
}
In the package service provider, try adding this:
// if 'src/helpers.php' does not work, try with 'helpers.php'
if (file_exists($file = app_path('src/helpers.php'))) {
require $file;
}
What you are doing is best practise and should work. I took the composer.json from barryvdh/laravel-debugbar as an example. https://github.com/barryvdh/laravel-debugbar/blob/master/composer.json
{
"name": "barryvdh/laravel-debugbar",
"description": "PHP Debugbar integration for Laravel",
"keywords": ["laravel", "debugbar", "profiler", "debug", "webprofiler"],
"license": "MIT",
"authors": [
{
"name": "Barry vd. Heuvel",
"email": "barryvdh#gmail.com"
}
],
"require": {
"php": ">=5.5.9",
"illuminate/support": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*",
"symfony/finder": "~2.7|~3.0",
"maximebf/debugbar": "~1.13.0"
},
"autoload": {
"psr-4": {
"Barryvdh\\Debugbar\\": "src/"
},
"files": [
"src/helpers.php"
]
}
}
My guess is that you are not requiring your own package the correct way in the main composer.json?
Just need to call in your main project
composer update you/your-package
Source
The only thing that has worked for me is to run composer remove <vendor>/<package> and require it back again. The files section was ignored otherwise.
This seems to happen while developing locally the package and making changes on the composer.json file.
With my clean Laravel 5.3 installation, I can run composer install to install the dependent packages.
Now, I've an internal package with its own composer.json, like below:
{
"name": "bar/foo",
"description": "A package for handling foo",
"licence": "MIT",
"authors": [
{
"name": "A. Foo",
"email": "a#foo.bar"
}],
"minimum-stability": "dev",
"require": {},
"autoload": {
"psr-4": {
"Foo\\Bar\\": "packages/foo/Bar/src"
}
}
}
So I prefer to autoload from the package itself, instead of autoloading from the main composer.json.
My questions:
Running composer dumpa from packages/foo/Bar doesn't take effect for autoloading. After Generating autoload files, Laravel doesn't know namespace Foo\Bar
Is there a way to run composer dumpa for all recursive composer.jsons?
You need to add the following section to your global composer.json
"repositories": [
{
"type": "path",
"url": "packages/*/*"
}
]
You also need to add the packages to the require object in composer.json
For a project I'm working on I would like to create a 'Core' package containing multiple smaller packages, like laravel does with it's framework.
The folder structure would be something like this,
Package1: gybrus/core/src/Gybrus/Package1
Package2: gybrus/core/src/Gybrus/Package1
After doing some research I've noticed this could be achieved with composer if I'm not mistaken but this is also where it breaks for me.
Currently I have multiple composer.json files, but after running the 'php artisan dump-autoload' command the classes aren't added to the autoload files.
Therefore I'm wondering if the Laravel framework adds some extra magic to make this happen.
Thanks in advance!
This is my current setup, I've changed the package names for the sake of not advertising something ;)
The first composer file is in the 'core' folder next to the 'src' folder.
{
"name": "gybrus/core",
"description": "The Core",
"keywords": ["core"],
"authors": [
{
"name": "Kevin Dierkx",
"email": "email#email.com"
}
],
"require": {
"php": ">=5.3.0",
"laravel/framework": "4.0.x"
},
"replace": {
"gybrus/package1": "self.version"
},
"require-dev": {
"mockery/mockery": "dev-master",
"phpunit/phpunit": "3.7.*"
},
"autoload": {
"psr-0": {
"Gybrus": "src/"
}
},
"minimum-stability": "dev"
}
The second composer file is in the package1 folder:
{
"name": "gybrus/package1",
"authors": [
{
"name": "Gybrus",
"email": "email#email.com"
}
],
"require": {
"php": ">=5.3.0",
"illuminate/support": "4.0.x"
},
"autoload": {
"psr-0": {"Gybrus\\Package1": ""}
},
"target-dir": "Gybrus/Package1",
"minimum-stability": "dev"
}
Found the cause of my problem!
The first script tells composer that it should autoload the namespace 'Gybrus' starting in the 'src' folder, after some testing this works as intended.
Where the above setup breaks is the following line:
return Finder::create()->files()->in($workbench)->name('composer.json')->depth('< 3');
This tells the Finder to stop looking for composer.json files that are deeper than 2 folders.
Nothing weird so far.
Where is goes wrong is here, I symlinked the workbench packages into the workbench folder.
This causes the weird problem that the composer.json files are actually deeper than they should be, which in result stop the loading of the composer.json files and breaking the autoloading for these packages.
A quick fix would be to either don't symlink or run composer install from inside the package.
I use a local repository ("depA") within my projects composer.json:
"repositories": [
{
"type": "package",
"package": {
"name": "marc/depA",
"version": "dev-master",
"source": {
"url": "/Users/Marc/Sites/depA",
"type": "git",
"reference": "develop"
}
}
}
],
"require": {
"marc/depA": "dev-master",
This works like a charm but it won't resolve dependencies from "depA". This means since "depA" requires "depB" (in composer.json of "depA") -> "depB" wont be installed.
Is this even possible with local packages?
Thanks,
Marc
You are defining the package inline so if you do it like this you must redefine all the requires etc inline as well. That's really not the best way to go at it. If it's a git repo and it has a composer.json you'd better use a vcs repository e.g.:
{
"repositories": [
{
"type": "vcs",
"url": "/Users/Marc/Sites/depA"
}
],
"require": {
"marc/depA": "dev-master",
}
}