Symfony2: class MapClassLoader not found - symfony

I'm trying to use MapClassLoader in autoload.php but for some reason I keep getting errors saying
Class 'Symfony\Component\ClassLoader\MapClassLoader' not found in ...\autoload.php
autoload.php:
<?php
use Symfony\Component\ClassLoader\UniversalClassLoader;
use Symfony\Component\ClassLoader\MapClassLoader;
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
//some values
));
$mapLoader = new MapClassLoader(array(
//some values
));
$mapLoader->register();
I double checked and MapClassLoader.php does exist in Symfony\Component\ClassLoader
Any idea why is it happening? :/

autoload.php is a file that configures autoloading for classes so autoloading isn't available in it and you need to include any files manually:
require_once __DIR__.'/../vendor/symfony/src/Symfony/ClassLoader/MapClassLoader.php';
Why is UniversalClassLoader available without require? Because symfony uses bootstrap file for system files to reduce file loading overhead.

Related

ClassNotFoundException - how do I register a custom namespace in symfony (2.4.4)

I have a new bundle MyBundle under
src/MyVendor/Bundle/MyBundle/
and in the default controller I wish to use class MyClass which resides in namespace called MyVendor\MyComponent
which is located under src/MyVendor/Component/MyComponent/src/MyVendor/MyComponent/MyClass.php
I've tried to do
use Symfony\Component\ClassLoader\UniversalClassLoader;
$myLoader = new UniversalClassLoader();
$myLoader->registerNamespace(
'MyVendor\\MyComponent\\',
__DIR__.'/../src/MyVendor/Component/MyComponent/src'
);
in app/autoload.php but I'm still getting the ClassNotFoundException error.
Either I'm missing something subtle, or my idea is completely wrong (wrong psr-0 dir structure) or should this be done solely via composer's autoload e.g.
"autoload": {
"psr-0": {
"MyVendor\\MyComponent\\":
"src/MyVendor/Component/MyComponent/src"
}
}
and
composer dump-autoload --optimize
In either case, I'd appreciate any help.
thank you
OK, I think I've figured it out:
registerNamespace should not have trailing \\ in the namespace
I need to add $myLoad->register();
so the whole thing looks like
use Symfony\Component\ClassLoader\UniversalClassLoader;
$myLoader = new UniversalClassLoader();
$myLoader->registerNamespace(
'MyVendor\\MyComponent',
__DIR__.'/../src/MyVendor/Component/MyComponent/src'
);
$myLoader->register();
I'd still like to see how to do this with the composer and dump-autoload

How to use symfony2 twig extension without symfony2

I want to use some of the builtin symfony2 extensions(e.g:humanize,yaml_dump) for twig for a website not developed in symfony but using a twig engine.how can I do that?
The symfony/twig-bridge package provides the symfony-specific twig extensions.
These include i.e. the YamlExtension that provides the yaml_dump filter and the FormExtension that provides the humanize filter.
The extensions can be found in the Extension folder.
I strongly advise you to install the package via composer to get the package's dependencies automatically.
composer require symfony/twig-bridge:~2.3
Further composer will automatically register the classes in the autoloader (vendor/autoload.php) for you.
Now you just need to add the extensions to twig as described in the documentation.
$twig->addExtension(new \Symfony\Bridge\Twig\Extension\YamlExtension());
// ...
A complete example, with an extension class and a quick extension (a new filter) :
<?php
require_once("vendor/autoload.php");
$loader = new Twig_Loader_String();
$twig = new Twig_Environment($loader);
// here we add the extension class (taken from #nifr answer)
$twig->addExtension(new \Symfony\Bridge\Twig\Extension\YamlExtension());
// here we add a new filter quickly
$filter = new Twig_SimpleFilter('paragraph', function ($argument) {
return "<p>{$argument}</p>";
}, array('pre_escape' => 'html', 'is_safe' => array('html')));
$twig->addFilter($filter);
// demo
echo $twig->render('{{ "hello" | paragraph }}');

How to create a standalone command line app with the Symfony2 ClassLoader and Console

