How can I check if PHP Composer autoload is working? - php

I am trying to transfer a small web application, which uses PHP Composer, from an old out of date server to a new server.
On the new server, I have run composer install to install the dependencies from the composer.lock file.
The web application requires a config file to set itself up (not that it does a lot of setting up), which includes the line
require_once __DIR__ . '/vendor/autoload.php';
…which should locate the autoloader relative to the config file, and make the packages installed by Composer known to the system.
However, this doesn't seem to be working properly, as when the web app tries to use SwiftMailer (which was installed via Composer), it says:
PHP Fatal error: Uncaught Error: Call to undefined method Swift_SmtpTransport::newInstance()
…in the file that calls it, which obviously suggests that it does not know about it and cannot find it.
Is there a way that I can meaningfully check what the result of the autoloader include is and see what it is (or is not) doing?

Make sure to add a use statement to your classes. You can arrange your classes and give your classes namespaces then allow composer to also auto-load your classes.
You can tell composer how your classes are arranged according to namespaces as follows:
In the autoload section of your composer.json file edit it to know how the namespaces of your classes is arranged
"autoload": {
"psr-4": {
"App\\": "src/",
"Mynamespace\\": "lib/src/ProjectSrc/"
}
},
The autoload section now tells composer the Classes in the ProjectSrc folder will have a root namespace of Mynamespace. From this information composer can determine all other sub namespaces in your directory tree. This will not happen automatically composer has to rebuild its mapping of classes with the composer dump-autoload command.

Related

Manually attach package to Laravel (unable to use composer due to firewall)

I can't use composer to handle my dependencies due to the corporate firewall. At the moment I'm trying to use Barry vd Heuvel's DomPDF wrapper for Laravel and tried to:
Download the zipfile from Github (master)
Updated composer.json (not sure if its needed, but did it anyway) and added "barryvdh/laravel-dompdf": "*" in the require container.
Create the folder structure: vendor/barryvdh/laravel-dompdf
Place all files from the package in there (config-folder, src-folder and the files .gitignore, composer.json and readme.md)
Add the service provider and facade in my app.php. Service provider is listed as Barryvdh\DomPDF\ServiceProvider::class and the facade is aliased like 'PDF' => Barryvdh\DomPDF\Facade::class
Ran composer dump-autoload
After refreshing the browser I'm getting Class 'Barryvdh\DomPDF\ServiceProvider' not found. I also tried to run php artisan cache:clear and php artisan dump-autoload but the last one fails over the fact it can't find Barryvdh\DomPDF\ServiceProvider.
What have I forgotten to do to make it work?
Update
I've tried the suggested answer from Wouter J and the composer.json now looks like:
..
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Barryvdh\\DomPDF\\": "vendor/barryvdh/laravel-dompdf/src"
},
..
I've verified if the composer dump-autoload had any effect but I think it had. Because the entry is now also listed in vendor/composer/autoload_psr4.php like:
return array(
// more entries
'Barryvdh\\DomPDF\\' => array($vendorDir . '/barryvdh/laravel-dompdf/src'),
'App\\' => array($baseDir . '/app'),
);
I believe at this point this is working, but the Facade isn't responding. When I try to call something like PDF::loadView(...) and I let PhpStorm import the class (vendor/barryvdh/laravel-dompdf/src/PDF.php) it throws an error I can't call the method loadView statically. According to the documentation I should be able to call it like this:
$pdf = PDF::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');
But that results in Non-static method Barryvdh\DomPDF\PDF::loadView() should not be called statically, assuming $this from incompatible context on my end.
Suggestions?
Composer autoload still doesn't know anything about how to download the package. You have to configure autoloading like this:
{
"autoload": {
"psr-4": { "Namespace\\Of\\The\\Package\\": "vendor/the/package" }
}
}
I know this question is old, but since I don't see any solution, here is mine: I had exactly the same problem. Was able to solve by running composer dumpautoload on my localhost and then copied the folder 'vendor/composer' to the shared hosting without an SSH access. Hope this will help someone in similar situation.
The problem addresses that application don't know about DomPDF\ServiceProvidor or anything related to DomPDF because barryvdh/laravel-dompdf itself depends dpmpdf/dompdf package which is still missing and does not comes with barryvdh/laravel-dompdf
the package dompdf/dompdf also depends on several extension i.e.
PHP version 5.3.0 or higher
DOM extension
GD extension
MBString extension
php-font-lib
php-svg-lib
have a look at package here https://packagist.org/packages/dompdf/dompdf download this and put in your vendor directory then update your composer dumpautoload it should be working

