Zend Framework 2 Library Paths - php

Trying to get my feet wet on ZF2 and I've stumbled on my first problem. Say on a module I want to use Shanty_Mongo (an external library to connect to MongoDb)
So I've copied the entire Shanty directory on the library and created a new Model class:
namespace Dummy\Model;
use Shanty\Mongo\Document;
class Dummy extends Shanty_Mongo_Document {
public function setConnections( $connections ) {
Shanty_Mongo::addConnections($connections);
}
}
(The setConnections() is to be used by DI, if I've understood it well)
This seems to fail to find Shanty_Mongo_Document. Should I add something to the application.config.php to point to the extra library?

The library Shanty_Mongo is an "old" underscore separated library without using namespaces. In ZF2, the style is the same PSR-0 standard but with namespaces (so Shanty_Mongo will be Shanty\Mongo). However, you are able to load these old style fine with a classmap for example. Then you can use underscore separated classes inside your ZF2 project.
I'd suggest you create a module for this library and put that module under ./vendor (for "modules providing 3rd party features"). In this module, you can create the following directory structure (I assume the name of the module is ShantyMongo):
./vendor/ShantyMongo/
library/
Module.php
autoload_classmap.php
autoload_function.php
autoload_register.php
The library is a submodule to the Shanty-Mongo git repository. The file autoload_classmap.php is a classmap created by the php script classmap_generator.php inside the bin directory of the ZF2 repository. Then the autoload_function.php can be something simple as this:
<?php
return function ($class) {
static $map;
if (!$map) {
$map = include __DIR__ . '/autoload_classmap.php';
}
if (!isset($map[$class])) {
return false;
}
return include $map[$class];
};
And autoload_register.php something like this:
<?php
spl_autoload_register(include __DIR__ . '/autoload_function.php');
To let the ZF2 application know you have this module, you need to fill the module.php with a ShantyMongo\Module class. Something like this should be sufficient:
<?php
namespace ShantyMongo;
use Zend\Module\Consumer\AutoloaderProvider;
class Module implements AutoloaderProvider
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
)
);
}
}
If you add "ShantyMongo" to your modules array in application.config.php you now have set up the autoloader for this 3rd party library inside ZF2. You can then use your model as follows:
<?php
namespace Dummy\Model;
class Dummy extends Shanty_Mongo_Document {
public function setConnections ($connections) {
Shanty_Mongo::addConnections($connections);
}
}
Because ShantyMongo doesn't use namespaces, you don't have that use statement anymore.

Related

Yii2: run console command from controller in vendor directory

I am doing some refactoring of our large work app. This involves separating out some tools I've build, like a schema/seed migration tool for the command line, in to their own repositories to be used by multiple applications.
If it's in console/controllers, it gets picked up. If I move them to their own repository and require it via Composer, how do I get Yii to know when I say php yii db/up, i mean go to the new\vendor\namespace\DbController#actionup ?
If you create an extension (and load it through composer of course), you can locate Module.php inside, which will hold path to console controllers (that you can call with your terminal).
I will write my example for common\modules\commander namespace, for vendor extension your namespace will differ, but it work for all of them the same way.
So I have the following file structure for my extension
<app>
common
modules
commander
controllers
• TestController.php
• Module.php
My Module class looks as follow:
namespace common\modules\commander;
use yii\base\Module as BaseModule;
class Module extends BaseModule
{
public $controllerNamespace = 'common\modules\commander\controllers';
public function init()
{
parent::init();
}
}
And TestController.php is inherited from yii\console\Controller:
namespace common\modules\commander\controllers;
use yii\console\Controller;
class TestController extends Controller
{
public function actionIndex()
{
echo 123;
}
}
And the main part to make everything work is to register out Module.php in console/config/main.php settings
'modules' => [
'commander' => [
'class' => \common\modules\commander\Module::className(),
],
...
],
Here it is, now you can use your command like:
yii commander/test/index
And it'll print you 123, showing that everything works and Console Controllers are located in different folders!

Codeception/AspectMock Parent class not found by locator

