Configuring class autoloading in composer - php

there is a project on Github that works stably. Below is a composer file:
{
"name": "wnull/userbars-warface-generator",
"description": "Simple and free library to generate userbars game Warface",
"keywords": [
"wf",
"warface",
"generator",
"userbars"
],
"type": "library",
"license": "MIT",
"require": {
"php": ">=7.1"
},
"autoload": {
"psr-4": {
"WF\\": "src/WF"
}
}
}
The problem is that after each successful installation of the project through the composer, you have to do the following command:
>>> composer dump-autoload -o
After that, all all classes will work correctly. Log from the console:
C:\hangry>composer require wnull/userbars-warface-generator
Using version ^1.0 for wnull/userbars-warface-generator
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing wnull/userbars-warface-generator (v1.0): Loading from cache
Writing lock file
Generating autoload files
C:\hangry>
C:\hangry>composer dump-autoload -o
Generated optimized autoload files containing 6 classes
Question: how do I avoid entering this command every time, and everything worked correctly during normal installation?

The problem was solved by replacing the PSR-4 to classmap. Thanks #MagnusEriksson.
Composer file:
{
"name": "wnull/userbars-warface-generator",
"description": "Simple and free library to generate userbars game Warface",
"keywords": [
"wf",
"warface",
"generator",
"userbars"
],
"type": "library",
"license": "MIT",
"require": {
"php": ">=7.1"
},
"autoload": {
"classmap": ["src/WF"]
}
}

Related

Asana Installation using composer

I am trying to install the asana library through the composer.
Json:
"asana/asana": "^0.10.0" added to composer.json and {
"name": "asana/asana",
"description": "A PHP client for the Asana API",
"type": "library",
"keywords": ["asana", "client"],
"homepage": "https://github.com/Asana/php-asana",
"license": "MIT",
"require": {
"php": ">=5.4.0",
"nategood/httpful": "~0.2",
"adoy/oauth2": "^1.2.0"
},
"require-dev": {
"instaclick/php-code-sniffer": "dev-master",
"phpunit/phpunit": "^9"
},
"autoload": {
"psr-0": {
"Asana\\": "src/"
}
}
}
to composer.lock but getting error 'Package Asana/asana has no version defined.
'
As suggested by #Jeto you should not edit composer.lock manually. To install the library, you can follow the steps mentioned in official docs here.
Assuming that you are doing a fresh install, follow steps below:
Put "asana/asana" package as a dependency in your composer.json file:
{
"require": {
"asana/asana": "^0.10.0"
}
}
Now run the command composer install
composer.lock file will be automatically updated by Composer when installation succeed.
EDIT:
OR
As mentioned by #Jeto in comments, You can simply do this using a single command: composer require asana/asana:^0.10.0

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

Develop Laravel Package locally with path repository is not working

I have a Laravel app, that use a plugin of mine : "xoco70/kendo-tournaments"
So, to develop it, I added it in a packages/ folder in root of app.
Now, in my composer.json, I used path, to point the local rep, and not to have to go through composer ( and have to version it for each comma I change) :
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.7.*",
"xoco70/kendo-tournaments": "dev-master"
},
"repositories": [
{
"type": "path",
"url": "/packages/xoco70/kendo-tournaments"
}
],
}
Thing is it doesn't work, when I try to composer install / update, it doesn't update the vendor folder.
Is there something I'm missing?
I could make it work
first I added the symlink option:
"repositories": [
{
"type": "path",
"url": "package/xoco70/laravel-tournaments",
"options": {
"symlink": true
}
}
],
Then composer update
If it still doesn't work, delete your vendor package and run composer update again
composer should output this:
- Installing xoco70/laravel-tournaments (dev-master): Symlinking from package/xoco70/laravel-tournaments
You need to run composer init from /packages/xoco70/kendo-tournaments in and use xoco70/kendo-tournaments for the name of the package as well as that packages autoload settings for your project composer to pick it up.

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.

My composer package's namespace doesn't show in the autoload_psr4 file

I'm creating my first composer package. I'm testing it by pulling it into a vanilla Laravel project.
The issue I'm having is that when I require my composer package in the main Laravel composer.json file and then try to update the autoload.
My package's composer.json:
{
"name": "cschmitz/l5-simplefm",
"description": "A Laravel 5 wrapper for Soliant Consulting's SimpleFM package.",
"require": {
"soliantconsulting/simplefm": "3.0.*"
},
"license": "MIT",
"authors": [
{
"name": "cschmitz",
"email": "schmitz.chris#gmail.com"
}
],
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"L5SimpleFM\\": "src/"
}
}
}
My package's folder structure in the Laravel project's vendor folder:
My Laravel project's composer.json:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.1.*",
"cschmitz/l5-simplefm": "dev" // Requiring my package here
},
...
After this, I performed a composer dump-autoload. I don't get any errors from composer, but when I check my vendor/composer/autoload_psr4.php file, my namespace isn't listed in the array:
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
...
'App\\' => array($baseDir . '/app'),
// I expected to see `'L5SimpleFM\\' => array($vendorDir . '/cschmitz/L5SimpleFM/src')` as the last key of this array, but nothing shows past the App key
);
I looked around online and on stackoverflow, but the various answers and suggestions I found didn't resolve the issue.
Can anyone see what I'm missing?
Update
Per Alexandru Guzinschi's answer, I tried telling my Laravel project that there was a local composer repository to inspect by adding the following block into my Laravel project's composer.json file:
"repositories":[
{
"type": "vcs",
"url": "../cschmitz/L5SimpleFM"
}
],
I then moved the package folder starting at the cschmitz directory out to the same level of my laravel project. The file structure looks like this now:
LaravelProjectFolder/
composer.json
cschmitz/
L5SimpleFM/
composer.json
Initially I ran into the error "No driver found to handle VCS repository vendor/cschmitz". After reading a bit I found that to be able to use this kind of local repository, the repo itself needs to be under version control (i.e. git, svn, etc). Makes sense. I hadn't put it under version control yet because this was just a test project used to try to develop the package.
I created a git repository at the root of my Laravel project and ran composer update. Now I'm getting the error:
[Composer\Repository\InvalidRepositoryException]
No valid composer.json was found in any branch or tag of ../cschmitz/L5SimpleFM, could not load a package from it.
This is confusing because I can ls ../cschmitz/L5SimpleFM/composer.json and see the file. I can also run git ls-tree -r master --name-only and see the composer.json file in the local repository version control:
Is there something that would prevent my Laravel project from seeing the local repository's composer.json file?
It is not enough to move your library inside vendor directory, because Composer doesn't scan that directory and doesn't know about your change.
You need to tell Composer about your package by publishing it to Packagist, or by loading it from local file system. In your Laravel project composer.json file, change:
"repositories": [
{
"type": "vcs",
"url": "/path/to/cschmitz"
}
],
"require": {
"cschmitz/l5-simplefm": "dev-develop"
}
Next you need to run composer update in order for your library to be installed.
Not related to your current issue, but it will became an issue soon:
After you will do this and you will continue working on your cschmitz/l5-simplefm library, you will notice that you need to commit your changes made to your library and then run composer update again in your Laravel project before you can use those changes. You have a few solutions here on how to circumvent this issue.

Categories