Yii Autoloading Custom Components - php

I'm trying to get Yii to autoload a component that doesn't follow Yii conventions. The library in question is Stripe which I moved into the Components folder. I was able to get it to autoload the main Stripe file no problem by modifying my config like this:
'import'=>array(
'application.models.*',
'application.components.*',
'application.components.stripe.*',
),
The directory structure looks like:
Components
----------
|
----> Stripe
|
----> Stripe.php (Class Name = "Stripe")
----> Customer.php (Class Name = "Stripe_Customer")
----> Charge.php (Class Name = "Stripe_Charge")
----> etc.
It has no problem recognizing the Stripe class, but can't find the Stripe_* named classes. Is there a way to get Yii to recognize this pattern or am I stuck with manual require statements in my controller? Would it work if I renamed all the files to be what their class name is?

Yes, renaming the file to the class name would help. Otherwise you have to require the classes by yourself.

You could also create your own autoloader and register it with: Yii::registerAutoloader(array("AutoloderClass", "methodName"), $append);.
First parameter is actually a php callback, i used class and method notation, can be function as well. This actually calls spl_autoload_register in order depending on second param - before or after Yii autoloader. Autoloader should be registered in index.php just before $app->run().
Some sample autoloader (for zend framework) can be found here:
http://www.yiiframework.com/extension/zendautoloader

Related

Where can I put my global code in Kohana 3.3?

Say I want a constant, a function or a class to be available in all my models, views and controllers code (within a particular web site (application)). Where can I put them? I don't mind importing them explicitly in every file I need them in but I don't know how (simple require_once does not seem to work).
You can put them in the vendor folder (in application/vendor or modules/MOD/vendor). Then you can load it like this:
require Kohana::find_file('vendor', 'folder/file','ext');
You can read up more on this in the user guide
Now, it should be stated you should in general not use functions or globals.
I declare Constants in Bootstrap.php and create my own Helpers for general functions under application/classes/Helpers.
If you need to integrate a third party library into Kohana or want to make code available to other Kohana users consider creating a module instead.
You can define all your constants in new php file and place it in the application/classes directory. In your Template controller or in the main controller like Welcome or Website, just place the code in the __constructor()
require_once Kohana::find_file( 'classes', 'filename' );
I suggest you to put your constant variables in the bootstrap.php file because this is loaded by the framework before every request.
You can simply put your classes in the root of the classes directory, the framework will find.
This worked well for me...
In application/config/constants.php:
define('SOME_COOL_CONSTANT', 'foo');
return array();
In index.php:
Kohana::$config->load('constants');
SOME_COOL_CONSTANT should then be available globally.

Storing PHP OPP classes