I have a problem with Codeception/AspectMock.
When using custom autoloader and try to create an instance of a class which has parent form the same custom namespace I have this error:
PHP Fatal error: Uncaught InvalidArgumentException: Class [parent
class name] was not found by locator in
vendor/goaop/parser-reflection/src/ReflectionEngine.php:112
I have very simple setup:
<?php
require_once __DIR__ . '/vendor/autoload.php';
$kernel = AspectMock\Kernel::getInstance();
$kernel->init([
'debug' => true,
'includePaths' => [__DIR__. '/lib'],
]);
$kernel->loadFile(__DIR__ . '/autoload.php'); // custom autoloader
$b = new \lib\B();
Class \lib\B:
namespace lib;
class B extends A {}
Class \lib\A:
namespace lib;
class A
{
public function getName()
{
return static::class;
}
}
Class B is loaded via my custom autoloader, but then the locator tries to load parent class A via composer autoloader and returns this error. Is this a bug, or I'm doing something wrong?
The topic starter has already got an answer on GitHub.
In order to use custom autoloader you should re-init ReflectionEngine with composite class locator that will be able to locate your classes or you can use CallableLocator with closure for resolving paths.
Or, even better you could switch your code base to the PSR0/PSR-4
For example:
$kernel->loadFile(__DIR__ . '/autoload.php'); // custom autoloader
\Go\ParserReflection\ReflectionEngine::init(
new class implements \Go\ParserReflection\LocatorInterface {
public function locateClass($className) {
return (new ReflectionClass($className))->getFileName();
}
}
);
$b = new \lib\B(); // here you go
If you can easily do a find and replace on your codebase, maybe you could refactor your code to PSR-4 autoloading standards and do away with the need for a custom autoloader altogether.
This is the spec https://www.php-fig.org/psr/psr-4/. I'll try and explain it as simply as possible.
Imagine changing your lowercase namespace lib to Lib, and setting that namespace to the src/ directory in your composer.json:
"autoload": {
"psr-4": {
"Lib\\": "src/"
}
}
After setting that, run composer dumpautoload. Then all you need to do is search and replace namespace lib;, replacing with namespace Lib;.
An example class located in src/Form.php would have namespace Lib; at the top, followed by class Form.
<?php
namepace Lib;
class Form
{
// code
}
Namespaces use the folder naming convention. All classes directly in src/ have namespace Lib;. If there are subdirectories, the directory name becomes part of the namespace. For example a file in src/Form/Field/Text.php would have namespace Lib\Form\Field; class Text {}.
<?php
namepace Lib\Form\Field;
class Text
{
// code
}
You can see the full convention in the link above, but the general rule is make any folders begin with a capital letter, as with your classname, and the autoloader should be able to find all of your classes.
This is probably the best practice solution for you, and again as I said, only requires a little bit of file renaming and namespace tweaking. Good luck!

Symfony2 - Use a third party library (SSRS)

Maybe dumb question, I'm new to Symfony2 and I'm using it for one of my projects.
I'd like to be able to use a third party library, namely SSRSReport (an API for SSRS Reports).
I have put the library into Symfony/vendor/ssrs/lib/Ssrs/src.
There are many classes defined here, I don't need them to be autoloaded.
I simply don't know how to require and call them from a controller.
For sure this doesn't work
require_once '/vendor/ssrs/lib/Ssrs/src/SSRSReport.php';
class DefaultController extends Controller
{
public function viewAction()
{
define("UID", "xxxxxxxx");
define("PASWD", "xxxxxxxx");
define("SERVICE_URL", "http://xxx.xxx.xxx.xxx/ReportServer/");
$report = new SSRSReport(new Credentials(UID, PASWD), SERVICE_URL);
return $this->render('myBundle:Default:view.html.twig'
, array('report' => $report)
);
}
}
SSRSReport() and Credentials() used here, are 2 of many classes contained into the API.
First of all, I don't recommend putting non-symfony-managed libraries into /vendors. Since you're managing this library, put it into /src.
Secondly, when using classes that aren't namespace (i.e., are in the root namespace), make sure you reference them properly or else PHP will look in the current namespace (which, in this case, is your controller namespace)
Thirdly, the quick-and-dirty solution is to just properly include the files from the controller:
class DefaultController extends Controller
{
protected function includeSsrsSdk()
{
require_once(
$this->container->getParameter( 'kernel.root_dir' )
. '/../src/ssrs/lib/Ssrs/src/SSRSReport.php'
);
}
public function viewAction()
{
$this->includeSsrsSdk();
define("UID", "xxxxxxxx");
define("PASWD", "xxxxxxxx");
define("SERVICE_URL", "http://xxx.xxx.xxx.xxx/ReportServer/");
$report = new \SSRSReport(new \Credentials(UID, PASWD), SERVICE_URL);
return $this->render('myBundle:Default:view.html.twig'
, array('report' => $report)
);
}
}
But that locks your logic for including the library into this one controller. You could make a separate wrapper for the SDK that does this, or even register it as a service.
You are probably using composer with symfony, so this is my suggestion.
Instead of require_once, you should use composer's autoload mechanism for autoloading non namespaced libraries or functions http://getcomposer.org/doc/04-schema.md#files
So just update the autoload section in composer.json.
"autoload": {
"psr-0": { "": "src/" },
"files": ["src/SsrsReport/SSRSReport.php"]
},
For consuming the service I would either use a Facade (extends SSRSREport class) or a Factory which returns it.

