In a composer file, what does "reference" refer to? - php

In my composer.lock file, I noticed that some packages have a reference value:
"dist": {
"type": "zip",
"url": "https://ftp.drupal.org/files/projects/paragraphs-8.x-1.1.zip",
"reference": "8.x-1.1",
"shasum": "c678e5704a98c6a0549e415412da081cfeb03a00"
},
And some just have null:
"dist": {
"type": "zip",
"url": "https://ftp.drupal.org/files/projects/redirect-8.x-1.0-alpha5.zip",
"reference": null,
"shasum": "927aa4c8d8b40b0cd2442bee86f2f386d25e53ca"
},
What does the value refer to? I thought it refers to a commit, but both of these are zip packages, where 1 has a reference and the other does not.

I have checked the code of both modules and read some articles. After a bit of research and discussing with JS developers, I came to know that 'reference' in the composer file denote the PHP library tag, branch or zip file. So for example, if I say my package reference is from "reference":"master" then this will pull the code from that repository every time I run the composer update command. Defining a reference is a way to omit the requirement of adding a composer file to the library itself. But if your library is already supporting the composer using a composer.json file within its own directory then you won't need to define the package in the composer file.
Now, let's come to both these modules. First, check the source tree of the Paragraph module at http://cgit.drupalcode.org/paragraphs/tree/?h=8.x-1.x, and you will notice that no composer.json file available there so we must need to define the reference parameter in composer file to tell the application to pick the correct source files. But on the other hand, if you see the code source tree of the redirect module at http://cgit.drupalcode.org/redirect/tree/, you will found a composer.json at the root of the file. This file will allow you to omit the reference parameter from your application composer.json file.
Also, I think if we don't define this parameter the latest will be pulled in and based on the above criteria composer.lock file gets updated upon composer install command to run.
Hope this will clear your doubts!

Related

composer config add repository location without the name

