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.
Related
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!
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.
The files directory is:
$ cd src/lib/Rpc/
$ ls
a.php b.php c.php
Then, I write the line in composer.json file:
autoload: {
psr-4: {
Rpc\\: src/lib/Rpc/
}
}
Now I want to load the file which it will be loaded the first when load Rpc lib.
Because the file needs to set global function, the content of file:
<?php
Rpc\Chan::setDefaultVar();
So, how should I write the composer.json file?
Ok, lets do this...
a) Syntax of your composer.json file is invalid.
You can check the syntax of your composer.json file by using the command composer validate composer.json on the CLI. It will give you some details about the missing properties and the syntax in general.
You minimal composer.json file should include a name, description and a license - but validate will tell you.
I've fixed your composer.json syntax and added the missing properties - to get you started.
{
"name": "thinkerou/rpc-project",
"description": "description for my rpc-project",
"license": "MIT",
"autoload": {
"psr-4": {
"Rpc\\": "src/lib/Rpc/"
}
}
}
b) The Autoloading problem
Its a bit hard to understand your question, because of the language barrier, but
i understand it like this: you want to load one file of your library always, because it uses a static function to initialize some values, right?
I would suggest to use the files autoloading mechanism in this case. It will require certain files explicitly on every request (!). (Its often used, when your package includes PHP functions that cannot be autoloaded by PHP, but should work in this case, too.)
Example:
{
"autoload": {
"files": ["autoload_this_file_on_every_request.php"]
}
}
So, in the end the complete example with "PSR-4 autoloading" for the classes of your library and "files autoloading" for one specific file would look like this:
{
"name": "thinkerou/rpc-project",
"description": "description for my rpc-project",
"license": "MIT",
"autoload": {
"psr-4": {
"Rpc\\": "src/lib/Rpc/"
},
"files": ["src/lib/Rpc/autoload_this_file_on_every_request.php"]
}
}
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.
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...