php namespace clashes with dompdf and AWS? - php

We have existing stuff in our code base that uses dompdf (version 0.5.1)... which has no namespace.
When I include the the aws.phar, using something like
require '/path/to/aws.phar';
all of sudden, I get fatal errors
Class 'DOMPDF' not found in XXXX on line 1193
The code that is causing the error is simply a new instantiating of the class:
$dompdf = new \DOMPDF();
I am guessing this is a namespace problem, but I have no idea how to go about fixing it, since I already put the global escape in front of DOMPDF... If I comment out the require line for the phar, then everything goes back to working.
I also did open up the AWS.phar to see what is going on inside. From the code that is not obfuscated, I see that they are using the symfony class loder.
$classLoader = new Symfony\Component\ClassLoader\UniversalClassLoader();
$classLoader->registerNamespaces(array(
'Aws' => 'phar://aws.phar/src',
'Guzzle' => 'phar://aws.phar/vendor/guzzle/guzzle/src',
'Symfony\\Component\\EventDispatcher' => 'phar://aws.phar/vendor/symfony/event-dispatcher',
'Doctrine\\Common' => 'phar://aws.phar/vendor/doctrine/common/lib',
'Psr\\Log' => 'phar://aws.phar/vendor/psr/log',
'Monolog' => 'phar://aws.phar/vendor/monolog/monolog/src'
));
I tried a couple of different things also to register the dompdf with the class loader, but they all threw errors. I am guessing bc it if written without namespaces.
I have also tried loading the dompdf include config file before and after loading the phar, it seem to make no difference.
Anyone have any suggestions??

v0.5.1 of dompdf did not yet implement SPL autoloading, it registered __autoload(). This outdated method of autoloading didn't work very well because it was difficult to register more than one autoload function at a time. Plus, when using writing PHP that does use SPL autoloading the v0.5.1 autoloader will not be called. PHP SPL autoloading disables calling __autoload() (spl_autoload_register).
v0.6.0 (just released) uses SPL autoloading and should be compatible with AWS.phar. v0.6.0 is (for the most part) a drop-in replacement for v0.5.1 so you might want to try upgrading your copy of dompdf.
If that's not possible for whatever reason you could register the dompdf autoloader manually using SPL after including dompdf_config.inc.php. At a minimum you could try the following:
<?php
require_once('dompdf/dompdf_config.inc.php');
spl_autoload_register('DOMDPF_autoload');
// ...
?>
See the dompdf v0.6.0 autoload include for a more complete example.

Related

HybridAuth 3.0 - fresh install - getting: Class 'Hybridauth\Hybridauth\Provider\Facebook' not found

all. I've installed HybridAuth 3.0 and went step-by-step through the Install instructions using composer. I've used the example code and am able to get an initial HybridAuth class instantiated. However, when I continue on to the example with using the Facebook provider class, I'm getting the error, "Class 'Hybridauth\Hybridauth\Provider\Facebook' not found". I'm also using this in a Drupal 7 environment but have other classes like this that autoload just fine so I'm hopeful that little extra detail has no bearing on the problem.
I tried this both with the composer autoloader and the basic autoloader included with the library (as described in the Installation instructions). They both have the same error when attempting to find the Facebook class which is down in the /src/Provider directory. It does this for other classes as well.
That said, using the Hybridauth\Hybridauth "unified interface" works fine and is my current workaround as it allows one to work with multiple providers at one time. But I'm wondering what I'm doing wrong to not be able to load a specific provider as shown in their Introduction documentation.
This works:
// Include Composer's autoloader
include 'vendor/autoload.php';
// Import Hybridauth's namespace
use Hybridauth\Hybridauth;
// Now we may proceed and instantiate Hybridauth's classes
$instance = new Hybridauth([ /* ... */ ]);
This gives an error:
// Include Composer's autoloader
include 'vendor/autoload.php';
// Import Hybridauth's namespace
use Hybridauth\Hybridauth;
$adapter = new Hybridauth\Provider\Facebook([ /* ... */ ]);

PHPUnit does not find a class

I am working on a new tokenizer class for PHP_CodeSniffer. This also requires making a new Standard since the valid tokenizers are specified within each standard.
My new classes are using the same namespaces as the “sibling” classes from the original codebase.
If I use composer to install my project, and run phpcs, the auto loader seems to pick up my new classes just fine. PHP_CodeSniffer will load my tokenizer by calling:
$className = ‘PHP_CodeSniffer\\Tokenizers\\’ . $type;
$class = new $className(<stuff>);
Similarly, my custom standard has a use statement for a class in the original codebase. When running phpcs, no error are thrown.
When I try to run a test file through phpcs in a unit test, I get an error that my custom sniff file is unable to find the class in the use statement. My guess is this is auto-loading related. The bootstrap.php file for phpunit requires __DIR__ . ‘/../vendor/autoload.php’.My project composer.json has:
“autoload”: {
“psr-4”: { “PHP_CodeSniffer\\”: “src\” }
},
“autoload-dev”: {
“psr-4”: { “PHP_CodeSniffer\\Tests\\”: “tests\” }
}
Is there an issue with using the same namespace as the original code? With registering the same namespace to a different location in composer? Why does it work in runtime but not in testing?
It turns out PHPUnit has its own custom bootloader. When I added a require_once() for that in my PHPUnit bootloader, it loaded successfully. Thanks to a user on the PHP subreddit.

