0

Let's say I want to write a new application.

/workspace/exampleapp/
  .git
  src/
  vendor/
    app/
      component1/
        .git
        src/
        composer.json
      component2/
        .git
        src/
        composer.json
  composer.json

I have three git repositories. They have no remotes. I also have three composer.json files.

/workspace/app/composer.json

{
  "name": "app/app",
  "type": "project",
  "license": "MIT",
  "require": {
    "app/component1": "dev-master",
    "app/component2": "dev-master"
  },
  "autoload": {
    "psr-4": {"app\\": "src/"}
  }
}

/workspace/app/vendor/app/component1/composer.json

{
  "name": "app/component1",
  "type": "library",
  "license": "MIT",
  "autoload": {
    "psr-4": {"app\\": "src/"}
  }
}

/workspace/app/vendor/app/component2/composer.json

{
  "name": "app/component2",
  "type": "library",
  "license": "MIT",
  "autoload": {
    "psr-4": {"app\\": "src/"}
  }
}

If I run composer update in the project root I receive the error The requested package app/component1 could not be found in any version, there may be a typo in the package name.

What do I have to change to make it work? Is there a way without moving the components directories out of the vendor directory or creating remote repositories?

Thank you!

Philippe Gerber
  • 17,457
  • 6
  • 45
  • 40

1 Answers1

1

I think you are suffering from the XY problem - I'm going to pretend that you actually asked "How can I setup a project with Composer with the following requirements?"

  • Be able to work without a remote Git server i.e. be able to commit and push locally.
  • Be able to commit code for the components separately to the commits to the main project.
  • Be able to edit the code in the vendor directories for your repositories, and commit from those directories.

In which case the answer is, setup your directories like this:

/workspace/app/
  composer.json
    src/

/workspace/components/
  component1/
    composer.json
    src/
  component2/
    src/
    composer.json

Setup each of your component's composer file like this:

/workspace/components/component1/composer.json

{
  "name": "app/component1",
  "type": "library",
  "license": "MIT",
  "autoload": {
    "psr-4": {"app\\": "src/"}
  }
}

Tell your the composer.json file for your app to use the components directories as local git repositories.

/workspace/app/composer.json

{
  "name": "app/app",
  "type": "project",
  "license": "MIT",
  "require": {
    "app/component1": "dev-master",
    "app/component2": "dev-master"
  },
  "autoload": {
    "psr-4": {"app\\": "src/"}
  },
  "repositories": [
    {
        "type": "vcs",
        "url": "/workspace/components/component1"
    },
    {
        "type": "vcs",
        "url": "/workspace/components/component2"
    }
  ],
}

When you do the first "composer update" you should run it with the option composer update --prefer-source. That will force composer to do a git clone for the code, rather than downloading just a zip-ball and extracting it.

(Adding --prefer-source may not be required, it seems composer may do clone by default when fetching from a local directory repository).

You will now be able to edit the code for the components in the vendor directory, be able to commit it separately to the app code, as well as be able to push it to 'remote' git repository at /workspace/components/component1/.

Community
  • 1
  • 1
Danack
  • 24,939
  • 16
  • 90
  • 122
  • Thanks for your answer! I don't really like this option because I have to hard-code paths into my `composer.json`. Do you know if there is a way to make it work with relative paths? I haven't been successful yet (`file://` doesn't really seem to work either). – Philippe Gerber Jan 17 '14 at 21:47
  • 1
    It appears not - https://github.com/composer/composer/issues/2171. To be honest, putting the repositories stuff inside the composer.json is not really a good idea to begin with. If you want to work like that, it's better to use a hack like in my other answer: http://stackoverflow.com/questions/18100420/how-do-i-use-a-git-submodule-with-a-composer-loaded-library/18130386#18130386 – Danack Jan 17 '14 at 22:03