/tmp/cache directory problem - php

I have just started out with testing some php mvc framework
In it, it has this function that throws an error.
The cachedirectory is set to /tmp/cache from the config file
additional:
The php is hosted on an IIS server.
Can someone help me out to get this working somehow?
This is the function within the class
function setCacheDir($cacheDir = null)
{
if( is_null( $cacheDir ) )
{
$config = config::getInstance();
$cacheDir = $config->config_values['template']['cache_dir'];
}
if (is_dir($cacheDir) && is_writable($cacheDir))
{
$config = config::getInstance();
$this->cache_dir = $cacheDir;
}
else
{
throw new Exception("De cache directory '$cacheDir' either does not exist, or is unwriteble");
}
}
thanks, Richard

Why don't you set the cache directory to something a little more Windows-y, like c:\temp (and make sure that folder exists).

I'm guessing "/tmp/cache" doesn't exist and isn't writable, so in the configuration file, set cache_dir to a directory that is.

Some PHP frameworks work best (or better) in a LAMP stack, the first letter (L) being Linux. If the documentation of your framework advises a LAMP stack, I'd go with that.

Related

Include module plugin in Zend

I wrote an Auth plugin to check if the user is logged in. No unlogged user should be able to visit anything in the app except the login page.
So I have this in the file application/modules/user/plugins/Auth.php:
class User_Plugin_Auth extends Zend_Controller_Plugin_Abstract {
public function preDispatch(Zend_Controller_Request_Abstract $request) {
if (Zend_Auth::getInstance()->hasIdentity()
|| $this->getRequest()->getActionName() == 'login') return;
$request->setModuleName('user');
$request->setControllerName('auth');
$request->setActionName('login');
}
}
Then I made this in the application.ini:
pluginPaths.User_Plugin = APPLICATION_PATH "/modules/user/plugins/"
resources.frontController.plugins[] = "User_Plugin_Auth"
However, no matter how I move the Auth.php file and no matter the name, I always get Fatal error: Class 'User_Plugin_Auth' not found. Please help me, I have wasted more than one hour on this and it's frustrating.
I think the problem is to do with the filename. I would try creating copies of the files in these locations
application/modules/user/plugins/User_Plugin_Auth.php
application/modules/user/plugins/User/Plugin/Auth.php
naturally you only need one of them, so delete the others once you have found the one that works.
If that doesn't help that the syntax I have in my config (Fam is just the codename of the project)
resources.frontController.plugins.layout = "Fam\Controller\Plugin\Layout"
resources.frontController.plugins.route = "Fam\Controller\Plugin\Route"
as pointed out in comments that is php 5.3 - this should work in older versions assuming include paths are configured
resources.frontController.plugins.layout = "Fam_Controller_Plugin_Layout"
resources.frontController.plugins.route = "Fam_Controller_Plugin_Route"
which maps to the files in my library, eg
APPLICATION_PATH "/../library/Fam/Controller/Plugin/Layout.php"
For reference in this project my Zend files are in
APPLICATION_PATH "/../library/Zend"
so adjusting the files to be in the relative place should do the trick
is your autoloader configured? I have
autoloaderNamespaces.0 = "Fam"
you might need something like
autoloaderNamespaces[] = "User_"
resources.frontController.plugins.UserAuth = "User_Plugin_Auth"

spl_autoload server related issue

We seem to have a very strange problem with the spl_autoload on 1 particular servers.
Warning: spl_autoload() [function.spl-autoload]: Unable to access file.php
public function loadClass($className) {
if(substr($className, -5) == 'Override') {
$classes = glob(_ROOT_DIR_ . 'classes/*/override/*/*.php');
foreach($classes AS $class) {
$explodePath = explode('/', $class);
$explodePath = array_reverse($explodePath);
if(file_exists(_ROOT_DIR_.'classes/'.$explodePath[3].'/override/'.$explodePath[1].'/'.$explodePath[0])) {
set_include_path(get_include_path().PATH_SEPARATOR._ROOT_DIR_.'classes/'.$explodePath[3].'/override/'.$explodePath[1].'/');
spl_autoload(strtolower($className));
}
}
}
return FALSE;
}
Also I have checked and in our WHM panel it shows the include path is set to
.:/usr/lib/php:/usr/local/lib/php
This particular server is Linux RedHat-9.3.6 running PHP 5.3.8 & eAccelerator
the code above has been testing on 10 other servers and works fine so really at lost to why this would be happening on this particular server.
We would really like to use the spl_autoload method instead of include/require which would slow down our application.
Please I hope someone can offer some advice with helping us to solve this issue.