I have a site which uses the library file lib.client.php which is stored in the php folder in my standard website root and contains a series of classes I have built.
The library file contains about 5 or so classes, should I leave this file as one or break up the classes into their own files and include them all individually? Are there any best practice naming conventions I should use for these file(s)?
(As you can see at the moment I'm using lib. and I also use inc. - only because I have seen it done a few times before).
UPDATE:
I am remodelling my structure to comply with the PSR-0 Standard. I now have:
CCall (Vendor)
Core
Connection
Gateway.php
GatewayDSN.php
GatewayException.php
Components
Environment.php
EnvironmentRequest.php
Centre.php
Access
User.php
UserSession.php
RenderException.php
I want to create a new Environment() in index.php, and its __construct method calls Gateway::checkInstance().
How would I manage namespace use in this model? What would have its own namespace and where would I define this?
How would I use an autoload with these namespace definitions (and where?)
Is there an equivalent standard for method and property naming?
I am using this https://gist.github.com/jwage/221634/download#
Break classes into their own files and follow the PSR-0 standard as a best practice.
http://phpmaster.com/autoloading-and-the-psr-0-standard/
If you are using a PSR-0 autoload:
add this in Environment.php
namespace Components;
and add a reference to Gateway
use Core\Connection\Gateway;
of course you need this line inside Gateway.php
namespace Core\Connection;
Then:
new Components\Environment();

Where to put auth adapter class file in Zend 1.12

Because I am working with legacy tables, I wrote my own Auth_Adapter, named User_Auth_Adapter_DbTable. I have a module user and I want to have this class there.
However, I have no idea where to actually put the file and how to name it so the Zend autoloader can find it. I have googled for more than an hour and did not find a hint, so I put it under /application/modules/user/controller/Auth/Adapter/DbTable.php, because it is used by the controller there. But it is not found.
Can you share some code from adapter? I think logically it must be in models folder. And the name of this class should follow Zend Framework naming conventions. So if you will put it to models/user/auth/adapter/DbTable.php class should be named as Model_User_Auth_Adapter_DbTable
I think you need to define service folder.
Define folder named service path /application/modules/user/service/ (Recomended Zend-project structure), put there your adapter DbTable.php (class name must bee Application_Modules_User_Service_DbTable). You can call this class:
$adapter = new Application_Modules_User_Service_DbTable();

Symfony class loader - usage? no examples of actual usage

I've been playing a bit with the Symfony class loader (read about others, the concept and started implementing).
I've read http://symfony.com/doc/current/components/class_loader.html as well
I've changed the directories structure to fit and all. Here's a small source-code which fails:
Filename: test.php , dir: C:/test/BE/src/main.php
<?php
define('BASE_DIR','/../..');
echo BASE_DIR.'/BE/libs/symphony/component/ClassLoader/2.1.0/UniversalClassLoader.php';
require_once BASE_DIR.'/BE/libs/symphony/component/ClassLoader/2.1.0/UniversalClassLoader.php';
use Symfony\Component\ClassLoader\UniversalClassLoader;
$loader = new UniversalClassLoader();
$loader->registerNamespace('App\Location\Location', 'Location/Location');
// You can search the include_path as a last resort.
$loader->useIncludePath(true);
$loader->register();
use App\Location\Location;
new App\Location\Location(); //Fatal error: Class 'App\Location\Location' not found in C:/test/BE/src/main.php
file name: Location.php , dir: C:/test/BE/src/Location/Location.php
namespace App\Location;
class Location{
private $lat;
private $lng;
}
By registering your namespace as follows:
$loader->registerNamespace('App\Location\Location', 'Location/Location');
autoloader will look for the App\Location\Location class in the Location/Location/App/Location/Location.php file. Note that file/directory names are case sensitive.
First parameter of registerNamespace() is either a namespace or part of the namespace. Second parameter is a path to a PSR-0 compliant library. That means that directory structure inside that path should follow PSR-0 standard. This path is not used when calculating path for given namespace.
If you want to keep your current directory structure following code should work for you:
$loader->registerNamespace('App', __DIR__);
I'd rather put your source code in one common directory, for example src:
src/
App/
Location/
Location.php
And then register namespaces as follows:
$loader->registerNamespace('App', 'src');
By doing that you're simply telling an autoloader that it should look for App namespaced classes in the src folder. Note that it will work for every class in the App namespace (following PSR-0).
Some time ago I wrote a blog post about the Symfony autoloader. You might find it helpful: Autoloading classes in an any PHP project with Symfony2 ClassLoader component
Since you plan to use Pimple and some of the Symfony components take a look at Silex microframework. Might be it's something you need to implement.
You use:
$loader->registerNamespace('Pimple', BASE_DIR.'/BE/libs/Pimple/1.0.0/lib/Pimple');
To register the Pimple namespace for Pimple is useless, as Pimple is one class in the default namespace (it has no namespace).
The UniversalClassLoader does only work for PSR-0 and PEAR compatible directory layouts. If your layout is not, you should not expect it to work nor to be documented that it works for incompatbile layouts.
Explicit Note: You are using the wrong tool to do the job. All file layouts you have shared over the last hours are incompatible with UniversalClassLoader.
Apart from that if you give the wrong values for it's parameters, the class just won't work.

Using custom classes in Kohana 3

Hey, I'm creating a Call of duty 4 Server Watcher in Kohana 3, and I had created the basic classes for it before:
A static Socket class (for handling basic network commands)
A Cod4Socket class, (which uses the previously mentioned Socket class) that provides wrapper functions for basic commands.
What I want is to be able to use said classes inside the controllers for the website.
Where am I supposed to put the class files, where should I "include" them, and how do I use them?
Edit: I'm using Kohana 3.
Where am I supposed to put the class files?
Add your class files into the application/classes/ directory with lowercase filenames.
Socket should go into application/classes/socket.php
Cod4Socket should go into application/classes/cod4socket.php
Where should I "include" them, and how do I use them?
There is no need to manually include them; simply use them as if they were already included.
The Kohana autoloader will find the classes if they're in the right files.
Additional Info:
Sometimes, you want to place your custom classes in a place like this
application/
classes/
controllers/
.......
models/
......
etc/
CustomClassFirst.php
CustomClassSecond.php
You can call these classes by
$customClassOne = new Etc_CustomClassFirst();
and then redefine the class name into this
class Etc_CustomeClassFirst {}
Did it on my own: http://www.dealtaker.com/blog/2010/06/02/kohana-php-3-0-ko3-tutorial-part-9/
You have to include the files in the bootstrap.php file, and then just call it normally on your controller.

Categories