Composer - unable to autoload files - php

I have a simple composer project on GitHub with the directory structure as:
/--
-composer.json
-lib/ComposerTest.php
with the ComposerTest.php file as:
namespace lib;
class ComposerTest{
public function doTest(){
return "This class was loaded from Composer\n";
}
}
and composer.json as:
{
"name":"sc/composerTest",
"autoload":{
"psr-0":{
"lib":"./"
}
}
}
Using the following composer.json file, I'm able to include the GitHub project into my vendor folder, but cannot get it to autoload.
{
"description" : "The CodeIgniter Application with Composer",
"require": {
"php": ">=5.3.2",
"codeigniter/framework": "3.1.*",
"kriswallsmith/buzz":"*",
"maltyxx/bower": "^1.0",
"sc/composerTest":"dev-master"
},
"require-dev": {
"mikey179/vfsStream": "1.1.*"
},
"repositories": [
{
"type": "git",
"url": "https://github.com/sc/composerTest.git"
}
]
}
Can someone advise?
Edit: ComposerTest Controller
class ComposerTest extends CI_Controller
{
public function index()
{
$composerTest = new ComposerTest();
echo $composerTest->doTest();
}
}

Related

Interface "Composer\Plugin\PluginInterface" not found

I am writing composer package and in my plugin code I am trying to get Composer\Plugin\PluginInterface but I'm receiving Interface "Composer\Plugin\PluginInterface" not found when I install my package.
My plugin composer.json
{
"name": "..../.....",
"description": "....",
"keywords": [
//...
],
"license": "MIT",
"authors": [
{
"name": "...",
"email": "...",
"homepage": "...",
}
],
"type": "composer-plugin",
"require": {
"php": ">=8.0",
"composer-plugin-api": "~2.0", <-- here
},
"autoload": {
"psr-4": {
"...\\....\\": "src/"
}
},
"extra": {
"class": "...\\....\\....",
"laravel": {
"providers": [
"...\\....\\...."
]
}
}
}
And here is my plugin structure: (In case I forget to add something!)
<?php
namespace ...\....;
use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface; <-- Not Found!
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
class MyClassPlugin implements PluginInterface, EventSubscriberInterface
{
public static function getSubscribedEvents(){}
public static function update(){...}
public static function myFunctionOne(){...}
public static function myFunctionTwo(){...}
public static function myFunctionThree(){...}
public static function generateConfig(){...}
public static function activate(){...}
public static function deactivate(){...}
public static function uninstall(){...}
}
Also this is my installed composer: Composer version 2.3.5
Any idea?

Making Laravel Package. but "Class Not Found"

I'm making laravel's package.
But Class "Username/PackageName/Class" not found.
I read this.
But I can't find problem.
Please help me.
This is my codes
composer.json
{
"name": "username/packageName",
"require": {
"php": "7.3",
"laravel/framework": "^6.0"
},
"autoload": {
"psr-4": {
"username\\packageName\\": "src/"
}
},
"license": "MIT",
"authors": [
],
"extra": {
"laravel": {
"providers": [
"username\\packgeName\\TestServiceProvider"
]
}
}
}
src/TestServiceProvider
<?php
namespace username\packageName;
use Illuminate\Support\ServiceProvider;
class TestServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
$this->app->bind('test', function ($app) {
return new Test();
});
}
}
src/Test.php
<?php
namespace username\packageName;
class Test {
public static function test(){
return 'abc';
}
}
in Controller
use username\packageName\Test;
/* ~~~~ */
dd(Test::test()); // error
after add current composer.json.
"repositories": [
{
"type": "path",
"url": "vendor/username/packageName",
"symlink": true
}
]
> composer dump-autoload
Thank you for reading my codes.
username -> my username
packageName -> my package name
Have you extend Facades class in your Test Class.
If you want to fetch all function of your files statically,
class Test extends Facades {
public static function getFacadeAccessor(){
return 'classNameWhereFunctionIsWritten' //say TestService has test() function
}
}
And Pass this classNameWhereFunctionIsWritten in your provider 'classNameWhereFunctionIsWritten' here.

Creating composer package for Codeigniter 4

