The problem:
I have Entities that are and will be the same for 2 projects written in Symfony. We got an idea to share them between projects. The first idea was to use git sub-module but everyone knows that it's not the most comfortable solution. So we put them in Satis as separate git repository.
In one project I would like to edit them in application directory src/AppBundle/Entity on the other they can be downloaded into vendor directory.
The question is how to setup composer so I can work with them not in vendor directory. How the commits will look like? Is git sub-module required for this?
I've already read about "type" : "path" for repository, I've checked composer installers. Is there any other solution than symlink which comes to my mind right now?
So to sum up.
How to work with shared library in one project from app directory and on the other on vendor directory?
This is the solution that worked for me.
I've cloned internal library into project.
In main project I've added this directory to .gitignore and in composer.json I've added following lines
"repositories": [
{
"type": "path",
"url": "internal-library",
"options": {
"symlink": true
}
},
{
"type": "composer",
"url": "http://our-satis"
}
],
In the other project I've added only satis repository(the entities will be changed only in one project and imported to the other).
"repositories": [
{
"type": "composer",
"url": "http://our-satis"
}
],
So now in development when I do composer update the library is symlinked to vendor directory. If I don't have this directory it will be get from satis. In production repository will be get from satis because my direc tory does not exist. I've had some issues with PSR loading but everything works as expected.
I hope it will help someone having same issues as I had.
Related
How to use git, when I have large tree of folders in file structure of web-project. Module, which I developing is separated by different folders in this file structure. As it is customary by MVC pattern concern.
In the overall file structure of web-project I have, roughly speaking:
model folder
controller folder
views folder
languages folder
and so on
I making changes in files at this folders and need track changes. These folders are not combined in one folder, that associated with module, that I developing. These folders are scattered in different parts of the file structure.
I could create git repository at the root of file structure and in .gitignore specify, which folders track. But I develop many modules. And I need separate git repositories for them.
Where and how create git repositories to developing many modules in large file structure?
If I init git repository at the root of web-project is it possible to create many repositories at the root of file structure for each module and for each repository specify which folders git should track?
I think simplest solution now days it's to keep your independent modules in separated repositories and then requesting them using composer.
I will show you some theoretical example of it.
I have project, which should use Payment Module. Payment Module is a separated repository with composer.json file provided in root directory.
For example:
{
"name": "company/payment-module",
"description": "Module handling payments from our customers.",
"autoload": {
"psr-4": {
"Company\\PaymentModule\\": "src/"
}
}
}
That gives you possibility of using any of modules created in any application/project you'll build.
So, for example in your project you can require Payment Module as dependency.
{
"name": "company/shop",
"type": "project",
"description": "The main repository of our shop.",
"autoload": {
"psr-4": {
"": "src/"
}
},
"repositories": [
{
"type": "git",
"url": "https://github.com/company/payment-module"
}
],
"require": {
"company/payment-module": "dev-master"
}
}
When you define modules you want in the project and install them using composer, they will be under vendor/ directory and will be autoloaded into the namespaces you define.
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.
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'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.
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...