Is there any way to use composer CLI command to add a repository location to composer.json but without stating its name?
You can do:
composer config repositories.<name> vcs /path/to/the/repository
But I'm interested in adding repository location without specifying its name.
Since repositories' locations can be added in composer.json without their names:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/igorw/monolog"
}
],
"require": {
"monolog/monolog": "dev-bugfix"
}
}
I'm interested to add such entry:
{
"type": "vcs",
"url": "https://github.com/igorw/monolog"
}
by using composer CLI but command:
composer config repositories vcs /path/to/the/repository
fails.
Do you know how to do that?
It's not possible at this point. If you take a look at the regex that parses your repositories argument (or repo/repos):
// handle repositories
if (preg_match('/^repos?(?:itories)?\.(.+)/', $settingKey, $matches)) {
https://github.com/composer/composer/blob/1.7.2/src/Composer/Command/ConfigCommand.php#L535
You see it's expecting the repositories.name format. My guess would be that while adding them anonymously would be fine if you don't already have repositories defined, it would break if you started trying to name some of them. Also the unset command wouldn't work because you haven't named it, and running the command repeatedly would continue to add them.
My guess is that it's a deliberate move not to support this. You could always open an issue on the repository to request it though.
I can't find a way to do that. I would just add it manually (without the cli tool). I did try using composer config repositories._ vcs /url. It worked, but it obviously set the name to be "_". Then you can go in and delete that. But if you are doing that anyway, you might as well just add it manually anyway.

Local "path" repository doesn't see vcs version information

I'm currently trying to setup a better local development without putting information about my local structure into a project itself or into the composer.json of this project.
I found out, that there is a new way to do this by using a config.json and adding a "repositories" section to it, that points to my local projects I depend on.
My setup
So I setup two projects with a few versions (on Win10 with IIS 10 with PHP 7.0), lets call them php-project1 and php-project2. php-project1 depends on php-project2 and I want to develop both of them at the same time. Changes I make in php-project2 should therefore directly influence the first project. In addition they are git repositories and they contain tags as composer versions. They are also on packagist, but of course I want to use the local version for development.
Their composer.json look like this (2 files):
{
"name": "test/php-project1",
"description": "Some text here",
"type": "library",
"require": {
"test/php-project2": "^0.3",
}
}
{
"name": "test/php-project2",
"description": "Some other text here",
"type": "library"
}
Please note that there is no "version" entry, because I want to use this from the git tags.
Now I tried to setup a global config.json like this:
{
"repositories": [
{
"type": "path",
"url": "/somePath/php-*"
}
]
}
I'm using php-* here because I want to use it later for all my repositories/projects with this pattern.
So what's the problem now?
When running composer update on the first project it doesn't use the local version of php-project2, but instead downloads it from packagist. This only works when I add a "version" entry to the composer.json, but I don't want this!
I also tried "type": "vcs", but this creates a clone and I need a symlink, to have changes directly in other projects too.
The documentation says:
If the package is a local VCS repository, the version may be inferred by the branch or tag that is currently checked out. Otherwise, the version should be explicitly defined in the package's composer.json file.
Currently php-project2 is on tag 0.3.0 without any changes.
So what do I do wrong?
Found my mistake with help of alcohol (this sounds wrong though^^).
When you use this and need a symlink, be sure to add a "version" entry to your composer.json or (in my case) checkout the tag (not the branch!), so composer may see, that your used version matches the required.

Is there a way to retain certain files during a composer update or install?

I'm using a package in my Laravel application that doesn't have support for Laravel out of the box, so I added a Facade and Service Provider to it myself. Both of those files are checked into my repo (specified in .gitignore using the ! prefix). However, when composer updates a package it completely removes it first, and this is preventing automatic deployment with platforms like Codeship.
Any suggestions?
You should fork the package, create a custom repo with your changes - then include that in your composer.json.
{
"repositories": [ {
"type": "vcs",
"url": "https://github.com/YourGithubUsername/PackageName"
}
}],
"require": {
"laravel/framework": "4.0.*",
"OriginalVendor/PackageName": "1.0.*"
},
}
That way you can pull your custom changes in anytime, without having to commit it to your specific project.
You can read more about forking and loading packages here: https://getcomposer.org/doc/05-repositories.md#vcs
Never change the code of packages! In fact, never touch the vendor directory.
You should either [fork] the repository and add your code (see #TheShiftExchange's answer)
Or add the Facade and the ServiceProvider in your namespace.

Using PHP's composer for the first time

I'm trying to get my head around using PHP's composer. In particular, I'm trying to load this project and its dependencies.
I am installing composer in the necessary folder (root of my application), so it creates a composer.phar.
I make sure I have the correct JSON file for the project in that same directory:
{
"name": "tumblr/tumblr",
"description": "Official Tumblr PHP Client",
"keywords": ["tumblr", "api", "sdk", "gif"],
"homepage": "https://github.com/tumblr/tumblr.php",
"authors": [{
"name": "John Crepezzi",
"email": "john.crepezzi#gmail.com",
"homepage": "https://github.com/seejohnrun",
"role": "developer"
}],
"license": "Apache-2.0",
"type": "library",
"require": {
"eher/oauth": "1.0.*",
"guzzle/guzzle": ">=3.1.0,<4"
},
"require-dev": {
"phpunit/phpunit": "*"
},
"autoload": {
"psr-0": {
"Tumblr\\API": "lib"
}
}
}
I then write this in the appropriate directory with the terminal: php composer.phar install.
However, this does not load the tumblr.php. It loads other files, such as symphony, sebastian, guzzle, etc.
Am I doing this incorrectly? Does this JSON not load the tumblr.php, but its dependencies?
Composer generates a file vendor/autoload.php. If you want to use any of the things you installed with Composer in your own code, you just need to require_once 'vendor/autoload.php' and can then simply call whatever code in whatever Composer-installed library you want; the autoloader will take care of locating and including the necessary files without you having to worry about particular directories inside the vendor folder.
The autoload entry in the composer.json file is there so any library can specify particulars of how its files should be autoloaded. You do not typically have to use that yourself in your application for anything. You may use this entry to add autoloading for your own code "for free" if you wish. However, again, you do not need to add this to use any of the installed dependencies, those should all already be configured correctly for autoloading in their respective composer.json files.
If the JSON data you show is in your OWN project, you are doing it wrong. That JSON data is from the original Tumblr project, and you shouldn't copy it into your project, because as you found out, it will not really help you using the Tumblr client.
The correct way of using Composer:
Start your project by having a main directory (probably already with files).
Run composer init to easily create the initial composer.json file.
You will be asked if you want to include dependencies. You may answer yes and add tumblr/tumblr as the dependency of your project.
Alternatively, if you already have a composer.json, you can call composer require tumblr/tumblr:~0.1.
To use the library in your code, you have to include the file vendor/autoload.php and can then create all the classes according to the documentation.

How to use composer to install part of a git repository?

I want to require only a sub portion of a git repository (instead of the full thing). The reason i want to do this is because the repository is huge.
In my case the repository is: https://github.com/pubnub/pubnub-api.git and I only want the /php directory.
I have the following package defined in composer:
{
"type": "package",
"package": {
"name": "pubnub",
"version": "dev-master",
"source" : {
"url": "https://github.com/pubnub/pubnub-api.git",
"type": "git",
"reference":"master"
}
}
},
Any tips?
Given the answer to "Is there any way to clone a git repository's sub-directory only?" Is "No" , and sub directory checkout functionality would be required by composer to satisfy the desired functionality, then I would suggest the best composer could do was checkout the whole thing and then delete what you didnt want.
In short: Not Possible.
The longer answer:
is that git can do a sparse checkout so in theory composer could someday support that feature. You can use the autoload field to only load the section of code you want (ie, not load the whole lib).
It seems that the php directory has been moved on its own repository here https://github.com/pubnub/php. That may help for this project, but no idea how to achieve this from main repo and composer...

Categories