TYPO3 extension autoloader not loading libraries - composer-php

I recently started with TYPO3 and composer as a student. I'm kind of new to all this and I can't get my composer to autoload an api library from Mautic that I want to have. My code keeps telling me that it can't find the classes.
In my extension root directory I have composer.json and it looks like this
{
"name": "woeler\/wlr_typo_mautic",
"config": {
"vendor-dir": "Libraries"
},
"description": "Typo3 to Mautic connection",
"type": "typo3-cms-extension",
"require": {
"mautic\/api-library": "^2.6"
},
"autoload": {
"psr-4": {
"Woeler\\WlrTypoMautic\\": "Classes",
"Mautic\\Auth\\": "Libraries\/mautic\/api-library\/lib\/Auth\/"
}
},
"license": "MIT",
"authors": [
{
"name": "MyName",
"email": "myemail#domain.tld"
}
]
}
I'm probably making a basic mistake, but the tutorials I can find are not really helping me. I have a class in the folder Classes/Controller and I want it to use a class that should fall under the namespace Mautic\Auth\ but it simply tells me it cannot be found.
The actual location of the mautic class is [extension root]/Librariesmautic/api-library/lib/Auth/ApiAuth.php
I require my Libraries/autoload.php in my ext_tables.php file.
Can anyone see what I'm doing wrong?

the package mautic/api-library brings his own composer.json with the psr-4 autoload section, so you don't need to add it to your composer.json.
If you require a package it will installed in the vendor folder (whatever the root composer.json is targeting it), so you can't be able to know where the package will be located.
Just remove the psr-4 section and let the composer autoloader do its magic.

Related

Composer dependency on local repository

I want to set up a main project depending on other local projects. That works actually fine. But when a dependency has a dependency by itself, composer update exists with an error.
Set up:
I have three packages.
/home/programming/git
vendor.main
vendor.depA
vendor.depB
vendor/main depends on vendor.depA which in turn depends on vendor.depB.
What works
The vendor/main composer.json defines a dependency on vendor.depA:
{
"name": "vendor/main",
"description": "main",
"repositories": [
{ "type": "vcs", "url": "/home/user/programming/git/vendor.depA/" },
{ "type": "vcs", "url": "/home/user/programming/git/vendor.depB/" }
],
"require": {
"vendor/depA": "dev-master"
}
}
And vendor.depA has no other dependency:
{
"description": ". . .",
"name": "vendor/depA"
}
composer update works as expected and installs vendor/depA.
What doesn't work
If vendor.depA defines a dependency on vendor/depB composer update fails:
{
"description": ". . .",
"name": "vendor/depA",
"require": {
"vendor/depB": "dev-master"
}
}
Adding the repository to the vendor.depA's composer.json doesn't help.
composer update runs through, if I add the dependency to vendor.main's composer.json (so, there is no typo, or another problem with vendor/depB, etc), but that doesn't really help, since depA should have the dependency.
What am I doing wrong here?
Edit: The error message
~/composer.phar update Loading composer repositories with package
information Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of
packages.
Problem 1
- vendor/depA dev-master requires vendor/depB dev-master -> no matching package found.
- vendor/depA dev-master requires vendor/depB dev-master -> no matching package found.
- Installation request for vendor/depA dev-master -> satisfiable by vendor/depA[dev-master].
Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setting see
<https://getcomposer.org/doc/04-schema.md#minimum-stability> for more
details.
- It's a private package and you forgot to add a custom repository to find it
Read <https://getcomposer.org/doc/articles/troubleshooting.md> for
further common problems.
I think that is not working with "dev-master". In you case that makes no sense then "dev-master" is always the latest version of your master branch. So you should tag a commit and work with tags and set your requirement depending on a tag number.
Here is an example how you can define a package if you really want to define it in your composer file.
"repositories": [
{
"type": "package",
"package": {
"name": "vendor/yourpackage",
"version": "v1.0.0",
"source": {
"url": "git#your.gitserver.com:vendor/project.git",
"type": "git",
"reference": "dd6ed3c8"
}
}
}
]
Then you have created a package with the specified version number "1.0.0" and that package is set to a git commit.

