PHP deploy on heroku - push failed - php

I have been trying to deploy a php app on heroku but I keep getting this error:
-----> PHP app detected
-----> Bootstrapping...
-----> Installing platform packages...
ERROR: neither your composer.json 'require' section nor any
dependency therein requires a runtime version, but 'require-dev'
or a dependency therein does. Heroku cannot automatically select
a default runtime version in this case.
Please add a version requirement for 'php' to section 'require'
in composer.json, 'composer update', commit, and deploy again.
! ERROR: Couldn't load 'composer.lock'; it must be a valid lock
file generated by Composer and be in a consistent state.
Check above for any parse errors and address them if necessary.
Run 'composer update', add/commit the change, then push again.
! Push rejected, failed to compile PHP app.
! Push failed
anyone know how I can solve it?

So this is what I would look at, and sorry if any of these assumptions are wrong, or I am going through stuff you have already done.
Your composer.json should be divided up into require and require-dev. Require would be installed in production with require-dev added locally.
{
"name": "something/something",
"description": "A description of my project",
"type": "project",
"require": {
"php": ">=5.5.9",
"doctrine/cache": "1.4.*",
"elasticsearch/elasticsearch": "~2.0",
"monolog/monolog": "~1.0",
"knplabs/github-api": "~1.2",
"ezyang/htmlpurifier": "~4.6",
"easyrdf/easyrdf": "0.9.*",
"hoa/compiler": "~2.15",
"hoa/visitor": "~1.15",
"collectiveaccess/service-wrapper": "v1.1",
"phpoffice/phppresentation": "dev-master",
"phpoffice/phpword": "v0.13.*"
},
"require-dev": {
"phpunit/phpunit": "4.3.*",
"maximebf/debugbar": ">=1.0.0"
}
}
If you have not got composer installed you need to do so. https://www.hostinger.com/tutorials/how-to-install-composer
Run php composer install in the first instance of php composer update later on to install your dependencies.
Please commit everything except, the vendor files and any cache or logging. This would include composer.lock which is a list of installed dependencies and the versions you are using.
Heroku will install these dependencies in production making sure it uses the same version as specified in the lock file.

Related

How to load composer package from remote git when running in production, and load from local package when development environment? [duplicate]

