Laravel 5.3 Composer is not updating package dependencies - php

I am in the process in developing my first package. The package is working.
Now I want to use Laravels Collective's HTML in my package (dependencies).
Therefor I added this to the package's composer file:
(this composer.json is in the root of the package: /packages/vendor/package)
{
"name": "vendor/package",
"description": "",
"license": "MIT",
"authors": [
{
"name": "firstname lastname",
"email": "info#domain.nl"
}
],
"minimum-stability": "stable",
"require": {
"twbs/bootstrap": "dev-master",
"laravelcollective/html": "5.3.*"
}
}
I've included the package in Laravel's own composer.json:
...
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Vendor\\Package\\": "packages/vendor/package/src"
}
},
...
When I do composer update the dependencies of the package are not updated. What am I missing here?

You have to define a local composer repository if your package is not uploaded to packagist.org yet. Inside Laravels' application composer.json add a local repository like this:
"repositories": [
{
"type": "path",
"url": "/full/or/relative/path/to/development/my-package"
}
],
"require": {
"my-package": "*"
}
You have to define the package src directory inside the package composer.json and not inside the Laravels' own composer.json. Try and define it like this inside packages' composer.json:
"require" : {
"twbs/bootstrap": "dev-master",
"laravelcollective/html": "5.3.*"
},
"autoload" : {
"psr-4" : {
"PackageNamespace\\PackageClass\\" : "src/"
}
},
UPDATE
In order to do a proper package development, you have to keep your package files at some directory other than /vendor/ composer directory. The main reason is that the developer can delete the entire /vendor directory if there might be conflict problems or for cleaning up and setting up everything under composer /vendor path. I use a very simple method and I keep my own packages at:
<application>/dev/packages/<package-namespace>/<package-name>
You have to initialize a git at your package for composer to recognize the repository. To make a git package, go at your package location and run the following commands:
cd <application>/dev/packages/<package-namespace>/<package-name>
git init
git add *
git commit -m "Initial commit"
You may also have to set the git config user.name and git config user.email before commit, for the git to be able to recognize someone and allow the local commits:
git config user.email "you#example.com"
git config user.name "Your Name"
In my example, the namespace is lytr and the package name is testpkg.
The <application>/dev/packages/<package-namespace>/<package-name>/composer.json (<application>/dev/packages/lytr/testpkg) will look like this:
{
"name" : "lytr/testpkg",
"description" : "Test package of Lytr",
"keywords" : [
"test",
"package"
],
"license" : "MIT",
"require" : {
"twbs/bootstrap" : "dev-master",
"laravelcollective/html" : "5.3.*"
},
"autoload" : {
"psr-0" : {
"Lytr\\TestPkg\\" : "src"
}
},
"extra" : {
"branch-alias" : {
"dev-master" : "1.0-dev"
}
},
"minimum-stability" : "dev"
}
Then at your application <application>/composer.json you'll have a local git repository and your package like this:
"repositories": [
{
"type": "path",
"url": "<full-application-path>/dev/packages/lytr/testpkg"
}
],
"require" : {
"lytr/testpkg": "*"
},
"minimum-stability": "dev",
I include "minimum-stability": "dev", because we are using master-dev versions. Then after running the composer update command only having the "twbs/bootstrap" : "dev-master", at the package requirements, we'll see the following output on the console window:
And after we change the package composer.json and require the "laravelcollective/html" : "5.3.*",, we do a composer update and we see composer installs the laravelcollective/html package properly:
I know this might look confusing and somehow overkill, but this is a proper way for developing packages for composer. You can also have your packages at a git repository and make composer clone that repository instead of your local files. When you're done developing the package and you release it under https://packagist.org/, then you will just have to require your package as any other normal package without the repositories and all the local git thing. Remember, you are at development phase of your package and not at production.

Related

UnexpectedValueException Could not parse version constraint mybranch: Invalid version string "mybranch"

I'm trying to develop a PHP library (called foo/bar) using Composer in dir /work/a with the composer.json contents:
{
"name": "foo/bar",
"require": {
"php": ">=7.2"
}
}
/work/a is a git project and I'm on the branch mybranch
I'm trying to use this lib in another project locally (called testing/foobar) using Composer in dir work/b with the composer.json contents:
{
"name": "testing/foobar",
"type": "project",
"repositories": [
{
"type": "vcs",
"url": "/work/a"
}
],
"require": {
"php": "^7.4",
"foo/bar": "mybranch"
}
}
When running $ composer install in /work/b I get the error:
[UnexpectedValueException]
Could not parse version constraint mybranch: Invalid version string "mybranch"
You have to prefix your branch name with dev-, so your branch name will have to be dev-mybranch.
Loading a package from a VCS repository
...
In composer.json, you should prefix your custom branch name with "dev-".
...
Also check this Q/A "Composer require branch name".
Change branch name to have dev- prefix, add it to /work/b project:
{
"name": "testing/foobar",
"type": "project",
"repositories": [
{
"type": "vcs",
"url": "/work/a"
}
],
"require": {
"php": "^7.4",
"foo/bar": "dev-mybranch"
}
}
Run composer install:
❯ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing foo/bar (dev-mybranch 85c97b7): Cloning 85c97b7b23 from cache
Writing lock file
Generating autoload files

How to force Composer to download a local package?

