Is it possible to add custom context to the .zfproject.xml? - php

I need to add custom context classesDirectory in applicationDirectory section to the .zfproject.xml.
I have created a context class, that extends Zend_Tool_Project_Context_Filesystem_Directory but the issue is that all contexts loading necessary to parse .zfproject.xml are hardcoded in Zend_Tool_Project_Provider_Abstract class as:
$contextRegistry->addContextsFromDirectory(
dirname(dirname(__FILE__)) . '/Context/Zf/', 'Zend_Tool_Project_Context_Zf_'
);
$contextRegistry->addContextsFromDirectory(
dirname(dirname(__FILE__)) . '/Context/Filesystem/', 'Zend_Tool_Project_Context_Filesystem_'
);
So I don't see any way to load my context without patching zend sources (or adding custom files to Zend library directories).
Is it even possible?

I don't believe it is possible without changing the ZF source code. You could try creating an issue on http://framework.zend.com/issues along with a patch. Please make this generic though.
Alternatively, what are you trying to do that requires a custom context? Maybe there's another way to solve it?

Related

Yii2: How to force using fallback MessageFormatter method?

My website is with a hosting provider that has the MessageFormatter class available on the server (Linux, PHP 7.0.27) but it is an old ICU version (4.2.1) that doesn't support my message {number,plural,=0{# available} =1{# available} other{# available}} and gives the error:
Message pattern is invalid: Constructor failed
msgfmt_create: message formatter creation failed: U_ILLEGAL_CHARACTER
...because of the =1 and =2 notation.
I'm not able to make changes to the server so how can I force using the fallback method provided by Yii2 which works just fine?
There is this hacky way you can try.
Copy the yii\i18n\MessageFormatter code to a new file. Name it MessageFormatter.php and place somewhere in your application (but not in vendor folder).
In this new file change the format() method to:
public function format($pattern, $params, $language)
{
$this->_errorCode = 0;
$this->_errorMessage = '';
if ($params === []) {
return $pattern;
}
return $this->fallbackFormat($pattern, $params, $language);
}
Don't change anything else (including namespace).
Now let's use Yii mapping.
Find a place in your application when you can put code that will be run every time in bootstrapping phase. Good place for this is common/config/bootstrap.php if you are using "Advanced Template"-like project.
Add there this line:
Yii::$classMap['yii\i18n\MessageFormatter'] = 'path/to/your/MessageFormatter.php';
Obviously change the path to the one you've chosen. Now Yii autoloader will load this class from your file instead of the original Yii vendor folder (as mentioned in Class Autoloading section of the Guide).
In the modified file MessageFormatter method presence of intl library is never checked so fallback is used as default.
The downside of this trick is that you need to update manually your file every time original Yii file is changed (so almost every time you upgrade Yii version).
Another approach is to configure I18N component in your application to use your custom MessageFormatter where you can extend the original file and just override format() method inside without modifying class map.

How would I use Cakepdf within my plugin?

