Compser projects self-contained? - php

I'm new to the whole Composer/dependencies game.
I want to know whether if I create a project using, for example composer require intervention/image from a cmd window opened in the project root folder, the project is then completely portable. Meaning if I copy the project folder on to another machine with no internet, no composer installed and none of the other dependencies installed, will it still work?
Put another way, when I run composer require intervention/image are all of the necessary files cloned locally?

You get a local copy of the files so if you manually copy the project, the vendor directory will go with it. If using Git, typically a .gitignore is set to ignore this dir and you won't get it on clone.

Related

how to install dependencies of a Laravel project?

I am very new to Laravel, I have got to work on an existing project that is located on a server. I am able to access the source code through the FileZilla. The size of the whole project on the server is more than 6 GB. I know that I shouldn't download the whole project but the main folder and run composer install command to install all the dependencies specified in the vendor/composer folder. but I am not able to figure out the structure of the project and which part I need to download in order to run the composer install command and get the project running locally on my machine. Below is the project structure on the server.
I can right-click on a node and select download. which folder is the main project? that I assume it should only has the main files and the composer.json file where all deps are specified in it?
you may need to download the whole project, since there are Controllers, Migrations, resources, etc.

Using Composer locally then uploading files through FTP

Bit of a strange set up but have come across a project where Composer has been used in a local environment to get a project started. The original developer did not have ssh access to the production server therefore he used Composer locally and uploaded the 'vendor' directory from his desktop to the server using FTP.
I now need to add the PHPMailer package so have done the following locally on my Mac:
cd Desktop/
composer require phpmailer/phpmailer
This has created the following structure on my desktop:
Desktop/composer.json
Desktop/composer.lock
Desktop/vendor/autoload.php
Desktop/vendor/composer/*
Desktop/vendor/phpmailer/*
Which of these do I need to upload through FTP? I realise vendor/phpmailer/* is the package I want, so will need uploading.
What about the others? I already have an autoloader configured so guessing vendor/autoload.php is not required here?
composer.json I could add the package to what's already there, e.g.
"require": {
*other packages here*
"phpmailer/phpmailer": "^5.2"
}
But I wasn't sure if that's necessary because I'm not going to be using ssh/Composer on the server to run any updates?
The usual workflow would be:
Checkout the current version from the version control.
Add dependencies via command line composer require new/package.
This will download the new package and update the autoloading.
Test the result locally or on a test website environment.
If happy with the result, upload the whole folder to the production server.
There may be several exceptions from this general workflow:
ad 1: If there is no version control, you'd probably better of starting a local git repo right now, and download the current production state into it as the first commit. Not having version control will make things harder, especially going back to known working versions. And because the files on the production server are probably unmanaged, you'd also check in the vendor folder into your newly created version control just to avoid canceling any changes that had been made to these files.
ad 2: Manually editing the composer.json file sometimes is a faster way to get what you want if you know what you are doing, but you'd have to correctly edit the JSON. For me it usually is too much hassle if I already have a command line ready. The command will also select a matching version that fits into the already installed dependencies. Manual editing may lead to version conflicts that you'd have to untangle. Remember to only install dependencies that work with the PHP version in production. You probably should run composer config platform.php X.Y.Z in order to add the production version of PHP into the composer.json file, which prevents Composer from installing dependency versions based on your development PHP. Adding the -g switch will add this setting to your global (user) setting instead, which will affect all composer operations you start, also for other projects.
ad 3: Manual editing will require you to run composer update on the command line, so there's probably no reason to not do composer require instead.
ad 4: How this could be done is entirely dependent on what environment you have to work with.
ad 5: At this stage you have assembled all files necessary to create a working website. Uploading them to production will always result in a working website unless the upload fails somehow. You could also use some "upload first to temporary folder, then move on the server" approach if you fear FTP would be unreliable. Some people take a different approach: They have a git repository on the production server and they simply push the version that should go live onto that remote repo. Some post-push scripts will run composer install then. This automated approach will also work (but not using FTP), but has the higher risk of something failing during deployment, and probably has no easy way back to the previous situation.
So in the end I'd say that uploading the whole folder structure via FTP (well, that protocol is insecure itself, better replace it with FTPS (FTP with SSL), SFTP or SCP) is better compared to running Composer on the production server.
Your specific question regarding which folders to upload: All of them. Especially upload the whole vendor folder. It contains the current autoloader and all dependency packages the software needs. If you worked correctly, you downloaded the existing composer.json and composer.lock file together with everything else and added the new dependency to it. This has changed both these files, added the new package to the vendor folder and the classes to the autoloader.
Don't fiddle with uploading only parts of the vendor folder, or manually editing a component of the autoloading. You will only create surprises for the developer coming after you if you do some aspect incorrectly, and it also takes more time. Composer is a very good tool to manage dependencies - use it!
You could copy the composer.json from the server to your local server, add the requirements and run composer update.
After that you can copy all files (composer.json, composer.lock and vendor folder to your server)...
Or you can copy local vendor/phpmailer into servers vendor folder, search for the entry of phpmailer in vendor/composer/autoload_psr4.php and add it to your servers vendor/composer/autoload_psr4.php.
Using this method also add phpmailers dependencies the same way.
composer depends phpmailer/phpmailer

How to push laravel packages and helper files through git

If I don't have ssh access and can't run composer command over server.
How can I upload laravel packages properly through GIT repository, so that my project will run without any problem .
I know If I remove vendor folder from root .gitignore then vendor file will up to the server.
But still I have doubt, I have to do other thinks also.
I am not good in git thats why before messing laravel I am asking this.
The general recommendation is not to commit the vendor directory, that's why Laravel ships with a .gitignore file that contains the vendor directory amongst other things.
However, there are situations such as yours where this is not possible, so in that case there is a nice section of the Composer Documentation that offers some advice on how you should handle committing the dependencies to version control:
Should I commit the dependencies in my vendor directory?

I can´t get all folders from my branch on GIT

I have a git project with one branch, I´m trying to clone all files in my local folder, but I can´t get the vendor folder. I would like to know if I can do it in other way to get also vendor class.
I´m running this:
git clone -b branch_name repo_URL
As I say, I don´t get one folder. What I need to do it to get all files?
PD: I can´t download a ZIP of the project.
You are not supposed to find that vendor folder in your repository. It would be useless redundancy.
In modern PHP projects, Composer is being used to manage external dependencies. When running composer update, it reads a file composer.json (which should be present in your repository) to find the most recent versions of the external packages to be used.
This creates a file composer.lock (which should also be in your repository) with the exact versions and commits that got downloaded. If at any later time someone (like you now) wants to recreate the contents of the vendor folder, they run composer install, which will try to get everything that was once downloaded.
So the first step for you is to download Composer, then run composer install (depending on your way of downloading, it might also be php composer.phar install or something close to that).
Reasons for composer install to fail is that dependencies can no longer be downloaded because they were removed from the internet, or that the project is too old and has too old versions of said files so that it does not run with current versions of Composer (although this should be very rare). In any case: If you encounter errors, ask a new question here with all the details, including the full output of the Composer command.
The reason why you don't get the vendor folder is because the vendor folder never went up to the git repository. If you see the .gitignore file, you will notice a :
/vendor
This tells git to ignore that folder and not to send it up to the git repository when you are pushing. There is no way for you to get that folder unless you find the original project before you pushed to the git.

Using Composer packages in a FuelPHP app managed with GitHub

I have a web application that I built using FuelPHP which is hosted on a private GitHub repo.
I recently added JAXL and APNS-PHP to the project using Composer. Specifically, I created a composer.json inside of the fuel/app directory with these contents:
{
"require": {
"varavan/apns-php": "dev-master",
"abhinavsingh/jaxl": "3.*#dev"
}
}
I ran composer update and everything works fine on my local development environment. I can push to GitHub just fine from the command line also.
However, when I pull on the public server, the newly installed composer packages are not included in the pull, although their directories are created. Specifically, these directories exist, but are empty:
fuel/app/vendor/abhinavsingh/jaxl/
fuel/app/vendor/varavan/apns-php/
If I look at those directories on GitHub, they don't look like directories. They've got an icon that I don't recognize:
Also, if I click on "Sync Branch" from the GitHub GUI app, it gives me this message This has been resolved, see update below.
The submodule at 'fuel/app/vendor/abhinavsingh/jaxl' was removed from
.gitmodules, but the folder still exists in the repositroy. Delete the
folder, commit the change, then try again.
WHAT!?
I used composer to install JAXL, not Git.
If I run composer install on the server, it says:
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
But the JAXL and APNS-PHP directories are still empty.
How do I get the composer packages onto my production server?
Update
I added this to my .gitmodules file:
[submodule "fuel/app/vendor/abhinavsingh/jaxl"]
path = fuel/app/vendor/abhinavsingh/jaxl
url = git://github.com/abhinavsingh/JAXL.git
[submodule "fuel/app/vendor/varavan/apns-php"]
path = fuel/app/vendor/varavan/apns-php
url = git://github.com/varavan/ApnsPHP.git
That fixed the error that the GitHub GUI app was giving me, but I still can't figure out how to get the composer packages installed on the production server.
I continued to try everything I could think of to get the composer packages loaded, but I couldn't figure it out.
After adding the submodule definitions to my .gitmodules file, I deleted the entire project from the production server and re-cloned the whole thing from GitHub. The submodules came along for the ride.
It's not the composer way of doing things...
Whatever. What a waste of time.
Composer uses git, especially if you specify you want a development version. It will get it directly from the source.
And if you create a second vendor folder inside app, and Composer has added a git repository there (because of what you specified), git will detect a repo-in-repo, and since submodules are active for Fuel, it will assume you're adding a new submodule.
Like Martin Bean said, make sure you exclude the vendor folder, to prevent this from happening.

Categories