I am trying to create my first composer package for Codeigniter 4. However, I always get an error Class 'Myapp\Settings\Greet' not found. I'm totally lost.
I created a folder inside the ThirdParty folder named myapp-settings. Inside of that folder is another folder called src and composer.json.
Here's the content of that composer.json
{
"name": "myapp/settings",
"description": ".",
"license": "MIT",
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Myapp\\Settings\\": "src"
}
},
"require": {}
}
I created a test file inside the src folder named Greet.php
<?php namespace Myapp\Settings;
class Greet
{
public function hello()
{
return 'Hey, there!';
}
}
On codeigniter's App\Config\Autoload.php
public $psr4 = [
'Myapp\Settings' => APPPATH . 'ThirdParty/myapp-settings/src'
];
Then on codeigniter's default controller I called it.
<?php namespace App\Controllers;
use Myapp\Settings\Greet;
class Home extends BaseController
{
public function index()
{
$h = new Greet();
echo $h->hello();
}
//--------------------------------------------------------------------
}
Once I run it I got an error Class 'Myapp\Settings\Greet' not found. APPPATH\Controllers\Home.php at line 9. How can I fix this?
Instead of editing the codeigniter's app/Config/Autoload.php, revert it and add these lines to the project composer.json:
"repositories": [
{
"type": "path",
"url": "app/ThirdParty/myapp-settings"
}
],
// "require": {
Then run composer require myapp/settings
Since I have also stuck at this problem, a GitHub repository is created just for it. You might check and clone, watch the commits to understand the steps.

Silex web test fails

I'm trying to write a simple web test to test my silex api using phpunit but
I keep getting this error when i try to run it..
1) App\Tests\BlogControllerTest::testInitialPage
TypeError: Argument 1 passed to Symfony\Component\HttpKernel\Client::__construct() must implement interface Symfony\Component\HttpKernel\HttpKernelInterface, null given, called in C:\vendor\silex\silex\src\Silex\WebTestCase.php on line 63
This is my web test
<?php
namespace App\Tests;
use Silex\WebTestCase;
class BlogControllerTest extends WebTestCase
{
public function createApplication()
{
require dirname(dirname(__DIR__)) . '/src/Application.php';
}
public function testInitialPage()
{
$client = $this->createClient();
$crawler = $client->request('GET', '/Blog/1');
$this->assertTrue($client->getResponse()->isOk());
}
}
The content of my Composer file:
{
"require": {
"silex/silex": "~2.0",
"symfony/validator": "^4.0",
"crell/api-problem": "~1.7",
"tobiassjosten/responsible-service-provider": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "6",
"symfony/browser-kit": "^4.0",
"symfony/css-selector": "^4.0"
},
"autoload": {
"psr-4": {
"App\\": "src/",
"Tests\\": "tests/"
}
}
}
From documentation:
For your WebTestCase, you will have to implement a createApplication method, which returns your application instance:
public function createApplication()
{
// app.php must return an Application instance
return require __DIR__.'/path/to/app.php';
}
So here's what you need:
public function createApplication()
{
return new Application();
}
But in the most cases it would be better to return configured application, from your bootstrap (bootstrap example):
public function createApplication()
{
require __DIR__.'../web/index-test.php'; // path to your bootstrap file
return $app;
}

Slim3 PSR-4 autoloader

I am writing Slim3 API, for some reason slim is not loading namespace defined in composer.
Here is the project structure.
FolderStruc:
projectApi
- composer.json
- src
- public
- index.php
- ProjectName
- Api
- Controllers
- Entities
- Commands
My composer file packages and PSR-4 autoloader settings.
composer.json
{
"require": {
"slim/slim": "^3.0",
"symfony/yaml": "3.1",
"symfony/console": "3.1",
"symfony/process": "3.1",
"doctrine/orm": "^2.5",
"doctrine/doctrine-bundle": "^1.6",
"doctrine/doctrine-cache-bundle": "^1.2",
"sabre/xml": "1.1.0",
"php-di/php-di": "#dev",
"php-amqplib/php-amqplib": "#dev",
"ramsey/uuid": "dev-master",
"monolog/monolog": "~1.15#dev",
"predis/predis": "~1.0.1",
"spipu/html2pdf": "^4.5",
"iio/libmergepdf": "~3.0"
},
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"src\\ProjectName\\": "src/ProjectName/"
}
},
"autoload-dev": {
"psr-4": { "src\\ProjectName\\Tests\\": "tests" }
}
}
index.php
CategoryController
namespace ProjectName\Api\Controllers;
class CategoryController
{
/**
* #param \Slim\App $app
* #return array
*/
public function index(\Slim\App $app)
{
return ['Cats', 'Cats2', 'Cats3'];
}
}
** routes.php **
$app->get('/v1/category/list', function (Request $request, Response $response) {
$response = $response->withHeader('Content-type', 'application/json');
$categoryCtrl = new \ProjectName\Api\Controllers\CategoryController();
});
Slim fails to load namespace \ProjectName\Api\Controllers\CategoryController
any idea where and what is going wrong?
Best Regards
Danyal
You have incorrect autoloader definitions in your composer.json file.
The pattern is:
"psr-4": {
"Namespace\\Prefix\\": "/path/to/source/root"
}
So in your case it probably should be:
"autoload": {
"psr-4": {
"ProjectName\\": "src/ProjectName/"
}
},
"autoload-dev": {
"psr-4": { "ProjectName\\Tests\\": "tests/" }
}
autoload-dev definition is assuming there's a tests directory under project root path, which is not mentioned in your question.

Categories