Magento different config files for live and staging

Is it possible to have different config.xml files for live, staging and dev versions of the site.
Im thinking that there should be a live.xml, local.xml and staging.xml and these should be set up once and then controlled via htaccess environment variable or something
Is this possible?
No, nothing supports this out of the box with Magento, and there's no module I know of that offers this functionality.
You could implement a version of this if you wanted to. All of Magento's config files are (or should be, don't hold me to it) loaded via the Mage_Core_Model_Config class.
#File: app/code/core/Mage/Core/Model/Config.php
public function loadModulesConfiguration($fileName, $mergeToObject = null, $mergeModel=null)
{
$disableLocalModules = !$this->_canUseLocalModules();
if ($mergeToObject === null) {
$mergeToObject = clone $this->_prototype;
$mergeToObject->loadString('<config/>');
}
if ($mergeModel === null) {
$mergeModel = clone $this->_prototype;
}
$modules = $this->getNode('modules')->children();
foreach ($modules as $modName=>$module) {
if ($module->is('active')) {
if ($disableLocalModules && ('local' === (string)$module->codePool)) {
continue;
}
$configFile = $this->getModuleDir('etc', $modName).DS.$fileName;
if ($mergeModel->loadFile($configFile)) {
$mergeToObject->extend($mergeModel, true);
}
}
}
return $mergeToObject;
}
You could create a class rewrite for this model that would alter the $filename based on environment, and then load the appropriate configuration file.
While it's an interesting idea, I don't think it's a good one. Differences in environment is something that should be handled on a deployment level, and a module's config.xml file shouldn't have any environment specific information in it anyways. Environment specific information is kept in one location
app/etc/local.xml
Your build/deploy system should modify local.xml as needed depending on which environment you're deploying to.

Translating paths in PHP from (*nix) server to (winxp) dev machine

I'm working on a PHP project that has a lot of hard coded paths in it. I'm not the main developer, just working on a small part of the project.
I'd like to be able to test my changes locally before committing them, but my directory structure is completely different. For example, there's a lot of this in the code:
require_once("/home/clientx/htdocs/include.php")
Which doesn't work on my local WAMP server because the path is different. Is there a way to tell either WAMP or XP that "/home/clientx/htdocs/" really means "c:/shared/clients/clientx"?
If its a local copy, do a search and replace on the whole directory , Please don't forget trailing slash. And when you commit the code, do reverse.
This is the solution, if you don't want to add extra variables and stuff (because that would change other developers' code/work/dependencies (if any)
search "/home/clientx/htdocs/" and replace to this: "c:/shared/clients/clientx/"
Always use $_SERVER['DOCUMENT_ROOT'] instead of hardcoded path.
require_once($_SERVER['DOCUMENT_ROOT']."/include.php")
as for your wamb environment, you will need a dedicated drive to simulate file structure. You can use NTFS tools or simple subst command to map some directory to a drive.
Create /home/clientx/htdocs/ folder on this drive and change your httpd.conf to reflect it.
But again, you will do yourself a huge favor by convincing your coworkers to stop using hardcoded paths
WARNING: ONLY USE THIS SOLUTION FOR EMERGENCY REPAIRS, NEVER FOR LONGER PRODUCTION CODE
Define a class with rewriting methods, see http://php.net/manual/en/class.streamwrapper.php
<?php
class YourEmergencyWrapper {
static $from = '/home/clientx/htdocs/';
static $to = 'c:/shared/clients/client';
private $resource = null;
//...some example stream_* functions, be sure to implement them all
function stream_open($path,$mode,$options=null,&$opened_path){
$path = self::rewrite($path);
self::restore();
$this->resource = fopen($path,$mode,$options);
self::reenable();
$opened_path = $path;
return is_resource($this->resource);
}
function stream_read($count){
self::restore();
$ret = fread($this->resource,$count);
self::reenable();
return $ret;
}
function stream_eof(){
self::restore();
$ret = feof($this->resource);
self::reenable();
return $ret;
}
function stream_stat(){
self::restore();
$ret = fstat($this->resource);
self::reenable();
return $ret;
}
static function rewrite($path){
if(strpos($path,self::$from)===0) $path = self::$to.substr($path,strlen(self::$from));
return $path;
}
//... other functions
private static function restore(){
stream_wrapper_restore('file');
}
private static function reenable(){
stream_wrapper_unregister('file');
stream_wrapper_register('file',__CLASS__);
}
}
stream_wrapper_unregister('file');
stream_wrapper_register('file','YourEmergencyWrapper');
Seriously, only some local debugging on your own dev-server. You can force it as an auto_prepend on almost any code. Left some function yet be implemented ;P