How to Trick Zend loader for multiple classes in one file

I'm trying to implement custom exception classes for a zend project.
I use modules and all my classes are under library.
so a filename DuplicateFileException.php which is under
APPLICATION_PATH "/../library/Abra/Exception" contains
class Abra_Exception_FileNotFoundExcpetion extends Exception {}
class Abra_Exception_MissingFileException extends Exception {}
class Abra_Exception_DuplicateFileException extends Exception {}
class Abra_Exception_FileIOException extends Exception {}
so the ErrorController works fine when i only throw Abra_Exception_DuplicateFileException
because there is indeed a file called DuplicateFileException, but the app breaks when i throw any other than that Exception.
I just can't believe that i have to create a file for each of them.
so how to work around it?
thanks for reading.
Proper zend-way would be implementing your own autoloader class. IT will implement autoload method. You can then use Zend_Autoloader autoloader stack to oad your classes. You just need to have some kind of system/logic in your classname->filename mapping.
class Abra_Autoloader
{
public function autoload($className)
{
if (strpos('Abra_Exception', $className) !== false) {
include $someFile; //faster
}
}
}
//anywhere in bootstrap (preferably in some autoloading section of yours)
// autoloader will load only classes starting with "abra"
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
$autoloader->pushAutoloader(array('Abra_Autoloader ', 'autoload'), 'Abra');
Please note that at least teh autoloader class should be Zend_Loader-loadable :)
The Zend autoloader works by mapping requested class names to filesystem paths so you can't make it load a file for a class where the filename does not match.
If you really want to persist down this path, I'd just include the file in your Bootstrap class, eg
protected function _initRequires()
{
require_once 'Abra/Exception/DuplicateFileException.php';
// assuming your "library" folder is on the include path
}

Will a directory with a period break autoload resolution based on a namespace in Zend Framework?

I have a folder in my library folder which is named after my website. The folder path is like:
~\www\library\myWebsite.com
If I'm using Zend autoloader to load the namespace of everything in the library path, will I have any trouble autoloading a class from that file with a namespace like this:
\myWebsite.com\myClass::myFunction();
I have looked online for documentation on this and I can't find any info about using periods in this way.
I tried it and the complication is in PHP. I think Zend is registering the namespace fine, because when I call \Zend_Load_Autoloader::getRegisteredNamespaces() it shows that it's registered. but when I call the static method from the fully qualified namespace, php gives an error of this:
Fatal error: Undefined constant 'myWebsite' in /home/jesse/www/application/controllers/MyController.php on line 15
It seems like PHP is terminating the namespace identifier, during parsing, at the . (period character). This is dissapointing because to me having a library named after the website was important to my design.
I will rename the directory to myWebsitecom or possibly make the .com it's own sub directory like
myWebsite\com and incorporate that into my namespace tree like: \MyNamespace\Com\MyClass::myFunction();
The easiest way to find out is to try it.
If it doesn't work, you could always write a custom autoloader to make it work. I don't have much experience with php namespaces, but the autoloader would look something like this (I imagine you'll have to tinker with it a bit to determine the correct file path given the class name):
<?php
class My_Loader_Autoloader_MyWebsite implements Zend_Loader_Autoloader_Interface {
/**
* (non-PHPdoc)
* #see Zend_Loader_Autoloader_Interface::autoload()
*/
public function autoload($class) {
if (strtolower(substr($class, 0, 9)) == 'mywebsite') {
$file = realpath(APPLICATION_PATH . '/../library/myWebsite.com/' . $class);
if ($file) {
require_once $file;
return $class;
}
}
return false;
}
}
then put this in your bootstrap:
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->pushAutoloader(new My_Loader_Autoloader_MyWebsite());
and if this class must be in that myWebsite.com directory, you could just cheat and throw in a require in there too:
require_once(APPLICATION_PATH . '/../library/myWebsite.com/Loader/Autoloader/MyWebsite.php');

Categories