Override composer package type upon installation - composer-php

Is there a way to override a composer package type upon installation? I want to install a composer package which uses a custom type. This type leads into a custom installation path.
What I try to achieve is to ignore the custom installation path and put all the sources under vendor. Is this possible?
The custom type is handled by a composer plugin: https://packagist.org/packages/getkirby/composer-installer

You cannot "override" a package type without actually forking the package.
And most of the time, wouldn't actually do anything, since the type is only used by custom installer logic. So unless one is actually using a composer plugin with installer logic... the type field does not accomplish anything.
Since you now mention that you are attempting to do this for a Kirby Plugin...
Kirby uses its own custom installer: composer-installer.
In the docs is mentioned how to choose a different installation directory for plugins. Following these instructions, to actually install your plugins in vendor it would in theory as simple as doing:
{
"require": {
"getkirby/cms": "^3.0",
"superwoman/superplugin": "^1.0"
},
"extra": {
"kirby-plugin-path": "vendor" // change this to your custom path
}
}
Sadly, it does not seem you can do this just for one specific plugin, as you would be able to do if Kirby just used composer/installers.

Related

Composer - Using a custom fork of a package dependency

I am using the package teamtnt/laravel-scout-tntsearch-driver and I wish to make a very small change to one of the files within teamtnt/tntsearch which is one of the packages dependencies.
Usually I would:
Create a fork of the package.
Add the repository into my composer.json as follows:
"repositories": [
{"type": "vcs", "url": "https://github.com/user/packagefork"}
],
Require/Upgrade the package to the correct version (usually dev-master) keeping the original name spacing and it all works fine.
However, with a dependency which is not directly included in my composer.json file, this does not seem to work. Do I need to fork both the base package, and the dependency package even though I do not need to change anything within the base?
I am hoping there is a simple way to do this, without having to fork each level.
This was actually quite simple. Not too sure why it did not work originally! Instructions below for anyone wondering:
Fork the package (i.e. GitHub)
Add the repo from your username, to your projects main composer.json as follows:
"repositories": [
{"type": "vcs", "url": "https://github.com/youruser/tntsearch"}
],
Edit the composer.json file within the new fork you created in step 1 (youruser/tntsearch) and create/add to the extras key:
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
}
This effectively allows you to install your dev-master version, but allow other packages where this is a dependency to request the 2.0 version (in this case). You do need to be careful in this case that you have forked the correct version and any upgrades are correctly managed later down the line, or things may break!
More info on composer alias here
Require/Upgrade the package using original package namespace, at the dev-master version.
composer require teamtnt/tntsearch:dev-master
The name spacing and package versions will remain the same as before your fork, but the edits from your fork will be pulled into your project instead.

Composer private package issue

