Composer not composing - saying file not found - php

I'm using the excellent phpwkhtmltopdf library and want to update to latest version and for this I need to use composer.
File structure:
vendor
--mikehaertl
--php-shellcommand
--php-tmpfile
autoload.php
Composer.json file:
{
"name": "mikehaertl/phpwkhtmltopdf",
"description": "A slim PHP wrapper around wkhtmltopdf with an easy to use and clean OOP interface",
"keywords": ["pdf", "wkhtmltopdf", "wkhtmltoimage" ],
"homepage": "http://mikehaertl.github.com/phpwkhtmltopdf/",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Michael Haertl",
"email": "haertl.mike#gmail.com"
}
],
"require": {
"php": ">=5.0.0",
"mikehaertl/php-tmpfile": "1.0.*",
"mikehaertl/php-shellcommand": "1.0.*"
},
"autoload": {
"psr-4": {
"mikehaertl\\wkhtmlto\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
}
}
I'm trying to use the library like this:
require '/home/bookmark/vendor/autoload.php';
use mikehaertl\wkhtmlto\Pdf;
...
$pdf = new Pdf('http://anysite.com'); <-- error points to this line
The problem is I get the error:
Fatal error: Class 'mikehaertl\wkhtmlto\Pdf' not found in /home/bookmark/public_html/ajax/action.php on line 132
This is my first time using composer, any idea what I'm doing wrong?

If you are using some package, you must not copy their composer.json file - that won't work.
The best thing would be to run composer init once to create an initial composer.json file for your project, and composer require mikehaertl/phpwkhtmltopdf:~2.0 to add this package you want to work with.
After that, it should work.

Related

Why am I getting error Class not found even if the class does exist and is correctly namespaced

