doctrine odm annotations or composer autoload.php not working? - php

I'm trying to use Doctrine MongoDB ODM 2.0 beta on a project with the Yii2 framework, with composer version 1.8.4 and PHP 7.2, but I keep getting the error Fatal error: Uncaught Error: Call to a member function add() on boolean where the code runs $loader->add('Documents', __DIR__);
bootstrap.php file (in DIR/bootstrap.php):
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
if ( ! file_exists($file = 'C:/path/to/vendor/autoload.php')) {
throw new RuntimeException('Install dependencies to run this script.');
}
$loader = require_once $file;
$loader->add('Documents', __DIR__);
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
$config = new Configuration();
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
$config->setHydratorDir(__DIR__ . '/Hydrators');
$config->setHydratorNamespace('Hydrators');
$config->setDefaultDB('fsa');
$config->setMetadataDriverImpl(AnnotationDriver::create(__DIR__ . '/Documents'));
$dm = DocumentManager::create(null, $config);
I already tried looking at How to properly Autoload Doctrine ODM annotations? and Laravel & Couchdb-ODM - The annotation "#Doctrine\ODM\CouchDB\Mapping\Annotations\Document" does not exist, or could not be auto-loaded and a host of other threads I can't quite recall for help, but I couldn't figure out a solution.
I also tried commenting out the lines below
if ( ! file_exists($file = 'C:/path/to/vendor/autoload.php')) {
throw new RuntimeException('Install dependencies to run this script.');
}
$loader = require_once $file;
$loader->add('Documents', __DIR__);
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
and ran composer dump-autoload and on command line it returned Generated autoload files containing 544 classes, but then I got the problem
[Semantical Error] The annotation "#Doctrine\ODM\MongoDB\Mapping\Annotations\Document" in class Documents\Message does not exist, or could not be auto-loaded.
So the annotations are not auto-loading, and I have no idea how to fix that.
In the model I have:
<?php
namespace Documents;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use \Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
/** #ODM\Document */
class Message
{
/** #ODM\Id */
private $id;
/** #ODM\Field(type="int") */
private $sender_id;
...
I also posted a thread on github at https://github.com/doctrine/mongodb-odm/issues/1976. One commenter stated that "By default, the composer autoload file returns the autoloader in question, which seems to not be the case for you." How can I fix that? The only information I can find online is to put (inside composer.json) the lines:
"autoload": {
"psr-4": {
"Class\\": "src/"
}
},
but then what class should I be loading?
I'm very confused and being pretty new to all these tools (mongodb, yii2, etc.) doesn't help at all. I'm not sure what other information would be helpful else I would post it.
Thanks in advance.

So turns out that the problem (as was mentioned in https://github.com/doctrine/mongodb-odm/issues/1976) was that autoload.php was required twice - once in bootstrap.php and once in web/index.php (of the framework). After the require line in index.php was removed, everything worked fine.

Related

Composer autoload custom classes

I try to autoload my custom pdo class with composer.
Ran the following command to update autoload:
compser update
composer install
Both seem to work, no error prompted. But,
vendor/composer/autoload_namespaces.php
Does not list the custom namespace added to composer.js.
File structure
-Root
->classes
->pdo
->class.php
->vendor
->various extensions loaded with composer
index.php
PHP Class
namespace Classes\Pdo;
Class DB {
//Do some stuff...
}
Composer.js
"autoload": {
"psr-4": {
"Classes\\Pdo\\": "classes/pdo"
}
}
Index.php
$pdo = new \Classes\Pdo\DB(); //Fatal error: Class 'Classes\Pdo\DB' not found
Old question, but I just ran across this myself.
For future Googlers, in my case the issue turned out to be the name of the class file did not exactly match the class name.
See this post: Why does 'composer dumpautoload -o' fix 'Class not found' PHP error?

"Convert" package to load in Symfony

I have THIS package (a payment gateway), which I would like to use in Symfony 3.0.1
Unfortunately I get this error:
ClassNotFoundException in AppKernel.php line 21: Attempted to load class "SofortBundle" from namespace "Sofort\SofortLib".
Did you forget a "use" statement for another namespace?
In the sofort\sofortlib-php folder i created the file SofortBundle.php with this content
<?php
namespace Sofort\SofortLib;
use Symfony\Component\HttpKernel\Bundle\Bundle as BaseBundle;
class SofortBundle extends BaseBundle
{
}
and I loaded the Bundle in AppKernel.php:
new Sofort\SofortLib\SofortBundle(),
But that only leads to above exception.
What am I missing?
Don't copy packages to your custom folder. Install package as described:
In composer.json add:
"require": {
"sofort/sofortlib-php": "3.*"
}
Run composer update sofort/sofortlib-php
In your code you can use the library like this:
use \Sofort\SofortLib\Billcode;
class MyClass
{
function doSomething($configkey) {
$SofortLibBillcode = new Billcode($configkey);
...
}
}

Composer and PSR-0 class autoloading with namespaces

Hello guys i have problem with autoloading my class with composer. On Linux all work perfect, but now my boss change env and set Windows. All this work on linux but windows show newbie fatal error:
Fatal error: Class 'AbstractController' not found in
D:\xampp\htdocs\ikacFw\frontController.php on line 7
Common to see my composer.json and stucture for better picture on problem.
Stucture is :
frontController.php
-- vendor
----- Doctrine
----- Ikac
--------- Components
---------- Mvc
------------- Controller
Am trying to load all data from vendor directory.
Composer.json
{
"autoload": {
"psr-0": {
"vendor": ""
}
}
}
Also new component i add manual. Like this :
$loader = require_once 'vendor/autoload.php';
$loader->add('vendor', "Ikac");
Okay next when i try to call :
<?php
require_once 'vendor/autoload.php';
use Ikac\Mvc\Controller;
$a = new AbstractController();
I get error "not found".
My class AbstractController contain defined namespace but dont work again. Like test i do this:
<?php
//vendor/Ikac/Mvc/Controller/AbstractController.php
namespace Ikac\Mvc\Controller;
class AbstractController {
function __construct() {
echo __CLASS__;
}
}
?>
I do from cmd composer dump-autoload, install, but dont work. All this perfect work on linux but here wont. Any idea how to fix this or where i do mistake.
Thanks guys!
SLOVED:
{
"autoload": {
"psr-0": {
"": "vendor/"
}
}
}
Well you should do
<?php
require_once 'vendor/autoload.php';
use Ikac\Mvc\Controller\AbstractController;
$a = new AbstractController();
Your autoloading declaration is wrong.
You will NEVER ever need to include the vendor folder in any autoloading. The vendor folder will contain the autoloading for both all dependencies, and - if configured - for your own classes as well.
You can use Composer to create autoloading for your own classes. Just include the correct info. But from your current info I cannot deduct what would be correct.

Symfony2: class included in AppKernel can not be found on webserver

I added to my project FOSUserBundle, on localhost it's works fine. But on web server I get
Fatal error: Class 'FOS\UserBundle\FOSUserBundle' not found in
/home/zone24/domains/zone24.linuxpl.info/public_html/worldclock/app/AppKernel.php on line 22
I can't cache:clear because I get this same message.
My autoload.php
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = require __DIR__.'/../vendor/autoload.php';
// intl
if (!function_exists('intl_get_error_code')) {
require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';
$loader->add('', __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs');
}
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
return $loader;
The line from AppKernel.php who make mistake
new FOS\UserBundle\FOSUserBundle(),
Folderfriendsofsymfony in /vendor has 775 permisions
Are you using APC ? If yes, restart apache to clear its cache.
If that does not help, you can always force the autoloader to register a specific namespace with $loader->add(), but you should not have to do that. FOS works fine for me without adding that.

Loading namespaced classes with Symfony 1.4's autoloader?

How to register namespaces (with PHP 5.3) in the Symfony 1.4 for the autoloader class feature (like the Symfony 2.0)?
You can use Autoloader from Symfony2 in Symfony 1.4 framework.
1. Copy Symfony2 classloaders to vendor directory of your Symfony 1.4 sandbox project:
SF_ROOT_DIR/lib/vendor/Symfony2/src/Symfony/Component/ClassLoader/UniversalClassLoader.php
SF_ROOT_DIR/lib/vendor/Symfony2/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php
2. Modify your SF_ROOT_DIR/config/ProjectConfiguration.class.php file as follows:
require_once dirname(__FILE__) . '/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';
require_once dirname(__FILE__) . '/../lib/autoload/sfClassLoader.class.php';
sfCoreAutoload::register();
class ProjectConfiguration extends sfProjectConfiguration {
public function setup() {
$this->namespacesClassLoader();
$this->enablePlugins('sfDoctrinePlugin');
}
public function namespacesClassLoader() {
if (extension_loaded('apc')) {
$loader = new ApcUniversalClassLoader('S2A');
} else {
$loader = new UniversalClassLoader();
}
$loader->registerNamespaces(array(
'Pohon' => __DIR__ . '/../lib/vendor/Pohon/src'));
$loader->register();
}
}
3. Register desired namespaces:
eg. I want to load class:
Pohon\Tools\String\Utils\Slugify.
Filename must be:
SF_ROOT_DIR/lib/vendor/Pohon/src/Pohon/Tools/String/Utils/Slugify.php
and registered namespace as follows:
Pohon => SF_ROOT_DIR/lib/vendor/Pohon/src
You can use Composer and it's very easy. Just install it on your machine (you probably have already since it's 2015 now) and run in your project folder:
composer init
You can then install all the packages you want with composer and include just this line in your ProjectConfiguration.class.php:
require_once __DIR__.'/../vendor/autoload.php';
Note that paths may differ if you changed the default Symfony1.4 directory structure.
Symfony uses the spl_autoload_register() function to register its own autoloader (sfAutoload).
You can register your own handler in the initialize() function of your Project/Application/Plugin. (whichever applies).
This is, for example, also what the Swift_Mailer plugin does: it registers its own autoloader when needed.

Categories