This question already has answers here:
Using PHP Composer to develop a local package and then copy it on deploy
(2 answers)
Closed 1 year ago.
I have two composer files to run composer in each environment
"repositories": [
{
"type": "vcs",
"url": "https://github.com/igorhaf/laravel-admin"
}
],
"require": {
"php": "^7.2.5|^8.0",
"fideloper/proxy": "^4.4",
"guzzlehttp/guzzle": "^7.4",
"laravel/framework": "^7.0",
"laravel/telescope": "^3.0",
"laravel/tinker": "^2.5",
"encore/laravel-admin": "dev-master"
},
I want running laravel-admin from a local folder in my project (packages) when I am using local environment, but with the same composer.json file
If I understand your use case clearly, you develop custom Laravel package and you don't want to always have to push it before you test it in local.
There's absolutely a use case for it. I personally use it. With this approach, your composer.json can stay the same both on local and server. No need to change anything.
You would need the package https://github.com/franzliedke/studio . The documentation is quite explanatory. You install it globally via composer. composer global require franzl/studio though can be installed per project but that's quite tedious because you may only need it locally.
At the root of your project run studio load path/to/your/local/vendor/directory/* (if you want all packages in directory) or studio load path/to/your/local/package (for a single package. You may add multiple packages). This will generate a studio.json file at the root.
Eg.
{
"version": 2,
"paths": [
"path/to/local/vendor/directory/*",
"path/to/local/package"
]
}
Anytime you run composer update, composer will first look in your studio.json file and symlink to local packages if available, else get it from the remote repository.
Note:
i. Make sure to add studio.json to .gitignore.
ii. Also remove the paths from studio.json as seen below and run composer update to generate new composer.lock file before you push your code to production/server. Else your code on server will be looking for packages from the symlink in local setup when you run composer install and give you errors.
{
"version": 2,
"paths": [
]
}
iii. After pushing to server, return the studio.json to initial state and continue to develop. No need to push to github/remote vcs before you see your changes. Changes are symlinked from local if available or gotten from remote.
iv. Run composer install on server.
v. Don't forget to push your package to remote else, you will not see the changes on server, though you see on local.

PHP composer generate composer.lock file from packages already installed in vender

I inherited a legacy code base where a previous developer did not commit the composer.lock file to version control, but instead committed all the packages in vendor. I know.
I have the composer.json, but it appears incomplete as it only has require-dev. Here's the complete composer.json:
{
"require-dev": {
"phpunit/phpunit": "4.3.*",
"squizlabs/php_codesniffer": "1.*",
"phpdocumentor/phpdocumentor": "2.*"
}
}
Can composer generate a composer.lock file from the packages already installed in vendor without updating those packages? When I run composer install, a lock file is created, but the packages are updated to their most recent versions instead of maintaining the version already installed.
run composer update --lock will only updates the lock file hash to suppress warning about the lock file being out of date without upgrade your packages.
reference: https://getcomposer.org/doc/03-cli.md#update-u

Composer update error on friendsofsymfony/http-cache-bundle

i wanted to install the following packages for the Symfony Framework 3.4 with help of the composer package manager:
friendsofsymfony/http-cache-bundle
guzzlehttp/psr8
php-http/guzzle6-adapter
I dont have much experience with composer. I saw that those packages are installable under Symfony 3.4 in a tutorial. I even tried installing every component of the bundle one by one, quitting with the bellow error message for example (guzzlehttp/psr7). When installations failed why are the packages then in my composer.json file under require ?
Here is my composer.json:
"require": {
"php": ">=7.1.3",
"friendsofsymfony/http-cache": "^2.5.2",
"friendsofsymfony/http-cache-bundle": "^2.6",
"guzzlehttp/psr7": "^1.5",
"php-http/guzzle6-adapter": "^2.0",
I am getting this error message:
Errror message
Error message installing (guzzlehttp/psr7):
Errror message
Thanks in advance.
You executed composer update, so probably you have added packages manually in the composer.json
composer update won't change what is inside composer.json.
upgrade: Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
See https://getcomposer.org/doc/01-basic-usage.md for mroe details.
Shortcut for "add if possible" is the command composer require
Exmaple:
composer require friendsofsymfony/http-cache
This will try to install the package. composer.* files will be updated only on successful installation.

How to cache phpunit.phar on Travis CI?

At the moment I use Travis CI in order to test a Symfony Bundle. PHPUnit is installed by Composer since PHPUnit is declared as a dev dependency:
{
…
"require" : {
"php": ">=5.5",
"symfony/symfony": "~2.7 || ~3.0"
},
"require-dev": {
"doctrine/orm": "^2.4.8",
"symfony/assetic-bundle": "~2.3",
"symfony/monolog-bundle": "~2.4",
"doctrine/doctrine-fixtures-bundle": "~2.3",
"liip/functional-test-bundle": "~1.4",
"phpunit/phpunit": "4.8.* || ~5.1"
},
…
}
Even if I use Travis CI's cache to cache the ~/.composer folder after each build, installing PHPUnit with Composer takes some time because PHPUnit has to check all the dependencies before installing them.
So I was thinking about using the PHAR version of PHPUnit that can be installed in one command:
wget https://phar.phpunit.de/phpunit.phar
But if I use this command on Travis CI, PHPUnit will be downloaded once in each build. Is there a way to cache this phpunit.phar file? An easy way may be to put the file in the cache, check the date of this file and download a new version if the file is older than 1 day, but there may be a simpler solution.
There is --self-update option for PHPUnit but it will be removed in the next major release.
You can configure Travis to keep certain directories between builds (called "caching" in their documentation).
Simply add the following configuration to your Travis config file and the /vendor directory should persist across builds:
cache:
directories:
- vendor
You should still include a composer install call to your build to update dependencies when the composer.json file changed. To further speed up the composer install process, you can also add you composer.lock file to version control.
Alternatively, you can of course use the same feature to simple keep a downloaded phpunit.phar file between builds.

Composer Only Installs Packages w/ Manual Update to JSON File

When I use composer to install packages in my project I am only able to do so if I update my json file manually.
For example, if I run the following command in Git-Bash in my project directory (which contains my composer.phar and composer.json file):
php composer.phar require php-di/slim-bridge
It returns the following error:
[Invalid Argument Exception]
Could not find package
php-di\slim-bridge at any version for your minimum-stability (stable).
Check the package spelling or your minimum stability.
However, if i were to just update my json file to the following (example I've provided contains multiple packages I am using in my project):
{
"require": {
"slim/slim": "^3.0",
"slim/twig-view": "^2.1",
"illuminate/database": "^5.2",
"respect/validation": "^1.0",
"slim/csrf": "^0.6",
"slim/flash": "^0.1",
"phpmailer/phpmailer": "^5.2",
"php-di/slim-bridge":"^1.0"
},
"autoload":{
"psr-4": {
"App\\": "app"
}
}
}
... And I run the command: $ php.composer.phar update
Everything installs to project correctly.
What is going on that I am not able to install packages using the require method thus making me resort to manually updating my json file each time?
Since I am using windows, I used the windows installer for composer rather than install through command line and I got this working correctly. Much much easier now since I don't have to update my JSON files manually.

Categories