I am trying to develop a package with will be required by some of our applications. However, I am unable to test the package when I require it using composer inside an application.
The package has the following composer.json:
{
"name": "armcanada/target-horaire",
"type": "library",
"license": "MIT",
"description": "",
"authors": [
{
"name": "Anthony Duhaime",
"email": "aduhaime#armcanada.ca"
}
],
"require": {},
"autoload": {
"psr-4": {
"Armcanada\\TargetHoraire\\": "src/"
}
},
"minimum-stability": "dev"
}
The package has the following directory structure and has the following testing class:
The package is require in another application in the composer.json:
"require": {
"armcanada/target-horaire": "dev-master"
}
"repositories": [
{
"type": "path",
"url": "../target-horaire"
}
]
Finally, I am importing the class and calling the test() method:
use Armcanada\TargetHoraire\Models\Timeline;
...
dd((new Timeline())->test());
It result with the following error:
"Class 'Armcanada\TargetHoraire\Models\Timeline' not found"
I have tried to composer dumpautoload in both the application and the package, still has no effect.
I also tried to delete the vendor folder and the package-lock.json then run composer install, I am now getting the error:
include(/app/vendor/composer/../armcanada/target-horaire/src/Models/Timeline.php): failed to open stream: No such file or directory. However, the file does exist:
What am I doing wrong?
I think you forgot
extends PHPUnit_Framework_TestCase
in `Timeline.php
and if using autoloading, try
phpunit --bootstrap src/autoload.php

composer.json does not contain a valid json

there is a syntax error in my composer.json file but I just can't seem to find the error. I already have a Laravel object on top of the file, but I also want to add Goaop, like the following code.
how can I do it? thank you
//newly added code
{
"name": "goaop/goaop-laravel-bridge",
"description": "Integration bridge for Go! AOP framework",
"type": "library",
"keywords": ["bridge", "laravel", "aop", "php", "aspect"],
"require": {
"goaop/framework": "^1.0|^2.0",
"laravel/framework": "^5.0"
},
"license": "MIT",
"authors": [
{
"name": "Lisachenko Alexander",
"email": "lisachenko.it#gmail.com"
}
],
"autoload": {
"psr-4": {
"Go\\Laravel\\GoAopBridge\\": "./src"
}
}
}
Looks like you just copied the package's composer.json and pasted it to the bottom of yours. That is not how you install packages.
From your command line run:
composer require goaop/goaop-laravel-bridge
This will update your composer.json and .lock files and install the package.

Class not being autoloaded

I've made a little library for myself and I'm trying to autoload it into my laravel project, it installs fine but whenever I try to use the class it simply says it's not found.
I've checked all the classmap files in vendor/composer and it doesn't seem to be in any of them.
This is my composer.json for my lib:
{
"name": "my-user/aspect-parser",
"version": "1.0.0",
"type": "package",
"require": {
"nesbot/carbon": "^1.22"
},
"autoload": {
"psr-4": {
"AspectParser\\": "src/"
}
}
}
My file structure is:
AspectParser
src
Parser.php
It was an issue with the type in composer.json. I've changed it to library and it adds to the autoload classmap.

Unable to use composer package in laravel

I am trying to use the following class in a laravel 5 project. https://packagist.org/packages/mk-j/php_xlsxwriter#dev-master
I do the:
composer require mk-j/php_xlsxwriter
Then it gets downloaded in my vendor folder. However I am unable to use it in a controller as it cannot find the class XLSXWriter.
I then look at the composer.json of mk-j/php_xlsxwriter and see the following:
{
"name": "mk-j/php_xlsxwriter",
"description": "PHP Library to write XLSX files",
"keywords": ["php", "library","xls", "xlsx", "excel"],
"type": "project",
"homepage": "https://github.com/mk-j/PHP_XLSXWriter",
"license": "MIT",
"autoload": {
"classmap": ["xlsxwriter.class.php"]
},
"require-dev": {
"phpunit/phpunit": "4.3.*"
}
}
It looks like the class is not PSR-4 compliant. Should I tweak the composer.json here to try to make it compliant but then that does not see adequate as another developer would have to do the same.
What can I do to be able to use the class in a controller? I am not very PSR savvy.
Thank You

Package Autoloading Issues

I'm struggling to work out how to correctly build and autoload my first composer package. I understand that if I follow the PSR-0 naming convention, autoloading should "just work". I am trying to set up with:
vendor: Programster
package name: CoreLibs
I have managed to debug a project that includes the package to the point where I am outputting the search path by putting a print in the findFileWithExtension() function of the ClassLoader.php file
/home/stuart/Desktop/test-project/vendor/Programster/CoreLibs/Programster/Programster/CoreLibs/Core.php
I know that the file is actually located at:
/home/stuart/Desktop/test-project/vendor/Programster/CoreLibs/Core.php
My Package's composer.json file:
{
"name": "Programster/CoreLibs",
"type": "library",
"description": "Core libraries for PHP 5.3+",
"keywords": ["core","library"],
"homepage": "http://svn.yadda-yadda.com/php/core-libs",
"license": "none",
"authors": [
{
"name": "xxxx",
"email": "my.email#email.com",
"homepage": "http://mywebsite.com/",
"role": "Developer"
}
],
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-0": {
"Programster": "Programster"
}
}
}
The following is a tiny script in a project that includes the package to test if is working:
<?php
require_once(__DIR__ . '/../vendor/autoload.php');
$loader = new \Composer\Autoload\ClassLoader();
$loader->register();
use \Programster\CoreLibs as programster;
programster\Core::println("hello world");
The project's composer.json file to include the package:
{
"repositories": [ { "type": "composer", "url": "http://satis.mydomain.com/" } ],
"require": {
"Programster/CoreLibs": "dev-trunk"
}
}
Is my package's config wrong, or do I really need to create Programster/CoreLibs/ subdirectories within the CoreLibs repository source code?
use \Programster\CoreLibs as programster;
programster\Core::println("hello world");
Your class name evaluates to \Programster\CoreLibs\Core.
With this PSR-0 autoloading,
"psr-0": {
"Programster": "Programster"
}
the class name will be completely converted into a path name like "Programster/CoreLibs/Core.php", and this path will be searched inside the directory "Programster" relative to your libraries composer.json location.
Using PSR-4 autoloading,
"psr-4": {
"Programster\\CoreLibs\\": ""
}
the prefix in this definition will be removed from the classname, the remainder will be converted to a path (i.e. "Core.php") and be searched in the path mentioned (in this case, in the main directory of the library, because "" + "Core.php" is pointing to a file in without any sub directory).
Your second definition is better in some regards. It uses a longer prefix, which is relevant if you include more than one library using the same prefix, because then Composer would have to search more than one path to find the class. And it uses a shorter path, which also allows for slightly faster disk I/O operations (I haven't done any performance measurements, though).
I managed to get it working by using the PSR-4 standard that others suggested in the comments and updating my package's composer.json file (see the autoload section):
{
"name": "Programster/CoreLibs",
"type": "library",
"description": "Core libraries for PHP 5.3+",
"keywords": ["core","library"],
"homepage": "http://svn.mydomain/core-libs",
"license": "none",
"authors": [
{
"name": "MY name",
"email": "my.email#email.com",
"homepage": "http://my-website.com/",
"role": "Developer"
}
],
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-4": {
"Programster\\CoreLibs\\": ""
}
}
}
This is my script in the project that has the package installed:
require_once(__DIR__ . '/../vendor/autoload.php');
\Programster\CoreLibs\Core::println("hello world");
#sectus was right, I did not need to use $loader = new \Composer\Autoload\ClassLoader();$loader->register();

Categories