I have an extremely simple question - When installing a program via composer (when not part of an existing framework like Laravel), how do I know what to put in the autoload section?
Here's what I've got...
{
"name": "Search",
"description": "Get search results from Solr",
"keywords": [
"search",
"solr"
],
"require": {
"ext-curl": "*",
"solarium/solarium": "3.2.*",
"twig/twig" : "~1.0"
},
"autoload": {}
}
When you use composer you get an PSR-4 autoloader. So when you use install your packages you can include the autoloader.
https://getcomposer.org/doc/01-basic-usage.md
Here is the introduction. There is an autoload section and some good examples.
Related
I am trying to install the asana library through the composer.
Json:
"asana/asana": "^0.10.0" added to composer.json and {
"name": "asana/asana",
"description": "A PHP client for the Asana API",
"type": "library",
"keywords": ["asana", "client"],
"homepage": "https://github.com/Asana/php-asana",
"license": "MIT",
"require": {
"php": ">=5.4.0",
"nategood/httpful": "~0.2",
"adoy/oauth2": "^1.2.0"
},
"require-dev": {
"instaclick/php-code-sniffer": "dev-master",
"phpunit/phpunit": "^9"
},
"autoload": {
"psr-0": {
"Asana\\": "src/"
}
}
}
to composer.lock but getting error 'Package Asana/asana has no version defined.
'
As suggested by #Jeto you should not edit composer.lock manually. To install the library, you can follow the steps mentioned in official docs here.
Assuming that you are doing a fresh install, follow steps below:
Put "asana/asana" package as a dependency in your composer.json file:
{
"require": {
"asana/asana": "^0.10.0"
}
}
Now run the command composer install
composer.lock file will be automatically updated by Composer when installation succeed.
EDIT:
OR
As mentioned by #Jeto in comments, You can simply do this using a single command: composer require asana/asana:^0.10.0
I am in the process in developing my first package. The package is working.
Now I want to use Laravels Collective's HTML in my package (dependencies).
Therefor I added this to the package's composer file:
(this composer.json is in the root of the package: /packages/vendor/package)
{
"name": "vendor/package",
"description": "",
"license": "MIT",
"authors": [
{
"name": "firstname lastname",
"email": "info#domain.nl"
}
],
"minimum-stability": "stable",
"require": {
"twbs/bootstrap": "dev-master",
"laravelcollective/html": "5.3.*"
}
}
I've included the package in Laravel's own composer.json:
...
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Vendor\\Package\\": "packages/vendor/package/src"
}
},
...
When I do composer update the dependencies of the package are not updated. What am I missing here?
You have to define a local composer repository if your package is not uploaded to packagist.org yet. Inside Laravels' application composer.json add a local repository like this:
"repositories": [
{
"type": "path",
"url": "/full/or/relative/path/to/development/my-package"
}
],
"require": {
"my-package": "*"
}
You have to define the package src directory inside the package composer.json and not inside the Laravels' own composer.json. Try and define it like this inside packages' composer.json:
"require" : {
"twbs/bootstrap": "dev-master",
"laravelcollective/html": "5.3.*"
},
"autoload" : {
"psr-4" : {
"PackageNamespace\\PackageClass\\" : "src/"
}
},
UPDATE
In order to do a proper package development, you have to keep your package files at some directory other than /vendor/ composer directory. The main reason is that the developer can delete the entire /vendor directory if there might be conflict problems or for cleaning up and setting up everything under composer /vendor path. I use a very simple method and I keep my own packages at:
<application>/dev/packages/<package-namespace>/<package-name>
You have to initialize a git at your package for composer to recognize the repository. To make a git package, go at your package location and run the following commands:
cd <application>/dev/packages/<package-namespace>/<package-name>
git init
git add *
git commit -m "Initial commit"
You may also have to set the git config user.name and git config user.email before commit, for the git to be able to recognize someone and allow the local commits:
git config user.email "you#example.com"
git config user.name "Your Name"
In my example, the namespace is lytr and the package name is testpkg.
The <application>/dev/packages/<package-namespace>/<package-name>/composer.json (<application>/dev/packages/lytr/testpkg) will look like this:
{
"name" : "lytr/testpkg",
"description" : "Test package of Lytr",
"keywords" : [
"test",
"package"
],
"license" : "MIT",
"require" : {
"twbs/bootstrap" : "dev-master",
"laravelcollective/html" : "5.3.*"
},
"autoload" : {
"psr-0" : {
"Lytr\\TestPkg\\" : "src"
}
},
"extra" : {
"branch-alias" : {
"dev-master" : "1.0-dev"
}
},
"minimum-stability" : "dev"
}
Then at your application <application>/composer.json you'll have a local git repository and your package like this:
"repositories": [
{
"type": "path",
"url": "<full-application-path>/dev/packages/lytr/testpkg"
}
],
"require" : {
"lytr/testpkg": "*"
},
"minimum-stability": "dev",
I include "minimum-stability": "dev", because we are using master-dev versions. Then after running the composer update command only having the "twbs/bootstrap" : "dev-master", at the package requirements, we'll see the following output on the console window:
And after we change the package composer.json and require the "laravelcollective/html" : "5.3.*",, we do a composer update and we see composer installs the laravelcollective/html package properly:
I know this might look confusing and somehow overkill, but this is a proper way for developing packages for composer. You can also have your packages at a git repository and make composer clone that repository instead of your local files. When you're done developing the package and you release it under https://packagist.org/, then you will just have to require your package as any other normal package without the repositories and all the local git thing. Remember, you are at development phase of your package and not at production.
Running into an issue with composer. I have a main project that im working on with some some small libraries I built that I want to more easily share between my projects. They are nowhere near release ready, so I do't want to add them to packagist, but when I require 1 that requires another, it will error unless I ad that custom repository as well on my master composer.json
also, the tertiary requirement can not resolve packagist libraries
Your requirements could not be resolved to an installable set of packages.
Problem 1
- ethereal/simpleCache dev-master requires predis/predis ^1.1#dev -> no matching package found.
- ethereal/simpleCache dev-master requires predis/predis ^1.1#dev -> no matching package found.
- Installation request for ethereal/simplecache dev-master -> satisfiable by ethereal/simpleCache[dev-master].
Main Project composer.json:
{
"name": "ethereal/SimpleTable",
"type": "project",
"repositories": [
{
"type": "vcs",
"url": "https://github.com/mathus13/SimpleConfig.git"
}
],
"require": {
"php": ">=5.3.9",
"doctrine/dbal": "^2.6#dev",
"ethereal/SimpleConfig": "dev-master"
},
"require-dev": {
"phpunit/phpunit": "~4.8"
},
"autoload": {
"psr-4": {
"Ethereal\\": "lib"
}
}
}
config library: when running composer update in SimpleTable, Simple Cache will not be included unless explicitly required in SimpleTable.
{
"name": "ethereal/SimpleConfig",
"type": "project",
"version": "0.0.1",
"repositories": [
{
"type": "vcs",
"url": "https://github.com/mathus13/SimpleCache.git"
}
],
"require": {
"php": ">=5.3.9",
"ethereal/SimpleCache": "dev-master"
},
"require-dev": {
"phpunit/phpunit": "~4.8"
},
"autoload": {
"psr-4": {
"Ethereal\\": "lib"
}
}
}
cache library: when running composer update in SimpleTable, predis can not be resolved.
{
"name": "ethereal/simpleCache",
"type": "project",
"version": "0.0.1",
"require": {
"predis/predis": "^1.1#dev",
"php": ">=5.3.9"
},
"require-dev": {
"phpunit/phpunit": "~4.8"
},
"autoload": {
"psr-4": {
"Ethereal\\": "lib"
}
}
}
ethereal/SimpleTable depends on ethereal/SimpleConfig in dev stability, which depends on ethereal/SimpleCache in dev stability, which depends on predis/predis in dev stability (version 1.1 hasn't been released yet).
Packages included into the main package cannot define any stability, the only stability allowed is the one in the main package. And that is "stable" by default.
You made ONE exception from this rule by depending on "dev-master" for SimpleConfig", but this is not inherited.
You have multiple solutions:
Tag your software. Tags declare it more stable than "dev", and it generally is a good idea to only use tagged software in production.
Include ALL your own packages that are needed in the main package, even if they are not directly used. This will add exceptions from the general stability for them, and allow Composer to resolve any sub dependencies.
You can add "minimum-stability":"dev" to the main composer.json, but this will also allow all other packages to be installed from a branch. Using branches however is a very bad thing, because you cannot easily go back to the version that was working before you did the update - the branch pointer moves only forward. Only tags will point to the same software forever.
Adding "prefer-stable":true" is some sort of workaround for the problem that 3 introduces for packages that are already available in a stable release version. However you still have the problem of not being able to go back to your own packages' earlier versions, because you are using a branch.
If you are still developing these packages, depending on branches may seem necessary. However, a good package will be able to be developed and tested standalone, with barely any foreign code present apart from interface definitions (which will be used to mock everything), so putting all code together into a mixture of repos with branches checked out usually is an invitation for writing code that isn't cleanly separated.
If any of these packages is already done (I'd say "good enough"), tag it and depend on that version instead of a branch. You can always release new versions if you find bugs or want to add new features.
I've inherited a project that was built with PHP 5.3.x, Symfony2, and Composer for dependency management.
The composer.json file has lots of lines like this: "vendorname/library" : "dev-master" for the version of the libraries in use. It was last edited in August of 2012, and clearly worked then since the composer.lock file exists and the project is running on a server at our host.
Thankfully with 1 small tweak to composer.lock, I got composer install to work, but what I'm trying to do now is fix some failures I'm getting when running composer update. There are plenty of posts online about composer dependency hell - and I'm in a leaky boat on the river styx headed there pulling my hair out.
In short, a couple years back when composer.lock was created, the project worked with the then-current versions of "dev" of dozens of included vendor libraries, but now that I am trying to clean up the mess, I'd like to put proper versions into composer.json and try to update things from a known state.
How do I discover what versions actually get installed by composer install? Or what keys/values in the composer.lock file tell you this?
I have plenty of github commit hashes in the composer.lock file but it's not clear given an arbitrary commit hash what the closest tagged version would be to replace that respective line in composer.json with.
Here's an example line from composer.json:
"doctrine/doctrine-bundle" : "dev-master",
and here is the corresponding node in composer.lock for that module:
{
"name": "doctrine/doctrine-bundle",
"version": "dev-master",
"target-dir": "Doctrine/Bundle/DoctrineBundle",
"source": {
"type": "git",
"url": "http://github.com/doctrine/DoctrineBundle.git",
"reference": "d3c930599723c8343472a5791b0f5909a4111a73"
},
"dist": {
"type": "zip",
"url": "https://github.com/doctrine/DoctrineBundle/zipball/d3c930599723c8343472a5791b0f5909a4111a73",
"reference": "d3c930599723c8343472a5791b0f5909a4111a73",
"shasum": ""
},
"require": {
"doctrine/dbal": ">=2.2,<2.4-dev",
"php": ">=5.3.2",
"symfony/doctrine-bridge": "2.1.*",
"symfony/framework-bundle": "2.1.*"
},
"require-dev": {
"doctrine/orm": ">=2.2,<2.4-dev",
"symfony/validator": "2.1.*",
"symfony/yaml": "2.1.*"
},
"suggest": {
"doctrine/orm": "The Doctrine ORM integration is optional in the bundle."
},
"type": "symfony-bundle",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-0": {
"Doctrine\\Bundle\\DoctrineBundle": ""
}
},
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien#symfony.com"
},
{
"name": "Benjamin Eberlei",
"email": "kontakt#beberlei.de"
},
{
"name": "Symfony Community",
"homepage": "http://symfony.com/contributors"
}
],
"description": "Symfony DoctrineBundle",
"homepage": "http://www.doctrine-project.org",
"keywords": [
"DBAL",
"Database",
"ORM",
"Persistence"
],
"support": {
"source": "https://github.com/doctrine/DoctrineBundle/tree/master",
"issues": "https://github.com/doctrine/DoctrineBundle/issues"
},
"time": "2012-09-10 15:12:44"
}
I am guessing that composer installs the dist->url or source->url from composer.lock, but I have several dozen modules to go through and wondering how to find the closest (by date) tag for each referenced library to create a sane composer.json file to move forward with updating our code.
First you need to find out which packages are dependent on a dev-master version.
composer show -i
This will list all your packages along with the version installed. Something like this:
symfony/http-foundation dev-master 1234abc
symfony/http-kernel v2.5.7
You will see some of the packages are listed as having the version dev-master <commit>. Take note of the names of these packages.
Now you can make it a bit easier on yourself by installing the source code for the packages in your vendor directory.
composer install --prefer-source
Now for each package you noted above, cd into the package directory and find the latest tag.
cd vendor/symfony/http-foundation
git describe # Shows the latest tag
Now you can use that tag to determine which version you want to install. For example if git describe returned v2.2.3, you could change the version number in your composer.json to 2.2.*.
"symfony/http-foundation": "2.2.*"
This part could be tricky if the latest tag is "far away" from the installed commit. If you run into too many problems, you can always install an exact commit hash by putting dev-master#<commit> into your version requirement.
"symfony/http-foundation": "dev-master#1234abc"
Thanks to other answers I start digging and found that you can have useful informations with:
composer show -t
It will produce a dependency tree, and next to every package there will be version.
I'm trying to address this issue now for a long time but still could not figure out what's my mistake.
I've got two repositories I want to combine. The Application and the Framework Core.
// Application
- composer.json
- public/
- CCF/
- core/ <- here the core package should go
- vendor/ <- here it goes instead
Now basically I want to create a composer package that install's into CCF/core/ instead of CCF/vendor/clancats/core/etc..
So I created a custom installer at:
vendor/composer/installers/src/Composer/Installers/ClanCatsFrameworkInstaller.php
class ClanCatsFrameworkInstaller extends BaseInstaller
{
protected $locations = array(
'core' => 'CCF/core/',
'orbit' => 'CCF/orbit/{$name}/',
);
}
The composer.json of the core
{
"name": "clancats/core",
"description": "The ClanCatsFramework Core repository",
"type": "clancatsframework-core",
"license": "MIT",
"require": {
"php": ">=5.3",
"composer/installers": "~1.0"
}
}
composer.json of the application that should implement the core at CCF/core
{
"type": "project",
"require": {
"php": ">=5.3",
"clancats/core": "dev-master"
},
"config": {
"vendor-dir": "CCF/vendor"
},
}
But after all that composer still installs the core package at /vendor/clancats/etc..
So my question is what is my mistake that composer won't install the core package to CCF/core/?
Added composer/installers to the requires.
I created a custom installer which defines the path to the core
Set the type of the core package to my custom one.
Another question that is spinning around my head, is my pattern wrong? I mean did i misunderstand how to use composer?
Github:
framework install repo: https://github.com/ClanCats/Framework
core repo: https://github.com/ClanCats/Core
Thanks for your help guys :)
So for everyone who runs into the same problem:
My mistake was that i didn't require the composer/installers in the main repository.
The composer installers have to be required in both repositories.
So in this case:
{
"type": "project",
"require": {
"php": ">=5.3",
"clancats/core": "dev-master",
"composer/installers": "~1.0"
},
"config": {
"vendor-dir": "CCF/vendor"
},
}
solves the problem.