When I try to Submit my repo on packagist.org, I get "undefined"

The repo is template-PHP-project. When I use the Submit function on packagist.org I get a pop-up that just says "undefined".
Clearly there is something wrong with my composer.json but I don't know what. (I can run composer update without errors.) It contains the following:
{
"name": "unixnut/template-php-project",
"description": "A self-contained project that uses the Composer autoloader to load class files",
"license": "GPL",
"authors": [
{
"name": "Alastair Irvine",
"email": "alastair#plug.org.au"
}
],
"require": {},
"include-path": ["app/include", "contrib"],
"autoload": {
"psr-4": {
"XYZ\\": "app/classes/XYZ"
}
}
}
Try to use composer validate command.
I just validated your file locally, and the only error is about the license. Fix and check again.
$ composer validate
./composer.json is valid, but with a few warnings
See https://getcomposer.org/doc/04-schema.md for details on the schema
License "GPL" is not a valid SPDX license identifier, see https://spdx.org/licenses/ if you use an open license.
If the software is closed-source, you may use "proprietary" as license.

Laravel vendor autoloading doesn't work

Currently, I am new on package development, and have a hard time on creating a new package. To the point, I want to make a new package, but the autoload (PSR-4 and classmap) seems not working. I get class not found exception. I tried to find the answer to my question everywhere, but still have not found any help.
Here is my composer.json file:
{
"name": "imamassi/tree",
"description": "Tree generator from an array/object with parent id",
"license": "MIT",
"authors": [
{
"name": "Imam Assidiqqi",
"email": "assidiqqi.imam#gmail.com"
}
],
"minimum-stability": "dev",
"require": {},
"autoload":{
"psr-4":{
"Imamassi\\": "src/"
},
"classmap":["src/"]
}
}
My package directory:
It already took me for several hours just to figure how to make this work, hope I'll find the answer here :)
NB: I already done dump-autoload for several times, still can't make this work.
The autoloading path must lead to the exact directory where the namespace is used.
That means for your composer.json:
"autoload": {
"psr-4": {
"Imamassi\\": "src/Imamassi"
}
}
After changing the main composer.json's autoload section, don't forget to rebuild autoloading cache via:
composer dump-autoload

Cannot update yii2 via composer bower-asset/jquery could not be found