Im tryng to use CakePdf https://github.com/ceeram/CakePdf but I need to be able to use it within the plugin I have created. Im not sure App:uses to call to use the plugin since im not familiar to his folder structure or what hes importing. I'm using Croogo CMS if that might be of any importance.
This is what I know and have done:
I have read their Read Me tutorial and have followed the intructions to the dot, but again, it does not explain usage within another plugin.
Their Bootsrap.php file is as so:
App::build(array('Pdf' => array('%s' . 'Pdf' . DS)), App::REGISTER);
App::build(array('Pdf/Engine' => array('%s' . 'Pdf/Engine' . DS)), App::REGISTER);
App::uses('PdfView', 'CakePdf.View');
To my understanding this is telling Cakephp to treat the files found within these folders as usable classes within the application. When I try App:Uses('CakePdf',('CakePdf.Controller') this does not load the correct class files. I've tested and found that the class is not evein available at my plugin level besided successful plugin bootstraping.
I had to use App::uses('CakePdf', 'CakePdf.Pdf');

PyroCMS - Loading a custom library in a module

I'm writing a PyroCMS module which involves image upload and thumbnail generation. I'm aware codeigniter has a built in image manipulation class that's capable of making thumbnails, but I'm a big for of phpThumb'ss adaptive resize function. For this reason I'd like to try and get phpThumb working.
I've placed the phpThumb files in ./addons/shared_addons/modules/mymodule/libraries
and I'm trying to load using the following:
$this->load->library('phpThumb/ThumbLib.inc.php');
I'm including the extension because an .inc.php file is not a .php file right? Either way if I keep the extension or not I get this error:
Class 'ThumbLib.inc.php' not found in .../htdocs/system/cms/libraries/MX/Loader.php on line 160
Anyone know what I'm doing wrong?
Thanks, Ed.
The loader naming conventions are probably stricter, so you may need to rename your file to something like 'Thumblib.php' and then declare your class as so class Thumblib {. You may also need to check the library doesn't conflict with anything as is suitable to use in CI.
Also, I think as long as you are loading the library from a controller in the same module folder tree as the library, the load line should be something like:
$this->load->library('Thumblib');
Or if not:
$this->load->library('othermodule/Thumblib');
Good luck.
This is a CodeIgniter question, not a PyroCMS question.
Simple solution:
include 'whatever/the/hell/you.like.php';
Remember guys it's just PHP. You don't need a special method for everything!

Developing/using a custom Resource Plugin in Zend Framework

We have used Zend_Log, which is configured in application.ini differently for different circumstances. We initialize it/get it in the bootstrap and store it in the registry:
$r = $this->getPluginResource('log');
$logger = $r->getLog();
But we've subclassed Zend_Log (say, Our_Log) to add customized features, and want to get it the same way. So then we have to make a new Resource Plugin. That seems quite easy - just copy Application/Resource/Log.php, rename the file to Ourlog.php, rename the class to class Zend_Application_Resource_Ourlog. For now, let's not worry about "Our_Log", the class -- just use the new Resource Plugin to get a Zend_Log, to reduce the variables.
So then, our new code in the bootstrap is:
$r = $this->getPluginResource('ourlog');
$logger = $r->getLog();
but of course this doesn't work, error applying method to non-object "r". According to the documentation,
"As long as you register the prefix path for this resource plugin, you
can then use it in your application."
but how do you register a prefix path? That would have been helpful. But that shouldn't matter, I used the same prefix path as the default, and I know the file is being read because I "require" it.
Anyway, any guidance on what simple step I'm missing would be greatly appreciated.
Thanks for the pointers -- so close, so close (I think). I thought I was getting it...
Okay, so I renamed the class Xyz_Resource_Xyzlog, I put it in library/Xyz/Resource/Xyzlog.php
Then, because I don't love ini files, in the bootstrap I put:
$loader=$this->getPluginLoader();
$loader->addPrefixPath('Xyz_Resource','Xyz/Resource/');
$r = $this->getPluginResource('xyzlog');
if (!is_object($r)) die ('Not an object!!');
Sudden Death. So, okay, do the ini:
pluginPaths.Xyz_Resource='Xyz/Resource/'
The same. No help. I believed that the basepaths of the plugin paths would include the PHP "include" paths. Am I mistaken in that? Any other ideas? I'll happily write up what finally works for me to help some other poor soul in this circumstance. Something to do with Name Spaces, maybe?
Plugin classes are resolved using the plugin loader, which works slightly differently to the autoloader; so just requiring the class in doesn't help you here. Instead, add this to your application.ini:
pluginPaths.Application_Resource = "Application/Resource"
you should then be able to use the class as normal. Since your path above will be checked before the default Zend one, you can also name your class 'Log' and still extend the Logger resource to override the standard functionality.

What are the possible reasons for App::import() not working?

I'm trying to implement a simple way to manage static pages in CakePhp, as described in this article.
The problem I'm facing is that App::import() doesn't seem to import the required class in the routes.php file.
The code is the following:
App::import('Model','StaticPage');
$page = new StaticPage();
$slugs = $page->find('list', array(
'fields' => array('StaticPage.slug'),
'order' => 'StaticPage.slug DESC'
));
I'm getting the error: Fatal error: Class 'StaticPage' not found in ...
This class is present in the models folder (models/StaticPage.php).
I just started CakePhp a few weeks ago and I guess I'm missing a simple thing here...
I'm using CakePhp 1.3 and Php 5.2.42.
I think the reason it doesn't work is because you don't follow CakePHP's naming conventions for file names: file names are lowercase and underscored. So renaming your file to static_page.php should fix the problem.
Having taken a quick look at the article you reference, your snippet doesn't match up. You're importing the ClassRegistry class (which doesn't need to be imported) and then trying to instantiate a StaticPage. I'd recommend removing the AppImport reference all together and using ClassRegistry:
$page = ClassRegistry::init( 'StaticPage' );
You don't need the AppImport line because ClassRegistry::init() both loads the model and instantiates the object.
The other (potential) problem I see is that your model file name doesn't follow convention. It should be models/static_page.php. Cake's inflection may not be handling the deviation from the norm.
Like the error says: You are missing the Class StaticPage. Are you sure that you have this file? If you do, sure that it's in right place, has the right filename so the autoloader can find it?.

Categories