Symfony console component, class not found

I'm using the console component without Symfony standard edition and I'm working on my application.php file. When I run it it says:
Fatal error: Class 'GenerateTableCommand' not found in D:\ConnectCommand\vendor\application.php on line 10
My code:
<?php
require __DIR__.'\autoload.php';
use Symfony\Component\Console\Application;
$application = new Application();
$application->add(new GenerateTableCommand());
$application->run();
?>
My repository can be found here if needed: https://github.com/guuse/ConnectCommand
Any help is greatly appreciated.
First of all there's no \GenerateTableCommand, as stated in the error message.
This class is under AppBundle\Command namespace, so it's full name is AppBundle\Command\GenerateTableCommand.
You should add use statement at the beggining:
use AppBundle\Command\GenerateTableCommand;
Anyway, you'll probably encounter further issues with loading this class, since you do not really have any autoloaders for your custom code, so PHP won't be able to load this class.
Also mixing 3rd party code with your own (in vendor directory) is not a good idea.

Class 'Zend_Search_Lucene' not found

A big beginner of Zend-framework on PHP calls, I could include it on Netbeans IDE. Now I'm trying to use it to achieve a Lucene indexer and searcher using Zend_Lucene, I followed the getting started of the official site, unfortunately they explain the whole thing with just few words. Anyway, I copied pasted this $index = Zend_Search_Lucene::create($indexPath);, but I got a message onto this line saying: Fatal error: Class 'Zend_Search_Lucene' not found in C:\wamp\www\witswork\luceneTry.php
means that the function still unknown, maybe, some files need to be copied on my project folder but really I'm running out of ideas right now.
Accept my regards,
dany90.
You need to load the php file which contains the Zend_Search_Lucene class first. One option is to load your/path/to/library/Zend/Search/Lucene.php:
require_once 'my/path/to/library/Zend/Search/Lucene.php';
$index = new Zend_Search_Lucene::create($indexPath);
This class loads all its dependencies, so you don't need to worry about that.
Another option is to use the autoloader of Zend, Zend_Loader_Autoloader. This class is a singleton and registers itself with spl_autoload() when you retrieve it for the first time:
$autoloader = Zend_Loader_Autoloader::getInstance();
$index = new Zend_Search_Lucene::create($indexPath);
After the autoloader is loaded, you just can use Zend_Search_Lucene without the require_once() call. In the manual of Zend Framework you can find more information about the autoloader.

ez components and AWS PHP SDK makes ez components freak out

I try to work with ez Components and AWS PHP SDK at the same time.
I have a file called resize.php which is just handling resizing images using the ez Components ImageTransition tools.
I queue the image for resize in Amazon AWS SQS. If I load the AWS PHP SDK and ez Components in the same file, PHP always complains about not finding the ez Components classes.
Code looks something like this:
amazonSQS.php:
require 'modules/resize.php';
require 'modules/aws/sdk.class.php';
$sqs = new AmazonSQS();
$response = $sqs->send_message($queue_url, $message);
resize.php:
function resize_image($filename) {
$settings = new ezcImageConverterSettings(
array(
//new ezcImageHandlerSettings( 'GD', 'ezcImageGdHandler' ),
new ezcImageHandlerSettings( 'ImageMagick', 'ezcImageImagemagickHandler' ),
)
);
Error message:
Fatal error: Class 'ezcImageConverterSettings' not found in /home/www.com/public_html/modules/resize.php on line 10
If I call resize.php from another PHP file which has AWS not included, it works fine.
I load ezComponents like this:
require 'ezc/Base/ezc_bootstrap.php';
It is installed as a PEAR package.
Any idea someone?
The PHP classes of Apache Zeta / eZ Components can be conveniently used from within your PHP script. You don't have to use any require or include statements for any of the Apache Zeta Components classes that you use, this is because of the integrated autoload mechanism which can locate the classes for you when you instantiate or use them otherwise.
There are three different ways of getting the autoload mechanism going:
Normal autoload, if you require a custom autoload function
Bootstrap file, if you only use Apache Zeta autoloading
SPL autoload, if you need to register several autoload functions
These 3 ways are fully documented here

Categories