A typical Developer Blog
by Gordon Franke
Icon

How can i override user attribute in my functional test?

Some times your code depend on a user attribute, so you want set the user attribute in your functional test.

The Problem is that the doCall method in the sfBrowser class rebuild the context and in the end calls theshutdown method from user and storage object to write the session data.

So you must create your own Browser class and override the doCall function and simple move the shutdown calls to the beginning.

Browser Class:

class myBrowser extends sfBrowser
{
  /**
   * Calls a request to a uri.
   */
  protected function doCall()
  {
    // manually shutdown user to save current session data
    if (isset($this->context) AND $this->context->getUser())
    {
      $this->context->getUser()->shutdown();
      $this->context->getStorage()->shutdown();
    }
 
    // recycle our context object
    $this->context = $this->getContext(true);
 
    sfConfig::set('sf_test', true);
 
    // we register a fake rendering filter
    sfConfig::set('sf_rendering_filter', array('sfFakeRenderingFilter', null));
 
    $this->resetCurrentException();
 
    // dispatch our request
    ob_start();
    $this->context->getController()->dispatch();
    $retval = ob_get_clean();
 
    // append retval to the response content
    $this->context->getResponse()->setContent($retval);
  }
}

Test code:

include dirname(__FILE__).'/../../bootstrap/functional.php';
 
$browser = new sfTestFunctional(new myBrowser());
 
$browser->
  get('/site-one');
 
// override the session data
$browser->getUser()->setAttribute('key', $value);
 
$browser->
  get('/site-one');

Symfony and doctrine compiled version

In a new symfony project i have tested the doctrine compiled version. since 1.3.7 symfony support this.

http://www.symfony-project.org/blog/2010/09/22/symfony-1-3-7-1-4-7

I have learned two things

first when you compile doctrine use the –driver option. with mysql you can save ~15% file size.

Second use Doctrine_Core not Doctrine. The class Doctrine is deprecated and not in the compiled file. So you get a fatal error.
To fix the class name automatically see:
Change from Doctrine to Doctrine_Core with one command

Simple script to find missing translation strings

Where can i get “Pro Git” book as pdf?

First make step get the git repository

git clone git://github.com/progit/progit.git

when you use ubuntu you must install some packages

sudo aptitude install ruby pandoc texlive-xetex texlive-latex-recommended

then build the pdf in your preferred language. run this command from the latex folder

ruby makepdf en

How can i simple mark all required form fields in symfony 1.3?

Create a function in your BaseForm class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static function listenToPostConfigure($event)
{
  $form            = $event->getSubject();
  $widgetSchema    = $form->getWidgetSchema();
  $validatorSchema = $form->getValidatorSchema();
 
  $fields = $form->getFormFieldSchema()->getWidget()->getFields();
  foreach ($fields as $key => $object)
  {
    $label = $form->getFormFieldSchema()->offsetGet($key)->renderLabelName();
    if (isset($validatorSchema[$key]) and $validatorSchema[$key]->getOption('required') == true)
    {
      $title  = $key . '_field_is_required';
      $label .= '<sup>getFormFormatter()->translate($title) . '">*</sup>';
    }
    $widgetSchema->setLabel($key, $label);
  }
}

Add a listener to the form.post_configure event with your function

1
2
$dispatcher = $this->getEventDispatcher();
$dispatcher->connect('form.post_configure', array('BaseForm', 'listenToPostConfigure'));

How can i change the netbeans color schema to look like symfony documentations?

Simple extract this file into your netbeans-6.8/nb6.8 folder:
SymfonyColorScheme.zip

Thanks Ze Technology for the color schema

http://www.ze-technology.com/2009/12/11/netbeans-aux-couleurs-de-symfony/

Change from Doctrine to Doctrine_Core with one command

To change all your code from Doctrine to Doctrine_Core you can use the following command

1
for fl in `find apps/ config/ lib/ test/ -name *.php`; do mv $fl $fl.old; sed 's/Doctrine::/Doctrine_Core::/g' $fl.old > $fl; rm -f $fl.old; done