I am trying to write a command line app using the Symfony2 Console and ClassLoader Components.
This is a screenshot of my code hierarchy and the script being called
Here is the CLI script:
#!/usr/bin/env php
<?php
require_once(dirname(__FILE__) . '/code/ClassLoader/UniversalClassLoader.php');
use Symfony\Component\ClassLoader\UniversalClassLoader;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use BlueHeadStudios\Command\Deployer;
$loader = new UniversalClassLoader();
$loader->register();
$input = new ArgvInput();
$debug = $input->hasParameterOption(array('--debug', ''));
$console = new Application();
$console->add(new Deployer());
$console->run();
I get this when running the script
dev#server:~/sites/pd/deployer$ php deployer.php
PHP Fatal error: Class 'Symfony\Component\Console\Input\ArgvInput' not found in /home/dev/sites/pd/deployer/deployer.php on line 13
I know it must be a simple registerNamespace call or something similar, but I've tried multiple registrations but cannot get it to work.
Any help is greatly appreciated.
I would recommend you to use composer, which will generate autoload.php for you to include at the top of the file:
#!/usr/bin/env php
<?php
require_once './vendor/autoload.php';
use Symfony\Component\ClassLoader\UniversalClassLoader;
$loader = new UniversalClassLoader();
$loader->registerNamespace('BlueHeadStudios', __DIR__.'/src/');
$loader->register();
// write your code below
You should run the application with app/console check the example here

Symfony2 class loader doesnt work if path does not match folder structure?

Here is my current structure
plugins/
|---init.php
|---/plugin1/lib/
|---/plugin2/lib/
|---/Symfony/
I have my code like this:
set_include_path(DIR_FS_CATALOG.'plugins');
require_once(DIR_FS_CATALOG.'plugins/Symfony/Component/ClassLoader/UniversalClassLoader.php');
// load the class loader and dependency injection component
$loader = new Symfony\Component\ClassLoader\UniversalClassLoader();
$loader->registerNamespaces(array('plugins\\plugin1' => __DIR__.'/plugins/plugin1/lib', 'plugins' => DIR_FS_CATALOG.'plugins'));
$loader->registerNamespace('Symfony',__DIR__.'/plugins');
$loader->register();
use plugins\plugin1\MyClass;
MyClass::init();
Fatal error: Class 'plugins\plugin1\MyClass' not found
I wonder what did I do wrong? Any help would be much appreciated.
The Symfony2 class loader is PSR-0 compliant (https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md) which means, that your namespaces must reflect your file system structure.

How to use PHPExcel correctly with Symfony 2

I need to use PHPExcel with a Symfony2 project. Anyone know how to set up the project correctly to use the library? Should i put it in the vendor directory? What should be changed in the configuration files etc?
Actually, to do it right you need to follow next steps:
Edit your deps file and add dependency from the PHPExcel
[PHPExcel]
git=http://github.com/PHPOffice/PHPExcel.git
target=/phpexcel
version=origin/master
Run php bin/vendors install in order to install all missing dependencies (PHPExcel in our case)
Update prefixes section in app/autoload.php:
$loader->registerPrefixes(array(
// ...
'PHPExcel' => __DIR__.'/../vendor/phpexcel/Classes',
));
Done. Now, you can use it in your bundle's controller (code based on PHPExcel example from Tests/01simple-download-xls.php):
<?php
namespace Demo\MyBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use PHPExcel;
use PHPExcel_IOFactory;
class DemoController extends Controller
{
public function demoAction()
{
$response = new Response();
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Me")
->setLastModifiedBy("Someone")
->setTitle("My first demo")
->setSubject("Demo Document");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Set active sheet index to the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
$response->headers->set('Content-Type', 'application/vnd.ms-excel');
$response->headers->set('Content-Disposition', 'attachment;filename="demo.xls"');
$response->headers->set('Cache-Control', 'max-age=0');
$response->prepare();
$response->sendHeaders();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit();
}
}
Copy the library to your vendors directory.
Configure autoloader in your bootstrap file:
$loader->registerPrefixes(array(
// Swift, Twig etc.
'PHPExcel' => __DIR__ . '/../vendor/phpexcel/lib/PHPExcel'
));
That's all.
actually the best solution is to use https://github.com/liuggio/ExcelBundle.
I tried to use #Crozin's solution but I was still getting an error about IOFactory::createWriter.
Hope this helps,
Simone
As of Symfony 2.3, you can now do this:
...
"require": {
...
"phpoffice/phpexcel": "dev-master"
...
},
...
Then just run composer update and dependencies will resolve automatically.
Or you can do composer require phpoffice/phpexcel:dev-master if you don't want to mess with the composer.json file.
If you are using composer to manage your project, you can just change the composer.json file:
"autoload": {
"psr-4": {
"": "src/",
"": "vendor/phpoffice/phpexcel/Classes/"
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
Then add
use PHPExcel;
use PHPExcel_IOFactory;
to your controller file, and you can use the PHPExcel like this:
$objPHPExcel = new PHPExcel();
Hope it helps.
With composer (since Symfony2.1) it's really easy, you only have to modify the composer.json.
You don't need to register the namespace anymore!
Only two things, to notice:
refer to github tags, I only found a soltion with the package type
when changing something in the composer.json related to the class autoloading stuff, you have to remove the whole directory in the vendor dir
Here is the related link: use PHPExcel with composer and Symfony2.2

Resources