klaaz
klaaz

Reputation: 551

Laravel 5.3 Composer is not updating package dependencies

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": "[email protected]"
    }
  ],
  "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?

Upvotes: 0

Views: 506

Answers (1)

Christos Lytras
Christos Lytras

Reputation: 37318

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 "[email protected]"
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:

Composer update

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:

Composer update

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.

Upvotes: 1

Related Questions