How can I refer to different environment's inside Symfony's ProjectConfiguration.class.php

I have a staging and development environment on the same machine. I would like to configure a different memcached port in ProjectConfiguration.class.php depending on my environment. I imagine its not safe to use $SERVER['HTTP_HOST'] inside of the ProjectConfiguration file because that won't account for tasks run from the command line.
What would be the best way to accomplish what this bit of code intends:
public function configureDoctrine(Doctrine_Manager $manager) {
if ($_SERVER['HTTP_HOST'] == 'dev.example.com') {
$memcached_port = 11211;
} else {
$memcached_port = 11212;
}
$servers = array(
'host' => '127.0.0.1',
'port' => $memcached_port,
'persistent' => true);
}
There are 2 ways to accomplish this.
To start off, you should be setting your environment separately in index.php. What we usually do is set it to prod, and then check in the file, and manually change it to dev on our dev servers and local machines in order to make deployment easier. I also recommend put this file in .gitignore so it never accidentally gets changed.
Use the app.yml configuration file. In your app.yml config, set up a key called 'memcache_port' and set the value separately for each environment. The yaml file would look something like this:
prod:
memcached_port: 11211
dev:
memcached_port: 11212
Then to access this value in the code you would say:
$memcached_port = sfConfig::get('app_memcached_port');
Get the environment name manually through the symfony context
$env = sfContext::getInstance()->getConfiguration()->getEnvironment();
if($env == 'dev') {
$memcached_port = 11211;
} else if($env == 'prod') {
$memcached_port = 11212;
}
I personally prefer option 1, but it's totally up to you how you want to handle this.
Note: When I refer to the environment, I am referring to the symfony environment defined in the index.php file.
Hope this helps.
I use http host in the web controller to detect the environment and pass it to the project configuration instance, as per here:
<?php
require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');
$env = ProjectConfiguration::getEnvironment();
if (!in_array($env, array('dev'))) throw new Exception('Not Allowed');
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', $env, true);
sfContext::createInstance($configuration)->dispatch();
This is from a frontend_dev.php - so it also ensures that you can't access the dev controller anywhere but dev.
My project configuration class contains the referenced method, which does the work:
public function getEnvironment() {
if ($_SERVER ['HTTP_HOST'] == 'dev.example.com') {
return 'dev';
} else {
return 'prod';
}
}
As you rightly say, there are also command line tasks to consider - but almost all symfony tasks will take --env=xxx arguments. I use those. They all default to dev anyway, which is where I do most of my work, so its not as arduous as it sounds.
I'd then use if (sfConfig::get('sf_environment') == 'dev') in your if statement, rather than the HTTP_HOST directly, which will work from both cmd line and web.

Categories