I am using composer to include a private package in my project that will includes some classes I will use to test against with PHPUnit. Most of the package is being autoloaded correctly and I can call the classes from my unit test, however any class that is named the same as the directory it is in is throwing a "Class not found" error.
The repository is conforming to psr-0 and is located at https://github.com/DeschutesDesignGroupLLC/IPS-Source
File structure example throwing error:
--src
----IPS
------DateTime
--------DateTime.php
Calling $date = new \IPS\DateTime; throws an error.
File structure example NOT throwing error:
--src
----IPS
------Http
--------Url.php
Calling $url = new \IPS\Http\Url; does not throw an error.
Composer.json of private package:
{
"name": "deschutesdesigngroupllc/ips",
"description": "Invision power board source files used to test against",
"homepage": "https://www.invisioncommunity.com",
"version": "4.3.6",
"autoload": {
"psr-0": {
"IPS\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-master": "4.3.6"
}
},
"require": {
"phpdocumentor/phpdocumentor": "dev-master"
}
}
In the first example, you want a file yet you give the path to it's parent. In the second, you again want a file, but this time supply the full path. Unsurprisingly, the first fails and the second succeeds.
It would appear that
$date = new \IPS\DateTime\DateTime;
is what you intended to ask for.
Related
I have created a custom comoposer package and I want to use it on my project with this composer.json:
{
"name": "papillon/test",
"type": "library",
"version": "dev-master",
"require": {
"php": "^7.1.11"
},
"autoload": {
"psr-4": {
"Papillon\\Fountaine\\Eau\\": "src/Papillon/Fountaine/Eau/"
}
}
}
I compress it in zip. In the main project, I add a folder called repo, where I add de composer package zip. Then, I modify the composer.json of the main project like this:
{
"repositories": [
{
"type": "artifact",
"url": "var/main/repo"
}
],
"require": {
"papillon/test": "dev-master"
}
}
I execute composer update and the pakage is added to vendor folder; all seems to be going well... but if I want to test the package from the main project with this script:
<?php
require (__DIR__ . '/vendor/autoload.php');
use Papillon\Fountaine\Eau\FlowerClass;
echo FlowerClass::bloom();
It returns: PHP Fatal error: Uncaught Error: Class 'Papillon\Fountaine\Eau\FlowerClass' not found in .../test_package.php:6
Stack trace:
#0 {main}
thrown in .../test_package.php on line 6
I think that the package may not be recognized by the main project; maybe the package was improperly installed in the main project?
Debugging autoload can be very useful to catch errors. Take care with the route paths, the autoload tryed to find the classes files in a path with a lowercase folder when in the package composer.json the route was definded with that folder uppercase.
Yes another question about the "class not found" error. Either I am missing something, or I misunderstood the PSR-4 logic.
My composer library directory sturcture:
"Scanner" => "src" => "Test.php"
Test.php
namespace MyNS;
class Test
{
}
composer.json
"autoload": {
"psr-4": {
"MyNS\\": "src/"
},
}
So, now I load the library in my project with composer and try using it.
require_once("../vendor/autoload.php");
$test = new MyNS\Test();
Which always results in
"Fatal error: Uncaught Error: Class 'MyNS\Test' not found."
. What am I missing? I am staring at this for days now. I have changed folders, I have changed folder names, I have changed uppper to lower and vise versa. Nothing seems to work.
I am using PHP 7.2.2 and Composer version 1.2.2
Even tried this:
require_once("../vendor/autoload.php");
use MyNS\Test;
$scanner = new Test();
Update
I debugt the Composer ClassLoader.php file (findFileWithExtension($class, $ext)) method and apparently my files are never loaded because I get put an echo "Done" and a die(); at the end of this method which means the file is not found and thus not loaded. What is wrong with my composer.json?
{
"name": "test/test",
"type": "library",
"description": "",
"keywords": ["php"],
"homepage": "",
"license": "MIT",
"authors": [
{
"name": "",
"email": "",
"homepage": "",
"role": ""
}
],
"require": {
"php": ">=7.2.2"
},
"autoload": {
"psr-4": {
"MyNS\\": "src/"
}
}
}
To debug what is happening open ClassLoader.php file then go where findFileWithExtension() method is defined to add an echo statement:
# vendor/composer/ClassLoader.php:386
foreach ($this->prefixDirsPsr4[$search] as $dir) {
if (file_exists($file = $dir . $pathEnd)) {
return $file;
}
// Add this line
echo $file, PHP_EOL;
}
Do not do composer dumpautoload after you manually modified above file until we are done.
Now by executing your PHP file you will see something similar to this at the very beginning of output:
path/to/project/vendor/composer/../../src/Test.php
Which is:
path/to/project/src/Test.php
So this is the file that composer is looking for and should contain something like this:
namespace MyNS;
class Test { }
If there is an issue in including the file then it means you have to care about three things:
Path and filename
Namespace used in file
Class name used in file (class name should be the same as filename)
i think the problem is in your namespace declaration
you calling the class from MyNS but class namespace is namespace MyNS\PSR4;
require_once("../vendor/autoload.php");
$test = new MyNS\Test();
// it should be new MyNS\PSR4\Test();
and make sure, your class file in same directory which you mentioned in composer autoload file
also you have to run dump-autoload command for any change in classnames
you can visit for this autoload feature
So I'm working on a composer plugin that adds a custom command that can be run after an install or update.
I understand the autoloading configuration, and composer is autoloading all classes but it's also missing a file that just contains namespaced functions.
In my plugin composer.json I have the following:
{
"name": "myco/composer-s3-downloader",
"type": "composer-plugin",
"version": "0.1",
"require": {
"composer-plugin-api": "1.1.*",
"aws/aws-sdk-php": "3.20.*"
},
"autoload": {
"psr-4": {"MyCo\\Composer\\": "MyCo/"}
},
"extra": {
"class": "MyCo\\Composer\\S3Downloader"
}
}
My plugin classes load without a problem. All of the classes for my dependencies also load without a problem. So my plugin command code starts off just fine using the AWS SDK.
The problems comes here, when I try to instantiate an S3Client:
private function initClient() {
if (is_null($this->_s3Client)) {
$this->_s3Client = new \Aws\S3\S3Client([
"version" => "latest",
"region" => 'us-west-2',
"credentials" => [
"key" => $this->_creds['key'],
"secret" => $this->_creds['secret'],
]]);
}
}
I get the following error:
PHP Fatal error: Call to undefined function Aws\manifest()
in .../test/vendor/aws/aws-sdk-php/src/AwsClient.php on line 143
I can see the autoload config in the AWS composer.json and it's correct:
"autoload": {
"psr-4": {
"Aws\\": "src/"
},
"files": ["src/functions.php"]
}
The Aws\manifest function is declared in functions.php. And functions.php is then specified in vendor/composer/autoload_files.php. But near as I can tell that file isn't being loaded. So not all of my dependencies are actually available.
What step am I missing that forces the inclusion of autoload_files.php? I'm not doing a single include for anything in the vendor folder. I'm assuming that composer will handle that for me. But I guess I'm wrong.
So after posting an issue at the Composer Github repo, I did learn that the autoloader that runs during composer execution only includes classes. So if you do need to include loose functions, you'll have to manually run the full autoloader.
I added the following method to my Command class that is loaded by composer and defined in the extra section of the plugin's composer.json.
private function getIncludes() {
$vendorDir = $this->composerInstance->getConfig()->get('vendor-dir');
require $vendorDir . '/autoload.php';
}
I just call it in my plugin constructor and everything I need becomes available.
I want my package to ship with a built-in composer-plugin.
I have a structure like this:
composer.json
src/
...
plugin/
composer.json
src/
...
The root composer.json is configured like this:
{
"name": "foo/bar",
"type": "library",
"autoload": {
"psr-4": {
"Foo\\Bar\\": "src/"
}
},
"repositories": [
{
"type": "path",
"url": "./tools",
"options": {
"symlink": false
}
}
],
"require": {
"foo/bar-plugin": "*"
}
}
And the built-in composer-plugin's plugin/composer.json like this:
{
"name": "foo/bar-plugin",
"type": "composer-plugin",
"require": {
"composer-plugin-api": "^1",
"composer/composer": "^1",
"foo/bar": "*"
},
"autoload": {
"psr-4": {
"Foo\\Bar\\Plugin\\": "src/"
}
},
"extra": {
"class": "Foo\\Bar\\Plugin\\MyComposerPlugin"
}
}
Notice how there's a two-way dependency here - the plugin depends on foo/bar, and the project itself depends on foo/bar-plugin.
Here's where it gets weird. During a fresh installation with e.g. composer install or composer update, everything is fine - the plugin does it's thing, which, right now, means just announcing itself on the console.
Now, after installation, if I type just composer, I'd expect to see the plugin announce itself, same as before, right?
Instead, it generates a fatal "class not found error", as soon as it tries to reference any class belonging to the foo/bar package.
It's as though composer lost track of the fact that foo/bar-plugin requires foo/bar, and for some reason it's classes aren't auto-loadable.
Is there any reason this shouldn't be possible? Why not?
Of course I can just package this stuff in separate external package, but that isn't going to make much sense, since these packages are just going to depend on each other - they're effectively one unit, a packaging them as two packages is going to result in a mess of major version increases with every small change, as basically every release of foo/bar will break foo/bar-plugin.
Ideally, I'd like to simply add the composer-plugin directly into the main package, but it appears that's not possible for some reason? Only a package with type composer-plugin is allowed to add plug-ins, it seems?
If the plugin is essentially a part of your package, you should not use it as such. Composer offers alternatives.
As Jens mentioned in a comment to your question, there is 'scripts' key in composer.json. You can invoke shell commands inside, but also call static class methods.
About plugin solution - composer explicitly mentions this on its site:
Composer makes no assumptions about the state of your dependencies prior to install or update. Therefore, you should not specify scripts that require Composer-managed dependencies in the pre-update-cmd or pre-install-cmd event hooks. If you need to execute scripts prior to install or update please make sure they are self-contained within your root package.
(my side note - this also roughly applies to plugins).
Anyway - to provide you with a solution: discard 'plugin' approach. Instead modify your composer.json file so it looks as follows:
composer.json
{
"name": "foo/bar",
"type": "library",
"autoload": {
"psr-4": {
"Foo\\Bar\\": "src/"
}
},
"require": {
},
"scripts": {
"post-install-cmd": [
"Foo\\Bar\\Composer\\Plugin::postInstall"
],
"post-update-cmd": [
"Foo\\Bar\\Composer\\Plugin::postUpdate"
]
}
}
Additionally, in src/Composer folder create Plugin.php:
src/Composer/Plugin.php
<?php
namespace Foo\Bar\Composer;
use Foo\Bar\Test;
/**
* Composer scripts.
*/
class Plugin
{
public static function postInstall()
{
print_r("POST INSTALL\n");
print_r(Test::TEST_CONST);
print_r("\n");
}
public static function postUpdate()
{
print_r("POST UPDATE\n");
print_r(Test::TEST_CONST);
print_r("\n");
}
}
As you see, it prints constant from Test class. Create it in src/:
src/Test.php
<?php
namespace Foo\Bar;
/**
* Test class.
*/
class Test
{
const TEST_CONST = "HERE I AM";
}
Run this and check, how it plays out.
I'm starting work on a new mini-framework project, which I have in a local GIT repo on my machine. I've set up a test project that pulls in the local repo via Composer, however the autoloader isn't working as expected (Fatal Error: Class X not found errors). This is the first time I've used autoloading outside of what is automatically generated (e.g. when using an existing framework) and despite reading around, I can't seem to solve this.
Package
In an attempt to get this working, the package only contains a src directory with a single App.php class on top of the composer.json file in the root.
composer.json
{
"name": "myvendor/framework",
"description": "Framework Description",
"license": "MIT",
"authors": [
{
"name": "Joe Bloggs",
"email": "joe#email.com"
}
],
"autoload": {
"psr-0": {
"Framework": "src/"
}
}
}
Project
composer.json
{
"repositories": [
{
"type": "vcs",
"url" : "../Framework"
}
],
"require": {
"myvendor/framework": "dev-master"
}
}
This successfully clones the local repo and adds the code to the vendor directory.
The namespace is also successfully added to Composer's autoload_namespaces.php file like so;
vendor/composer/autoload_namespaces.php
'Framework' => array($vendorDir . '/myvendor/framework/src'),
When I attempt to load the App class however using the following code, I get the error;
web/index.php
<?php
require_once '../vendor/autoload.php';
$app = new \Framework\App();
You're using the psr-0 specification for the class loader. This means that the full namespace has to be visible in the file structure. The prefix only tells the autoloader were to look for this namespace.
So in your case, you configured that the "Framework" namespace is available in the "src/" directory. This means that the class \Framework\App should life in src/Framework/App.php. In your case, it exists in src/App.php. This means that the autoloader cannot find your class.
However, there is a class loader specification that does what you want: psr-4. This is also the recommended specification (psr-0 might be removed in the future). With PSR-4, the file structure only includes the namespaces after the configured prefixes. So when doing "psr-4": { "Framework\": "src/" }, a class called \Framework\App should life in src/App.php and a class called \Framework\Some\Special\App should life in src/Some/Special/App.php.