Keep local and production composer.json files in sync? - php

I am working on a large project with a lot of git repositories. I can easily keep them in sync like this:
https://getcomposer.org/doc/05-repositories.md#path
"repositories": [
{ "type": "path", "url": "../another-component" },
{ "type": "path", "url": "../yet-another-component" }
]
If I run composer this will create a symbolic link inside my vendor folder for each repository provided. This way whenever I update one of the repositories outside my main app repository changes will be reflected immediately.
This is a perfect local development solution but on production I would just like to have a composer file that points to the online Github repositories and pull the repositories from Github.
Of course this composer file would look a bit different since the types would be set to vcs and the url's would point to Github instead of being relative paths.
How do people do this for local/production setups? Do you just maintain 2 separate composer files, one for local development and another for production?
This would mean whenever I add another repository to my codebase I have to remember to update it in 2 places or my entire codebase breaks down.

Since it seems your components are already hosted online, I would recommend to just let composer pull them down from Github and manage them like any other dependency, be it from development or production.

Related

PHP Developing a composer package under vendor of the main project

I have a main php project that needs a php component developed by me, that i intend to reuse in other projects, the component is in the main project vendor directory.
However pushing to Github the component, go to the main project, running composer update is time consuming.
In order to speed up the development of the component, is there a way to include the local component project into the main project?
This is how I do local composer package development.
Create a vendor-repo folder next to the vendor folder.
Create a directory for the vendor and project name for example vendor-repo/vendorname/packagename
Create a git repo inside the packagename folder and build your composer package complete with composer.json etc.
Add your local repository to the main composer.json (the one that requires your package). You can also disable the packagist repo by adding the "packagist": false as per the example below. This will speed things up and is a good idea if you aren't using any packages from packagist.
"repositories": [
{
"type": "path",
"url": "vendor-repo/vendorname/packagename"
},
{
"packagist": false
}
]
Then when you run composer update it will get your package from your local repo rather than needing to get it from GitHub.
You can add the library directly inside your vendor folder, register the sources using the psr-0 or psr-4 autoload, dump the autoload classes and create a git repository inside the folder.
So you can use it as a library, and at the same time, you don't need to pull every update you do to test the library inside your project.
I don't know if it's a good approach, but symlinks didn't work for me and i still need to register the classes in the autoload.

How can I make composer load a branch dynamically?

I'm using composer to organize my vendor libraries. One of these libraries is a private repository of general system classes.
The problem is that I want to have my code run out of the dev branch when it's on the dev environment, and automatically use the dev branch of my composer library. When the code is on staging, I want to use the staging branch and have composer switch to use the staging branch of the composer library. Same for production.
Is it possible for composer to pull the correct branch without having to manually switch to that branch or edit the composer.json file?
Edit: useful answer is the comments of accepted answer.
Composer is not designed to dynamically include code. In fact, it is designed to intentionally NOT change the code on it's own, but only with developer interaction (i.e. explicitly updating).
So if you manage to require one version in your dev branch, and then merge this to staging, if you move the composer.lock unaltered, it will fetch exactly the version of your dev branch situation - now in staging. Which is actually a good thing because you do want to test the real software, and the software used to develop is the software that should go to staging and then to production - nothing else that is dynamically bound.
I am experiencing quite the same problem with an old legacy module. It does contain configuration data, which should get rolled out differently to dev, staging and production, but I cannot use Composer for this.
Maybe if you can describe your use case a bit more, there might be a solution for you. But I hardly believe it will contain Composer.
Require inline alias
Branch aliases are great for aliasing main development lines. But in order to use them you need to have control over the source repository, and you need to commit changes to version control.
This is not really fun when you just want to try a bugfix of some library that is a dependency of your local project.
For this reason, you can alias packages in your require and require-dev fields. Let's say you found a bug in the monolog/monolog package. You cloned Monolog on GitHub and fixed the issue in a branch named bugfix. Now you want to install that version of monolog in your local project.
You are using symfony/monolog-bundle which requires monolog/monolog version 1.*. So you need your dev-bugfix to match that constraint.
Just add this to your project's root composer.json:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/you/monolog"
}
],
"require": {
"symfony/monolog-bundle": "2.0",
"monolog/monolog": "dev-bugfix as 1.0.x-dev"
}
}