I have created a private composer package in packagist.com but when I use
composer require command to fetch it. My package coming under vendor folder which is on root.
But I want it to be in app/code folder. Is there any parameter for composer.json where I can set app/code, so it will come under app/code/.
Yes there is. According to the composer documentation, if your project uses the PSR-4 specification for loading classes, then adding the snippet below to the composer.json file would work.
NB: You will need to change ProjectNameSpace to the base namespace for your project.
...
"autoload": {
"psr-4": {
"ProjectNameSpace\\": "app/code"
}
},
...
Theoretically. You can't composer will always place the code in vendor directory. The code is external and therefore can be updated only by composer. It must not be in app/code as only code you modify for the project should be in app/code.
If you wish to make a Magento project, you should have the fallowing files in the versioning tool.
app/*
composer.json
composer.lock
.htaccess
index.php
The other files will be handled by composer.
But if you really need to do it, but I don't see any reason to do so, then you could use a post-update & post-install script to move the code. But it's a very bad idea.

Composer autoloads classes of TYPO3 extension without any autoload configuration

Why does the following root composer.json result in an vendor/composer/autoload_classmap.php containing all the class mappings for Smarty's classes – although the composer.json of typo3-ter/smarty does not come with any autoload configuration?
{
"repositories": [
{
"type": "composer",
"url": "https://composer.typo3.org"
}
],
"require": {
"typo3/cms": "^6.2",
"typo3-ter/smarty": "2.1.2"
}
}
Some background info:
https://composer.typo3.org is a composer repository, which enables legacy TYPO3 Extension Repository (TER) extensions to be installable via composer. Because TER extensions do not have a vendor name, all of them share the same vendor, which is "typo3-ter".
This (legacy) composer repository is built by using meta information (dependencies to other TER extensions and TYPO3 versions, author, description …) from TER.
To make this repository more useful for end users and especially because TYPO3 >7.6 completely relies on the composer autoloader when installed via composer, the complete extension directory is added to the composer classmap. Without that, extension classes would not be loadable at all, without any additional configuration.
Because this can cause trouble, I taught TER to partially capture information from composer.json in case this file is present. This means if a composer.json is present and it contains an autoload section, this section is used to generate the autoload information for this extension on composer.typo3.org
Regarding the smarty extension:
Surprisingly this extension has a composer.json file already. But it is broken. First and foremost: It misses autoload information, although it clearly has classes available. Because of the lack of autoload information, the composer.typo3.org package generator adds the complete directory as classmap.
If this causes trouble (you never mentioned that, but I assume so), you should add the repo directly as type "vcs" to your composer.json and require "rtp/smarty" instead of "typo3-ter/smarty".
Or you ask the author to fix the composer.json and upload a new version to TER, or even better register that package directly on packagist.org

Is there any easy way to exclude a dependency in dev environment using composer?

I'm using composer to manage a WordPress project and don't need some plugins in development. So basically the opposite of require-dev.
If there was a name for it I would guess it to be require-production or exclude-dev. So requirements in this object would only get installed if composer --production was ran. That's not a real flag, just trying to make the path to the solution clear.
"require-production": {
"example/caching-plugin": "1.0"
}
Is there a way to do this?
I can do this with a php function, but would really like to keep my dependency management in one location
As for workaround, you can define replace property to specify list of packages to ignore, e.g.
{
"require": {
"example/caching-plugin": "*"
},
"replace": {
"example/ignore-this": "*"
}
}
This tells Composer that you're replacing these dependencies, so they won't be downloaded.
Another way is to perform code patching by adding this section:
"extra": {
"patches": {
"vendor/package": {
"Bug #1234: Something is wrong": "patches/1234.diff"
}
}
}
For above feature to work, you need to install cweagans/composer-patches package first, e.g.
composer require "cweagans/composer-patches:~1.0"
Then you can provide different patch files on different branches for certain environments.
There is also scripts property where you can invoke some scripts or commands which can check your environment and depending on the hostname, can invoke certain commands to do some post-processing such as removing some files or folders.

How to use composer with a non-packagist github project containing a packages.json

I want to put https://github.com/timrwood/moment into my composer.json for easy maintenance.
It's not an official packagist project (of course, as it's not PHP), but it contains a packages.json for nodejs. Can I use this in my composer.json?
I tried this, but it didn't work:
{
"repositories": {
"timrwood/moment": {
"type": "git",
"url": "git://github.com/timrwood/moment.git"
}
}
}
It throws an error message saying "No valid composer.json was found in any branch or tag of git://github.com/timrwood/moment.git, could not load a package from it."
And it is lacking the version string to define the version I want to use...
Can anyone help here?
Or shouldn't I use composer here at all cause I'm mixing JS and PHP?
Composer only manages composer packages. It does not know how to parse a package.json file. There are different approaches to this problem. Composer may be able to deal with frontend dependencies in the future.
For the time being I'd recommend using a separate dependency manager for your JavaScript dependencies. Either NPM or something like jam or ender.
Check out composer plugin to handle components via bower, nodejs and git repositories: fxpio/composer-asset-plugin.

Resources