When you have a plugin you can use this command

1
for fl in `find . -name *.php`; do mv $fl $fl.old; sed 's/Doctrine::/Doctrine_Core::/g' $fl.old > $fl; rm -f $fl.old; done

How can i check and optimize all Zimbra databases and tables?

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
#
 
source ~zimbra/bin/zmshutil || exit 1
zmsetvars zimbra_home mysql_directory mysql_socket mysql_root_password
 
echo ${mysql_root_password}
 
${mysql_directory}/bin/mysqlcheck --all-databases --optimize -h localhost -P 7306 --protocol=tcp \
        --user=root --password=${mysql_root_password} $*

How can i remove all generated symfony base files automatically from svn?

call this from your project root

for DIR in `find lib -name base -type d`; do svn propset svn:ignore base $DIR/..;svn rm $DIR; done;

How can i run multiple instances of memcached on one server?

Problem

I need more then one memcached server.

Solution

Virtual machine

I can build for each memcached server a virtal machine.

Problems

  • more memory (system+memcached)
  • one ip address for each instance
  • more work

Multiple instances via ports

For this it needs to patch the init.d and start-memcached scripts to run multiple instances.

After the patch it search for configuration files that match the following pattern /etc/memcached_*.conf. When it found no file it use the /etc/memcached.conf configuration file. So it breaks no backward compatibility.

When you want to create 2 memached instances copy the configuration file and change the port, memory …

cp /etc/memcached.conf /etc/memcached_myserver_1.conf
cp /etc/memcached.conf /etc/memcached_myserver_2.conf

Commands

start all servers

/etc/init.d/memcached start

start only a specific server

/etc/init.d/memcached start myserver_1

stop all servers

/etc/init.d/memcached stop

stop only a specific server

/etc/init.d/memcached stop myserver_1

The patches

/usr/share/memcached/scripts/start-memcached

26,30d25
> if (scalar(@ARGV) == 2) {
> 	$etcfile = shift(@ARGV);
> 	$pidfile = shift(@ARGV);
> }
>

/etc/init.d/memcached

16a17
> DAEMONNAME=memcached
18d18
< NAME=memcached
20d19
< PIDFILE=/var/run/$NAME.pid
26a26,63
> FILES=(/etc/memcached_*.conf);
> # check for alternative config schema
> if [ -r "${FILES[0]}" ]; then
>   CONFIGS=();
>   for FILE in "${FILES[@]}";
>   do
>     # remove prefix
>     NAME=${FILE#/etc/};
>     # remove suffix
>     NAME=${NAME%.conf};
>
>     # check optional second param
>     if [ $# -ne 2 ];
>     then
>       # add to config array
>       CONFIGS+=($NAME);
>     elif [ "memcached_$2" == "$NAME" ];
>     then
>       # use only one memcached
>       CONFIGS=($NAME);
>       break;
>     fi;
>   done;
>
>   if [ ${#CONFIGS[@]} == 0 ];
>   then
>     echo "Config not exist for: $2" >&2;
>     exit 1;
>   fi;
> else
>   CONFIGS=(memcached);
> fi;
>
> CONFIG_NUM=${#CONFIGS[@]};
> for ((i=0; i < $CONFIG_NUM; i++)); do
>   NAME=${CONFIGS[${i}]};
>   PIDFILE="/var/run/${NAME}.pid";
>
30c67
<       start-stop-daemon --start --quiet --exec $DAEMONBOOTSTRAP
---
>       start-stop-daemon --start --quiet --exec "$DAEMONBOOTSTRAP" -- /etc/${NAME}.conf $PIDFILE
50c87
<       start-stop-daemon --start --quiet --exec $DAEMONBOOTSTRAP
---
>       start-stop-daemon --start --quiet --exec "$DAEMONBOOTSTRAP" -- /etc/${NAME}.conf $PIDFILE
54c91
<       N=/etc/init.d/$NAME
---
>       N=/etc/init.d/$DAEMONNAME
59a97
> done;