Composer check local repository instead online repository like Maven

how to get composer http://getcomposer.org to work like maven in java. Instead of getting repository online, it can get from local repository. So, we don't have to download again.
I don't know Maven, but at least two options in composer's world come to my mind:
Access a local git repository
Let's say you want to use a local git repository of braincrafted's bootstrap bundle.
Add the following entry to your composer.json:
"repositories": [
{
"type": "vcs",
"url": "/home/user/braincrafted/bootstrap-bundle"
}
]
"require":{
"braincrafted/bootstrap-bundle":"dev-master"
}
Satis
Another alternative is Satis - makes you have your private packagist.com
It's explained in the documentation
This talk is worth watching to learn more about Composer, SAtis etc: https://www.youtube.com/watch?v=P3NwF8RV1lY

How to develop and include a Composer package?

I'm looking to develop a package in PHP, but I don't want it immediately available on GitHub or somewhere. It's easy enough to include a Packagist file in my composer.json, but how do I add a local package into my composer.json? Also, should I be building the package in /vendor/foo/bar (relative to the root composer.json), or should I put it somewhere else?
Edit: I guess my question is about how everyone else writes their packages. Does every new package get added to Packagist, and then when you want to test your changes, you commit to GitHub (or wherever), and then pull that back down via Composer? That seems really inefficient.
Instead of creating a new repository, you can tell composer to use any local path:
https://getcomposer.org/doc/05-repositories.md#path
For instance, lets say that you have your PHP projects under ~/devel/projects
You may have your main project in ~/devel/projects/main_project, and your "local package" in ~/devel/projects/local_package
Define your composer configuration for the local package. In ~/devel/projects/local_package/composer.json.
{
"name": "your_vendor_id/your_local_package",
...
}
Then, you can edit ~/devel/projects/main_project/composer.json and link to your local package via path repo:
"repositories": [
{
"type": "path",
"url": "../local_package",
"options": {
"symlink": true
}
}
],
"require": {
"your_vendor_id/your_local_package": "dev-master",
...
}
More info on this link (not written by me but has a good explanation on this subject):
https://carlosbuenosvinos.com/working-at-the-same-time-in-a-project-and-its-dependencies-composer-and-path-type-repository/
As this question has many different components/standards to be explained, I'll try to explain as much as possible here and you can PM me or just Google it for more specific questions as they come up.
To answer your first question, "How do I add a local package into my composer.json?":
If by "add local package" you mean autoload your class/package, you can do that by either using PSR-4 or PSR-0 or Classmap options in composer.
Read more
PSR [petermoulding.com] (via archive.org)
PSR-4 autoloading support in Composer [seld.be]
Battle of the Autoloaders: PSR-0 vs. PSR-4 [Sitepoint]
Why use a PSR-0 or PSR-4 autoload in composer if classmap is actually faster? [SO]
You can Google it if you need more info on PSR-0, PSR-4 and Classmap.
Example
"autoload": {
"psr-4": { "Core\\": "src/Core" } ## "standard": { "namespace" : "path/to/dir"}
}
Or (edit)
If you actually want to add a local package:
Create a composer.json for the local package, like:
{
"name": "localPackage/core",
"version": "dev-master"
}
You can also specify other properties and/or dependencies as required.
Zip the package, with the composer.json file as the root file in the archive.zip, and place it where desired.
In the other project/package where you want to include the local package, add the local package name to the required parameter, like
"localPackage/core": "dev-master"
Add the following under the repositories parameter:
"repositories" : [
{
"type": "artifact",
"url": "path/to/localPackage.zip"
}
]
Now if you have the local package on git, then there would be no need to archive the package (basically omit step 2) and you just need to replace the URL in the above example to the path/to/localPackage/.git.
(End of edit)
Now to answer the larger question: "How do I develop and include a Composer package?":
Decide the directory structure. Commonly it is as follows:
/PackageRoot
/src/PackageCore
composer.json ## this is your library’s composer.json
LICENSE
and set up your composer.json.
An example of one of mine composer.json files can be found at http://pastebin.com/tyHT01Xg.
Upload it to Github and tag the version. Use Semantic versioning (make sure you exclude/ignore the vendor directory when uploading to Github).
Register the package with Packagist (after logging in).
If you have tagged your commit as v1.0.0 (or similar), then this will show up in your Packagist dashboard for that package.
Now, if everything is done right, you should be able to use your library as a dependency in other projects by adding it to the composer.json of that project.
Here's a recap of the solutions plus my own
publish on packagist
Since you don't want to publish yet, you're in development, this is a poor option.
upload github
You may not want to publish your library source on github, not want to pay for a private repo, or be able to use a cloud external service (due to political or network policies).
zip your library
You can use the composer repository path in your example implementation to point to a local zip file as a release. You'll have to re-zip every time you make a change to the lib, even with a batch file to do it, this is icky.
upload to local git/svn repo on your machine
This is getting close, but you'll need to do a composer update each time you change your library and want to test your example implementation. This mimics productions but is cumbersome. Personally I recommend this solution, even though it's not brainless.
Autoload the library directly (sortof does what you want)
This is a hack, but you could just add:
{
"require": {
},
"autoload": {
"psr-4": {
"yourlibnamespace": "D:\\Code\\yourlib\\src\\"
}
}
}
Please note that you will need to copy+paste the 'require' section from your lib to your sample implementation. Change 'yourlibnamespace' to your library namespace and "D:\Code\yourlib\src\" to your local path to your library source.
This way any changes are immediately reflected. However you will not be using or testing your library's composer.json file at all. If you change a requirement in your library .json it will not flow through at all. So it has some big disadvantages, but does do what you want, which is to test your library implementation immediately with the least commands possible.
add your example implementation directly in your library tree (recommended)
Usually you just have src\ and tests\, but many have examples\ where you can find sample implementations. As you develop your application you can contribute to these example implementations. You can do this in a local git/svn repo, and you have the advantage of getting the lib's 'require', plus the namespace automatically. It is the best of all worlds. I recommend this method.
It seems most answers on this thread are not "in the know". I am new to Composer myself, but these answers are misleading. The question could simply be stated as: "How can I develop a composer package".
Yes, you could use a custom repository or upload an unfinished package and update it after every change. That neither is the correct solution or the answer to the question.
It doesn't help that Composer's official documentation doesn't state this upfront, but you can see the heading on the Libraries documentation page:
Every project is a package
This is very important to understand
composer.json:
The previously mentioned page goes on to state:
In order to make that package installable you need to give it a name. You do this by adding the name property in composer.json
{
"name": "acme/hello-world",
"require": {
"monolog/monolog": "1.0.*"
}
}
In this example so far, we have a required package, and now a name. Note the vendor/name format.
So now to autoloading our own files, which is documented on the Basic usage page.
{
"autoload": {
"psr-4": {"Acme\\": "src/"}
}
}
This will autoload the namespaced class files under the src/Acme directory.
On to the fun.
Install/Update
Install or update the package with the command:
composer update
or
php composer.phar update
This will download the required packages and create the autoload.php file
Our project structure should look simular to the following:
src
Acme
Foo.php
vendor
monolog
...
composer.json
Including
Now to test.
Include autoload.php
require_once 'path/to/project/vendor/autoload.php';
Assuming Foo.php looks like the following:
<?php
namespace Acme;
class Foo {
public static function bar(){
return 'baz';
}
}
?>
we can then call this from our script:
echo Acme\Foo::bar(); // baz
Please correct any misleading information I may have stated. This is what seems the be the solution to a popular question.
Maybe adding a custom repository will help you?
https://github.com/composer/composer/blob/master/doc/05-repositories.md
You can set up a local git repository with your library very easily.
Of course if you use composer to manage dependencies you should build your library someplace else and download it to vendor/ via composer coz this is the whole point i guess.
That's my flow of creating and developing a new Composer package locally:
Create a new repository for the package on GitHub (just an example)
Add it to Composer's database (packagist.org)
Add it to your main project via composer require. This is where you start wondering how can you apply hotfixes quickly
Clone it on your local machine somewhere, this is where you develop it
Now to test your local version, add a php require() statement where you load the class files in question. The autoloader won't load the one downloaded via composer but your local one.
When you're done hotfixing, delete/comment out the the require statement to revert back to using the composer version of the package.
Commit all changes, tag the commit and push to GitHub; hook fires to update Composer. Run composer update on your main project, the package gets updated to your local version.
This is still not ideal, but it gets the work done for small to medium packages.
To make developing more efficient I simply symlink the development repository into a directory that has already installed it.
For example, if /Code/project-1 requires a package that's in /Code/package-1, I:
Commit package-1 to GitHub (can even be private).
I then tell project-1 to install it using a custom repository (see other answers for the link to repository configuration).
Once it gets installed, I symlink /Code/project-1/vendor/developer/package-1 to /Code/package-1.
That way, when I make changes in /Code/package-1, it's immediately reflected in /Code/project-1.
My workflow does not completely answer the OP's question but may be helpful for others.
I like to develop a reusable package / lib in a real world project and I do not want to go through git / composer locally.
I also do not like to load the package differently locally compared to production (e.g. through a local require statement or local composer setting).
So what I do is simple: I add the package directly under the vendor directory, either directly or using a symlink.
drawbacks
When using composer update / install, your package may be overwritten. (so I tend to update or add dependencies one by one, during this time).
When not being careful, composer install on the production environment may install a different version. So keep pushing your package and version tags!!
Changing the package's dependencies and loading it in the parent project trough composer update, overwrites the package's folder

