I'm trying to use the Stripe php api in a Bolt extension but it's having trouble finding the class. I added the Stripe library to composer.json:
"require": {
"stripe/stripe-php": "1.*"
}
And ran composer install. myextension/vendor/composer/autoload_classmap.php now shows the classes loaded:
$vendorDir = dirname(dirname(__FILE__));
return array(
'Stripe' => $vendorDir . '/stripe/stripe-php/lib/Stripe/Stripe.php',
'Stripe_Account' => $vendorDir . '/stripe/stripe-php/lib/Stripe/Account.php',
'Stripe_ApiConnectionError' => $vendorDir . '/stripe/stripe-php/lib/Stripe/ApiConnectionError.php',
'Stripe_ApiError' => $vendorDir . '/stripe/stripe-php/lib/Stripe/ApiError.php',
...
And now I'm trying to use it in the extension like this:
use Stripe, Stripe_Customer, Stripe_Charge, Stripe_Plan, Stripe_Coupon, Stripe_Error;
public function initialize()
{
$stripe = new Stripe();
$stripe->setApiKey($this->config['stripe_key']);
But I get the error:
Error: Class 'Stripe' not found
File: extensions/local/andyjessop/myextension/Extension.php
I still haven't got my head round autoloading, so I think I'm doing something basic wrong, but I can't see what it is. Can anyone help?
You should include the composer autoload file.
require_once('vendor/autoload.php');
Why don't you try this command
composer require stripe/stripe-php
also make sure you are updating your composer file, this will work cheers
Related
Composer in autoload_static.php use class that I don't need them in every app request.
'd5fa61a7f6cbc1df09dd4df84549a2dc' => __DIR__ . '/..' . '/rospdf/pdf-php/src/Cpdf.php',
'2d15964294879de66053d54f6bde65d7' => __DIR__ . '/..' . '/rospdf/pdf-php/src/Cezpdf.php',
How to remove them from this autoload file? I can delete/comment them manually but every Composer update this file is re-generated.
I try to add in my main composer.json:
"exclude-from-classmap": ["vendor/rospdf/pdf-php/src/"]
& run composer dump-autoload bo those class are still in there.
You can trick the autoloader of composer and let him think those are already loaded:
<?php
// Setting global variable:
$GLOBALS["__composer_autoload_files"] = [
"d5fa61a7f6cbc1df09dd4df84549a2dc" => true,
"2d15964294879de66053d54f6bde65d7" => true,
];
require "vendor/autoload.php";
But this needs to happen before the vendor/autoload.php is included.
I'm having trouble understanding with doesn't Composer autoloads the packages I required.
My current composer.json file has the following:
{
"require": {
"atlas/orm": "#dev"
},
"require-dev": {
"atlas/cli": "#dev"
}
}
It was supposed to generate a Namespace in the /vendor/composer/autoload_namespaces.php file. But it doesn't. The file only has the following:
// autoload_namespaces.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Psr\\Log\\' => array($vendorDir . '/psr/log'),
);
Of course, when I try to use the "require DIR . '/vendor/autoload.php';" to autoload the package and then use its classes, it does not work.
Any idea on how can I solve this?
For requiring in all of the installed dependencies, you have to require 'autoload.php'. For autoloading(PSR-4), in the composer.json file, you have to give a name under which everything will be namespaced and the folder name from which files will be autoloaded.
"Namespace_name\\":"folder_name"
Note: The backslash after the namespace_name needs to be escaped, hence the extra backslash.
Then run composer dump-autoload -o
i recently updated using composer. After updating I am getting error in php file , where I am sending data to intercom. This is the error :
Fatal error: Class 'Intercom\IntercomBasicAuthClient' not found in <filename>
I found a similar problem here Symfony Exception (Class not found) only on development and production servers. But couldn't understand exactly how to solve the issue.
I tried using intercom in Uppercase as well as lowercase, but problem is not solved.
In my installed.json I found this :
"autoload": {
"psr-4": {
"Intercom\\": [
"src"
]
}
}
And this is the directory location of intercom files :
/public_html/vendor/intercom/intercom-php/src
When I dig into more composers file(was trying to understand how classes are included and all), I came across this code in autoload_namespaces.json
<?php
// autoload_namespaces.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'libphonenumber' => array($vendorDir . '/giggsey/libphonenumber-for-php/src'),
'PhpAmqpLib' => array($vendorDir . '/php-amqplib/php-amqplib'),
);
libphonenumber and phpamqplib are two libraries that I installed using composer, and intercom is missing here.
So I am totally confused, what is the actual problem.
Is the intercom library missing in autoload_namespace or is it the uppercase-lowercase issue or what.
Edit :
This I found in autoload_psr4.php
<?php
// autoload_psr4.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Psr\\Http\\Message\\' => array($vendorDir . '/psr/http-message/src'),
'JmesPath\\' => array($vendorDir . '/mtdowling/jmespath.php/src'),
'Intercom\\' => array($vendorDir . '/intercom/intercom-php/src'),
'GuzzleHttp\\Psr7\\' => array($vendorDir . '/guzzlehttp/psr7/src'),
'GuzzleHttp\\Promise\\' => array($vendorDir . '/guzzlehttp/promises/src'),
'GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'),
'Aws\\' => array($vendorDir . '/aws/aws-sdk-php/src'),
);
And this is how I am including Intercom in my php file :
use Intercom\IntercomBasicAuthClient;
I figured it out myself.
The problem was with PHP version. I am using php-5.5 and the intercom library strictly requires PHP version >= 5.6 .
Created my own library using Intercom APIs to get it work for php-5.5.
After initializing the Composer autoloader, I want to check the existence of a class:
require_once('vendor/autoload.php');
var_dump(class_exists('PagesController'));
This gives me boolean false, as if my class doesn't exist. However, it does, and is mentioned in the classmap autoloading.
When I add a simple change in the vendor/autoload.php, like adding a var_dump("ballon"), the original check for the PagesController changes to boolean true.
My composer.json file looks like this:
{
"require": {
"propel/propel": "~2.0#dev"
},
"autoload": {
"classmap": ["controllers/", "views/", "views/helpers/", "controllers/components/", "models/", "generated-reversed-database/generated-classes/"]
}
}
I've ran php composer.phar install to generate the autoloader.
require_once('vendor/autoload.php') includes the composer-generated file and is the very first line of code to be executed (it's in my index.php). The PagesController class is located in controllers/.
I'm running php 5.5.9 with Apache2 on an Ubuntu server.
It doesn't even matter whether its a var_dump, a comment (//), or simply a whitespace () that I add in vendor/autoload.php, as long as I add something.
My autoload_classmap.php file:
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
(...lots of classes...),
'PagesController' => $baseDir . '/controllers/PagesController.php'
);
So the class is found and listed.
I am sorry if this has once been asked but I lack understanding on how this should work.
I am building a File Server to manage uploading and downloading of music files and have found getID3 library that I can use to get information about the files being uploaded.
I have installed the library via composer. Below is my entry on composer.json in my root director
"james-heinrich/getid3": "dev-master"
Vendor folder was successfully updated with the library.
The library has not been added onto autoload_namespaces.php file in vendor/composer (see below):
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Symfony\\Component\\Filesystem\\' => array($vendorDir . '/symfony/filesystem'),
'Psr\\Log\\' => array($vendorDir . '/psr/log'),
'GetId3_' => array($vendorDir . '/phansys/getid3'),
);
Also under autoload_psr4.php file in vendor/composer the namespace for the library has not been registered either. I am not sure if this would be the case. Below is the content of the file:
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'),
'FileServer\\' => array($baseDir . '/src'),
);
Question(s)
How can I access the class libraries from getID3? Is there any special operation that I need to perform to make these classes available to me?
NB: The demos folder of the library does not use namespaces but rather "require" to include class files and that in my understanding forfeits the point of composer and namespaces. Resorting to Phansys Symdony library didnt help at all as I was not even sure if the library can be used on standalone projects.
Thanking you in advance.
Command composer dump-autoload is the answer thanks to #Frank.