Let's say we have the following directory structure, we assume my-package also exists on Packagist:
- apps
\_ my-app
\_ composer.json
- packages
\_ my-package
\_ composer.json
To add my/package as a dependency of my-app, the documentation states we can use the following configuration:
{
"repositories": [
{
"type": "path",
"url": "../../packages/my-package"
}
],
"require": {
"my/package": "*"
}
}
However when I composer update, the dependency is still downloaded from Packagist. So, to see, I disabled Packagist.org:
{
"repositories": [
{
"type": "path",
"url": "../../packages/my-package",
"packagist.org": false
}
],
"require": {
"my/package": "*"
}
}
I cleared the cache with composer clearcache, removed my/package with composer remove my/package and installed it again with composer require my/package --prefer-source (I didn't understand if --prefer-source is for vcs only). The downloaded package is still not the local one. How to force composer to use the local one?
"require": {
"my/package": "*"
}
In case of VCS or path repository types, you need to specify version of the package you request. So instead of using *, as you have currently, use #dev:
"require": {
"my/package": "#dev"
}
For
composer --version
Composer version 2.0.14
in composer.json file
"require": {
"my/package": "dev-master"
}

Force Composer to update a package

I've developed a custom laravel package and put it on GitHub. I put it in composer.json (code below) and it installs fine.
I have no version info on it yet, since it is still in development. When I make changes to my package (in a separate directory), I commit and push the changes up to the GitHub repo.
When I run 'composer update', I get "nothing to install or update". If I delete the package from my vendors directory and update, then my package IS installed from the GitHub repo, with the latest changes.
But I would like to be able to pull/force the latest changes from the repo without deleting it first from my vendors directory, since I have other dependencies on that package, and if I delete it, I get errors from artisan clear-compiled that classes are not defined (since they are defined in my deleted vendor package...)
The relevant portion of my top-level composer.json is:
"repositories": [{
"type": "package",
"package": {
"name": "myrepo/MyExtension",
"version": "dev-master",
"source": {
"url": "https://github.com/myrepo/MyExtension.git",
"type": "git",
"reference": "master"
},
"autoload": {
"psr-4": {
"MyExtension\\": "src/Extensions/"
}
}
}
],
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.*",
"myrepo/MyExtension": "dev-master"
},
You have created all meta data about your package yourself, likely making Composer think that the data didn't change.
The easier, and probably working, way would be to simply point to the repository URL and let Composer query the meta data from the composer.json file contained in the repository:
"repositories": [{
"type": "vcs",
"url": "https://github.com/myrepo/MyExtension.git"
}]
For it to update your changes you need to version your package but as you said earlier you are not versioning your packages so for it to update your changes you can go to composer.lock to remove your package entry or to use composer to remove the package and install it again. eg
// composer remove vendor/package && composer require vendor/package
composer remove zizaco/entrust && composer require zizaco/entrust

Unable to create composer package - InvalidArgumentException

I have been trying to create a simple composer package. But I'm stuck with the following issue. Dont know how to resolve this.
[InvalidArgumentException]
Could not find package sarav/sample-package at any version for your minimum-stability (stable). Check the package spelling or your minimum-stability
Package Url : https://packagist.org/packages/sarav/sample-package
I ran the following composer command
composer require sarav/sample-package
My composer contents
{
"name": "sarav/sample-package",
"description": "Sample package",
"type": "Library",
"license": "MIT",
"authors": [
{
"name": "Sarav",
"email": "me#sarav.co"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-0": {
"Sarav": "src/"
}
}
}
Your package config looks good to me, but your Git repo doesn't have any tagged versions.
Use git tag v1.0.0 or whatever version is appropriate, then git push origin --tags to update on GitHub.
Alternatively, you can go without tagged versions by specifying the master branch when you require the package, like so:
composer require sarav/sample-package dev-master
You can require any branch in this manner, but the dev- prefix is mandatory.

How to publish package and installing with composer php

I have so far been able to publish my package on packagist.
From my repository, http://github.com/pbalan/directory-parser
I followed How to create a library to be used by composer autoloading?
When I try to install the same using composer, I am unable to do so.
Composer fails saying:
Loading composer repositories with package information
Reading composer.json of pbalan/directory-parser (0.0.1)
Importing tag 0.0.1 (0.0.1.0)
Reading composer.json of pbalan/directory-parser (master)
Importing branch master (dev-master)
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- The requested package pbalan/directory-parser 1.0.0 could not be found.
Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setting
Here's my composer I have in my github repository:
{
"name" : "pbalan/directory-parser",
"description" : "DirectoryParser",
"license": "MIT",
"keywords" : ["DirectoryParser"],
"homepage" : "https://github.com/pbalan/directory-parser",
"authors" : [
{
"name" : "prashant"
}
],
"autoload" : {
"psr-0" : {"src" : ""}
},
"require" : {
"php": ">=5.3.3"
}
}
And I am trying to install in a fresh directory using this composer.json:
{
"name" : "pbalan/directory-parser",
"description" : "DirectoryParser",
"license": "MIT",
"keywords" : ["DirectoryParser"],
"homepage" : "https://github.com/pbalan/directory-parser",
"authors" : [
{
"name" : "prashant"
}
],
"repositories": [
{
"type": "vcs",
"url": "http://github.com/pbalan/directory-parser.git"
}
],
"require" : {
"php": ">=5.3.3",
"pbalan/directory-parser": "dev-master"
}
}
I changed my composer.json now to a point where I have no errors, however I am not able to install the package and just install autoloader files.
How could I get my package installed?
Please help!
The problem is probably that you are re-using the name of your package in the second composer.json.
Simply use this as the composer.json for your empty directory:
{
"require": {
"php": ">=5.3.3",
"pbalan/directory-parser": "dev-master"
}
}
That's all! You don't need all the extra information such as name, description etc. unless you are creating another library that should be available on Packagist as well.

Categories