Laravel 5.1 Package Development - loading packages dependencies in development

Hi I'm trying to develop a package in Laravel 5.1. Thanks to help here I have the basics set up.
My current problem is how to load dependencies for the package while I'm developing it.
In the packages composer.json I have added dependencies and have these installed now in a vendor folder within my packages development folder. This is not the frameworks root vendor folder.
Here's my require section of the packages composer.json:
"require": {
"illuminate/support": "~5.1",
"php" : ">=5.3.0",
"google/apiclient": "dev-master"
},
Because they are not part of the main autoload process what is the best approach to ensuring the dependencies for my package are loaded correctly from within the development folder? How do I include the autoload? I'm concerned that if I reference them to their current location/namespace that it will break when later installed as a package in another app.
in my code I have the following:
$client = new \Google_Client($config);
which gives the error:
Class 'Google_Client' not found
I can get round this by adding this dependency to the main composer.json but don't think that is the correct approach to keep the package development independent (if that makes sense)
When I developed in L4.2 there was the workbench which took care of the loading which of course no longer features in L5.1
Any help and best practice appreciated
Because they are not part of the main autoload process
I think you misunderstood how composer dependencies are managed. When in your main compose.json file you list a dependency, composer will add it to the main autoload process as well as all their dependencies, and the dependencies of their dependencies, and so on recursively.
You don't have to worry about where the dependencies are stored or how Composer will load them. Composer will automatically add them to the autoload file and all you have to do is make sure you require the composer autoload file. Once you require the composer autoload file, all the classes and functions loaded by composer will be available. Provided you required the composer autoload file all you have to do to use the classes from any of the installed packages is to make sure you address them using the proper namespace. Composer is smart enough to know where all classes are stored and how to load them (that is what psr-0, psr-4,... are for).
So if you are developing a Composer package, lets call it 'A', and you list the package 'C' as one of the dependencies of your package 'A', composer will add it to the autoload file for you. If you use another package, lets say, Laravel, which has a dependency of you package 'A', then also the package 'C' will be available within Laravel, since it is a dependency of 'A'.
I.e: If this is your composer.json file
{
"name": "foo/bar",
"require": {
"google/apiclient": "1.0.*"
}
}
This code will work
require_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$youtube = new Google_Service_YouTube($client);
Note I've required the composer autoload file, which seems to be your problem. When you are using Laravel, it will add that file for you.

Custom composer autoload directory, loading multiple packages

Im developing a group of packag within a Laravel 5 app.
all the packages have the following structure.
Folder structure:
Vendor/Package/src
composer.json
"autoload": {
"psr-4": {
"Vendor\\Package\\": "src/"
}
},
I would like to be able to have a separate folder outside of the vendors/ folder that contains the packages, under development. As they are the basis for the application. NOTE: These packages will not be downloaded through composer, just auto-loaded with composer.
e.g.
\Laravel-App
\app
\bootstrap
\config
\database
\public
...
\src
The \src directory containing all the packages mentioned above.
It's not relevant that there is a composer.json inside each package, because the are not managed by composer (, yet). Their autoload definition will only be used for an autoload dump by Composer, if the package is installed/updated.
Here there are not handled by Composer, just sitting around in a folder.
There are multiple ways of solving the autoloading problem.
You could:
use the PHP autoload functions to register your own autoloader (during bootstrap)
use Laravel's Autoloader to add the path to these packages (during app bootstrap, somewhere after laravel autoloader init)
use Composer's Autoloader to add the paths to these packages (during app bootstrap, after Composer init)
but the easiest way would be to define the mapping in the autoload section of the composer.json file of your main application. Then they will be included in the dumped autoloader, after install or update of your Composer managed dependencies.

Why do CodeIgniter projects sometimes include composer.phar package?

