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

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.

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.

Workflow for developing Laravel Packages

I'm developing my first Laravel Package, and I realize that my workflow contains much more steps for publishing each modification.
Here is my workflow:
I create a Fresh Laravel 5.4 install. Inside, I create a package/author/name/src where I code my plugin.
I run my tests
Once I have made my change and want to publish it:
a.I push it to my Git rep
b.I tag it with version number
c.In my main project, I run composer update
I check that nothing broke with the integration
This is quite a lot of changes for each little change I want to see in my main project....
Is there a way not to publish any single change, just doing local?
Yes, you can locally develop a package and use it without publishing it. The workflow is: create a packages directory outside of your Laravel project then create the package directory in it, for example the directory structure should look something like this:
- my-laravel-project
- packages
- - my-package-1
- - - composer.json
- - my-package-2
- - - composer.json
Then, in your Laravel project's composer.json file, add the repositories section like given below and add an entry into the require section for each local package, for example:
{
"repositories": [
{
"type": "path",
"url": "../packages/my-package-1"
},
{
"type": "path",
"url": "../packages/my-package-2"
}
],
"require": {
"my/package-1": "*",
"my/package-2": "*"
}
}
In this example, I've two different packages inside packages directory and I've added both packages in the Laravel project. The repositories section describes the package type and path/url for each package so composer can find them locally and require section is simply requiring the dependencies as usual. You may check more on Composer's website.

Satis Private Repository - satis.json structure

EDIT
I think I misunderstood satis, now this is my new understanding, please correct me if I am wrong:
In satis.json, I must specify the url s of the packages I want to mirror, e.g the doctrine git repo, yaml git repo... Then satis mirrors all these packages on my server. This would mean I need to add once all packages used within my composer.json of my project to the satis json (around 20 packages/requirements ). Now, I can add my private satis repository to the composer.json file of my project and when running "composer update", it will first look within the satis mirrored packages for the required package. Did I understand that correctly?
Situation:
I am trying to use Satis for private composer repository. I have a private project on github for a website of mine. On the project, I am using composer and therefore I have a composer.json on the root of the project. It looks the following:
{
"name": "Peter North",
"license": "proprietary",
"type": "project",
"autoload": {
"psr-4": {
"": "src/"
}
},
"require": {
"php": ">=5.3.9",
"symfony/http-foundation": "dev-master"
}
...
}
Now I wanted to use my private satis repository of url: packages.ait.company, running on an apache and accessible so far. The satis.json looks the following:
{
"name": "AIT Company",
"homepage": "packages.ait.com",
"repositories": [
{
"type": "vcs",
"url": "git#github.com:north/ait.git" // this is the url of my private github project
}
],
"require-all": true,
"require-dependencies": true,
"archive": {
"directory": "dist",
"format": "tar",
"skip-dev": true
}
}
I think that I did not understand well how to structure the satis.json file and what it needs to contain, because the way I am trying, it does not download the "php" and "symfony/http-foundation" packages that I specified in the composer.json file - though it does download the correct composer.json file of the project into /satis/include directory json file.
How does the satis.json need to look like, when I want to read the composer.json of my project from github and build the private satis repository of the "require entries" ?
In my Satis update script, it is a two step process to create local copies from both external packages as well as internal, private repositories.
The first step only downloads the meta data of external dependencies and puts them into an intermediate satis repository. The configuration explicitly does not "require-all", but has explicit statements of all packages that are being used (and some that may be used, or had been used in the past) with an open-ended version wildcard, i.e. "symfony/console":">=2.4" (no tilde or caret - I want to get major version updates here). Adding require-dependencies:true scans all the dependencies of all the required packages and adds them to the meta data collection as well.
The result is stored in a directory "external".
The second step is responsible for scanning all packages from internal "repositories", and creating archives from them. This obviously is a list of all private repos, but the trick is: You can also add to the satis.json a repository of type "composer" - and this is where I add the external repository with the meta data from external packages. Adding it here adds all the external packages' versions to the list of versions that need to have ZIP files created.
Running it for the first time takes a long time because of all the packages that need to be downloaded (and created locally from the private repos). Running it after that is simply an incremental update of only the new versions that haven't been created previously.
The key is to configure collecting external packages without ZIPs and with explicit version ranges ("*" as version would work, but only use it if you really need all versions) in one Satis file, then add the resulting repository to the second Satis configuration. You cannot combine the two operations (only creating ZIPs of selected external dependencies together with ZIPs of ALL internal repositories) into one Satis run.
One more tip: You probably only want to create local copies of your external dependencies to avoid hitting a Github outage when you are deploying to production (or are developing and need that update). In order to ensure every used dependency is in my Satis, I added both the Satis repository to every composer.json, as well as "packagist":false, to turn off any direct contact to Packagist. You cannot just add a random package then, it has to be in your local Satis repository first.

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.

Categories