I was updating my yii2 via composer then reverted back to the old beta version.
Here is the error on my composer:
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- The requested package bower-asset/jquery could not be found in any version, there may be a typ
o in the package name.
Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setti
ng
see <https://groups.google.com/d/topic/composer-dev/_g3ASeIFlrc/discussion> for more details.
Read <http://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.
Tried searching for bower-asset/jquery at packagist but it is not found.
Thanks for the help :)
Finally fixed it, just followed the steps on the UPGRADE.md doc
If you are using Composer to upgrade Yii, you should run the following command first (once for all) to install the composer-asset-plugin:
composer global require "fxp/composer-asset-plugin:^1.2.0"
(See http://www.yiiframework.com/doc-2.0/guide-start-installation.html#installing-from-composer for latest version.)
You may also need to add the following code to your project's composer.json file :
"extra": {
"asset-installer-paths": {
"npm-asset-library": "vendor/npm",
"bower-asset-library": "vendor/bower"
}
}
Hopes this helps :)
For me helps to remove folder ~/.composer and execute command:
php composer.phar global require "fxp/composer-asset-plugin:1.*"
Then just run again
php composer.phar update
Found a cleaner solution. Just add following repository in your composer.json file
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
]
and watch the magic
If you don't want to use fxp/composer-asset-plugin then all you have to do is to follow these simple instructions from Yii2 documentation.
Using asset-packagist repository
This way will satisfy requirements of the majority of projects, that need NPM or Bower packages.
Note: Since 2.0.13 both Basic and Advanced application templates are
pre-configured to use asset-packagist by default, so you can skip this
section.
In the composer.json of your project, add the following lines:
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
]
Adjust #npm and #bower aliases in you application configuration:
$config = [
...
'aliases' => [
'#bower' => '#vendor/bower-asset',
'#npm' => '#vendor/npm-asset',
],
...
];
Visit asset-packagist.org to know, how it works.
If you don't need the update for bower-asset, you can require yidas/yii2-composer-bower-skip before yiisoft/yii2. in composer.json file:
"require": {
"php": ">=5.4.0",
"yidas/yii2-composer-bower-skip": "~2.0.0",
"yiisoft/yii2": "~2.0.5",
"yiisoft/yii2-bootstrap": "~2.0.0"
}
After that, you can update Composer smoothly without bower-asset.
See https://github.com/yidas/yii2-composer-bower-skip
Just in case for anyone upgrading Yii 2.0.41 - 2.0.43,
should be noted that you need to install the "external" bower-asset.
Run the following
composer require yidas/yii2-bower-asset
Then, need to update the aliases inside config (depends on your structure) for the Yii to handle the new bower-asset folder.
// here is important part
'aliases' => [
'#bower' => '#vendor/yidas/yii2-bower-asset/bower',
],
//below is just another config just ignore. example purpose don't copy
'components' => [
'db' => [
Then, reload your Yii app. Should be fine.
-Extra-
Here is the example of the composer.json for anyone who need the updates to 2.0.43
{
"name": "yiisoft/yii2-app-advanced",
"description": "Yii 2 Advanced Application Template",
"keywords": ["yii2", "framework", "advanced", "application template"],
"homepage": "http://www.yiiframework.com/",
"type": "project",
"license": "BSD-3-Clause",
"support": {
"issues": "https://github.com/yiisoft/yii2/issues?state=open",
"forum": "http://www.yiiframework.com/forum/",
"wiki": "http://www.yiiframework.com/wiki/",
"irc": "irc://irc.freenode.net/yii",
"source": "https://github.com/yiisoft/yii2"
},
"minimum-stability": "dev",
"require": {
"php": ">=5.4.0",
"yiisoft/yii2": "2.0.43",
"yiisoft/yii2-bootstrap": "*",
"yiisoft/yii2-swiftmailer": "~2.0#dev",
"yiisoft/yii2-redis": "~2.0.0",
"yiisoft/yii2-elasticsearch": "~2.0.0",
"bryglen/yii2-apns-gcm": "1.0.5",
"snhccm/baidu-push": "dev-master",
"google/cloud": "dev-master",
"minishlink/web-push": "6.0.7",
"understeam/yii2-fcm": "~0.1",
"yidas/yii2-bower-asset": "2.0.13"
},
"require-dev": {
"codeception/codeception": "*",
"yiisoft/yii2-debug": "*",
"yiisoft/yii2-gii": "*",
"yiisoft/yii2-faker": "*"
},
"config": {
"process-timeout": 1800
},
"extra": {
"asset-installer-paths": {
"npm-asset-library": "vendor/npm",
"bower-asset-library": "vendor/bower"
}
}
}
As described in YII2 repository documentation: https://asset-packagist.org/site/about
We can solve this problem by adding aliases on those folders in our config.
It will looks like that:
$config = [
...
'aliases' => [
'#bower' => '#vendor/bower-asset',
'#npm' => '#vendor/npm-asset',
],
...
];
It works perfectly!
Simple and clean solution:
In composer.json just replace the bower-asset/jquery line with:
"yidas/yii2-bower-asset":"*"
I propose we add also bower-asset/datatables to the yidas/yii2-bower-asset
My Problems with accepted solution of adding fxp/composer-asset-plugin are that the plugin is significantly slowing down the composer system, impacts everywhere, isn't always portable across operating systems and environments, has errors with PHP7.2 relating to inconsistent method names. So, I prefer my quicker to develop, faster at runtime, more local, and more compatible solution.
I tried all the mentioned steps like adding following in main.php
$config = [
...
'aliases' => [
'#bower' => '#vendor/bower-asset',
'#npm' => '#vendor/npm-asset',
],
...
];
composer.json
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
]
Doing "composer install/update" was still not installing bower packages given by yii2-bootstrap.
I found, I was using composer.phar 2x to set this up. I downgraded composer.phar to 1x and all works well without having the need of fxp/composer-asset-plugin plugin.

Custom repository doesn't read my composer.json?

I've forked https://github.com/jasonlewis/basset and is now trying to add my fork to my project. But it doesn't seem to read my forked composer.json file after I've run composer update, why? What am I doing wrong?
I've added this repository after require: {}:
"repositories": [
{
"type":"package",
"package": {
"name": "marwelln/basset",
"version":"master",
"source": {
"url": "https://github.com/Marwelln/basset.git",
"type": "git",
"reference":"master"
}
}
}
],
My forked composer.json looks like this (nothing is changed from the original except name):
{
"name": "marwelln/basset",
"description": "A better asset management package for Laravel.",
"keywords": ["assets", "basset", "laravel"],
"license": "BSD-2-Clause",
"authors": [
{
"name": "Jason Lewis",
"email": "jason.lewis1991#gmail.com"
}
],
"require": {
"php": ">=5.3.0",
"kriswallsmith/assetic": "1.1.*"
},
"require-dev": {
"mockery/mockery": ">=0.7.2",
"illuminate/config": "4.0.*",
"illuminate/console": "4.0.*",
"illuminate/filesystem": "4.0.*",
"illuminate/log": "4.0.*",
"illuminate/routing": "4.0.*",
"illuminate/support": "4.0.*",
"symfony/process": "2.3.*"
},
"suggest": {
"aws/aws-sdk-php": "Deploy static assets directly to your S3 buckets.",
"rackspace/php-cloudfiles": "Deploy static assets directly to your Cloud Files container."
},
"autoload": {
"psr-0": {
"Basset": "src/"
},
"classmap": [
"tests/Cases/FilterTestCase.php"
],
"files": ["src/helpers.php"]
},
"extra": {
"branch-alias": {
"dev-master": "4.0-dev"
}
},
"minimum-stability": "dev"
}
With this, the Basset namespace isn't registered in autoload_namespace.php nor am getting "kriswallsmith/assetic": "1.1.*", but if I use the original require: { "jasonlewis/basset" : "dev-master"} it adds it just fine. What is it I'm missing?
You've done some things that are considered not the best practice with Composer.
First of all, you should try to avoid including repositories of the type "package" with all the details of where to find the stuff you need. This will become a maintenance nightmare in the long run. Packages should only be included in your composer.json if you are absolutely sure the maintainer of the software will not include a composer.json file himself. The documentation states on http://getcomposer.org/doc/04-schema.md#repositories:
package: If you depend on a project that does not have any support for composer whatsoever you can define the package inline using a package repository. You basically just inline the
composer.json object.
This case does not apply here, because you maintain the repository to be used yourself and should be able to put in a composer.json.
Now the process of forking and extending an existing library and then using that within your code is supported by Composer, but not the way you did it. First of all, you SHOULD replace the name of the vendor, because you take over that role, and your repository and the software should be distinguishable from other packages. So it is the correct thing to rename "jasonlewis/basset" into "marwelln/basset" and only use that name if you want to reference your own version.
Now the problem might be that if you forked a popular library, and are using other libraries that do require the original, you'll end up adding both packages. That's what the "replace" field is for: You can state in your own composer.json that you think your package is able to replace the original software. Some details are here: http://getcomposer.org/doc/04-schema.md#replace
I gave a detailed answer on how "replace" works here: How does the "replace" property work with composer?
In the end, I think you should do the following (and you still can change that):
Clone the original, change the composer.json to bear your own vendor name as the "name" and add a "replace" with the version of the original software you are changing. You should probably be strict about the version, because you really only replace the version you know about. If later you still know you replace a newer version, you can change the "replace" info.
Then you have to add your own repository as you did, and require the software name with your own vendor name.
You probably did most of that at some point in time, but the step with adding the replacement info is the missing link.
I changed repository to this:
"repositories": [
{
"type":"vcs",
"url": "https://github.com/Marwelln/basset"
}
],
And then used jasonlewis/basset instead of marwelln/basset. It's now working as it should.

Resources