I'm new to Composer and in my current project I would like to install a bunch of PHP libraries like:
Doctrine
Security Library (Which i have no idea but looking for in CodeIgniter)
Bootstrap layout libraries and other when necessary
For that matter , I would like to use Composer based library management in my application,
and i get confused that if i have to include composer.phar on my project directory or not.
since i have it on my environment path and I can run Composer form command line .
How can integrate the above libraries into my codeigniter application then..
Appreciate your toughs!
The composer.phar file is an executable and it should not be committed. What it actually does is that it looks in your composer.json file and there you can declare some more dependencies (libraries for example) and their version:
{
"require": {
"doctrine/orm": "*"
}
}
The version in this case is declared with "*" so Composer will get the latest version. This is very useful if there are more people on the project, to make sure all of them have the same version of dependencies installed (so the composer.json file must be committed).
If you run "composer.phar update" on the other hand, this will get the latest version of all dependencies, no matter the version placed in composer.json and updates the lock file with the new versions.

How to use Zend Framework 2 class in my project?

I juste want to have the possibility to use the class of this library but after 2 hours impossible :
So how to use the Zend Framework 2.1 with his autoloader to just use class of this library and not create a ZF project?
I have try everything with the classmap_generator, inlude_path... i haven't any error but it still return me :
Could not find action class? Could not find version class!
Thanks you.
You can install individual Zend Framework modules via composer, which will take care of most of this for you:
composer.json
{
"require": {
"zendframework/zend-http": "2.*"
},
"repositories": [
{
"type": "composer",
"url": "http://packages.zendframework.com/"
}
],
"minimum-stability": "dev"
}
this should take care of any autlloading for you. Composer would generate an autoloader which you would just include in your app:
require 'vendor/autoload.php';
there's a load of composer examples out there:
https://packages.zendframework.com/#composer
http://net.tutsplus.com/tutorials/php/easy-package-management-with-composer/
Since ZF2 components are decoupled and focused on a specific task, one class often uses another to do its business. Simply including a single class file is usually not sufficient.
As a result, ZF2 definitely requires some autoloading. But you don't have to use the ZF2 autoloader.
If you have no autoloading already in place in your app, then it's actually pretty easy to add:
Make sure the path to the Zend library is on your include_path.
Register a standard PSR-0-compliant autoloader using spl_autoload_register()
Something like:
spl_autoload_register(function($class)){
$file = str_replace('\\', '/', $class) . '.php';
$resolvedFile = stream_resolve_include_path($file);
if (file_exists($resolvedFile)) {
include $resolvedFile;
return $class;
}
});
If you already have an autoloader for your application, then I'd be quite surprised if it was not PSR-0-compliant. As before, make sure that the Zend library is on the include_path.
Even if the Zend library is not on your include path, you can create a custom autoloader that is aware of where in your filesystem the library is based.
In all cases, usage of the desired class is then:
$myinstance = new \Zend\Component\Class();
With the correct autoloading in place - however you choose to go about it - this class can use any other Zend classes that it needs to go about its business.
After many search, i have install composer. But i'm not very happy of this solution cause it touch my system.
Here is my procedure, i still say that ZF documentation is very poor.
Install ZEND FRAMEWORK WITH COMPOSER
1) Allow whitelist
suhosin.executor.include.whitelist = phar
In file : nano /etc/php5/cli/conf.d/suhosin.ini
2) Download of composer
Apt-get install curl
curl -sS https://getcomposer.org/installer | php
3) Move composer
mv composer.phar /usr/local/bin/composer
4) Copy the composer.json
Copy composer.json (From zend framework archive) a my root folder application
5) Composer install
Execute where the composer.json is
Now you will have a folder named "vendor" in your app folder, to use in your php code :
include("vendor/autoload.php");
new Zend\Mail\Storage\Pop3(...);
And now it work for me.
Bug allowed memory size :
php -r "echo ini_get('memory_limit').PHP_EOL;"
change to 1024M
nano /etc/php5/cli/php.ini
/etc/init.d/apache2 restart
Failed to clone http://github.com/zendframework/Component_ZendStdlib.git, git was not found,
Install GIT
Apt-get install git
Just register the namespace in your autoloader.
if you are using composer then add include_once '<path to vendor dir>/autoload.php'; on top of your php file

Categories