Better approach to download php composer dependencies

I have been using maven in JAVA and started using PHP Maven, recently I switched to composer.
My project is with Zend Framework 2 and the team only checks in the application code not anything on the vendor directory. This is done to avoid conflicts and not to have the libraries under SVN.
Now each time a developer sets his or her new environment, we observe that, the composer pulls the dependencies from internet. This takes quite a long time.
Is there any better idea/approach to make this faster or handling the project in different way to avoid this problem?
maven uses maven proxy servers which can cache the download and can be used in the network again, but do we have any solutions to handle problems like this?
Composer is a very young project, so there might be things missing which e.g. Maven can co without hassle.
You could set up your own Packagist server as described in the composer docs. I believe packagist has some caching options which can be used to store packages on the packagist server.
What you also could do is fork your dependencies and push them to a company-owned private repository. In your composer.json you would now only use this dependencies, making it faster to clone. Of course this would require you to maintain all the different dependencies (although this could be done with a script and a cronjob, pulling the data from the github repo and pushing it into your company owned).
I also believe composer has some proxy options, but I don't think these are meant to cache dependencies.
Last option would be to develop something like this, either as part of composer/packagist or as stand-alone.
In PHP there is an existing option for running a composer like repo locally and it's called Satis (it's actually provided by Composer) here: https://github.com/composer/satis
So you can run it locally on your server and point your composer to use that as a default composer repository and Satis makes sure that all installed packages and different versions are cached on disk as ZIP files so could be retrieved quicker compared to always downloading them from Internet.
You can do something like this:
{
"repositories": [
{
"type": "composer",
"url": "http://satis.example.org/"
}
],
"require": {
"company/package": "1.2.0",
"company/package2": "1.5.2",
"company/package3": "dev-master"
}
}
This also allows you to have private packages and libraries without exposing them on GitHub.
Another BIG advantage is when GitHub goes down for whatever reason you can still deploy as all of your dependancies are cached locally. This is assuming you haven't added new, non-existent packages to the release.

Categories