Search code examples
gitgithubyii2composer-phppackagist

Yii2 & Composer - How do I pull locally for 1 package, ignoring packagist


I am wanting to play around with one of my repos that is currently on Github/Packagist. Actually, I am wanting to turn it into a Yii2 extension. I do not want to follow the git add, git commit, git push, composer update, circle. I want to bypass my Github/Packagist and solely use a local source. I want it to act like my package doesn't exist on Packagist.

I am using Yii2, so it has it's own composer.json file. So they all still need to work, and just use a different source for my 1 package.

Of course, default Yii2 composer require block:

"require": {
    "php": ">=5.4.0",
    "yiisoft/yii2": ">=2.0.6",
    "yiisoft/yii2-bootstrap": "*",
    "yiisoft/yii2-swiftmailer": "*",
    "wadeshuler/my-package" : "*"      // <-- needs to pull from local
},

Then you have the composer of my package:

"autoload": {
    "psr-4": {
        "wadeshuler\\Package\\": "src/"
    }
}

Again, I want to tell the Yii2 composer.json to load my package locally and ignore Packagist.


Solution

  • Apparently, Composer is smarter than I was giving it credit for :)

    I was trying all kinds of extra things, when it was really simple, and right in front of me.

    Composer will automatically prefer your local package, if it exists.

    So in my Yii2 composer.json, add a repositories block:

    "repositories": [
        {
            "type": "path",
            "url": "../My-Package"
        }
    ],
    

    and require it as normal:

    "require": {
        "php": ">=5.4.0",
        "yiisoft/yii2": ">=2.0.6",
        "yiisoft/yii2-bootstrap": "*",
        "yiisoft/yii2-swiftmailer": "*",
        "wadeshuler/my-package": "*"    // <-- My-Package
    },
    

    Even though my package is on GitHub, Composer magically knows I want to use the local source :)