Code snippets for symfony 1.x

Navigation

Snippets by user Olivier LOYNET

myPakeTransformSchemaSfguard.php

or apply transformation on sfGuard tables into your main schema

Problem

When you generate a schema with sf_guard tables from your main database with the following command, the schema contains all the tables included the sf_guard tables (it's normal).

symfony propel-build-schema xml
 

Then after, when you build the model classes, the sfGuardPlugin classes have a wrong name and are in a wrong place (lib/). There is a post on this problem, how to correctly use propel-build-schema after sfGuardPlugin installation?

symfony propel-build-model
 

Solution

To correct the schema, apply the transformation on the XML schema after the schema generation :

symfony transform-schema-sfguard xml
 

The script add in the schema for all sf_guard_* tables, the package and the phpName with the camelcase used by the plugin.

Also you have to switch off the schema in the plugin to don't have twice declaration :

mv ./plugins/sfGuardPlugin/config/schema.yml ./plugins/sfGuardPlugin/config/schema.yml.off
 

The script: myPakeTransformSchemaSfguard.php

pake_desc( 'apply transformation on sfGuard tables into your main schema' );
pake_task( 'transform-schema-sfguard', 'project_exists' );
 
function run_transform_schema_sfguard($task, $args) 
{
  // Check params
  // -- missing params ?
  if ( !count($args) > 1 ) {
    throw new Exception("You must provide a transformation to apply.\nsymfony transform-schema-sfguard\nsymfony transform-schema-sfguard xml");
  }
 
  // -- schema exists ?
  if ($args[0] == 'xml')
  {
    $schema_filename = sprintf( '%s/schema.xml', sfConfig::get('sf_config_dir') );
    if ( !file_exists($schema_filename) ) {
        throw new Exception( "Missing schema.xml" );
    }
  }
  else
  {
    $schema_filename = sprintf( '%s/schema.yml', sfConfig::get('sf_config_dir') );
    if ( !file_exists($schema_filename) ) {
        throw new Exception( "Missing schema.yml (not yet implemented)" );
    } else {
        throw new Exception( "schema.yml not yet implemented" );
    }
  }
 
  // Backup schema
  //pake_copy( $schema_filename, $schema_filename . '.previous', array('override' => true) );
 
 
  if ($args[0] == 'xml')
  {
    $handle = fopen($schema_filename, "r");
 
    // get the entire file in $contents
    $contents = '';
    while (!feof($handle))
    {
      $contents.= fread($handle, 8192);
    }
    fclose($handle);
 
    $num = preg_match_all('/<table(.*)>/i', $contents, $matches);
 
    // each table definition found
    foreach ($matches[0] as $val)
    {
      if (!preg_match('/package|phpName/i', $val) && preg_match('/name="(sf_guard_.+?)"/i', $val, $val_matches)) {
 
        $table_name = $val_matches[1];
 
        //$php_name   = sfInflector::camelize($val_matches[1]);
        $php_name = sfToolkit::pregtr($val_matches[1], array('#/(.?)#e'   => "'::'.strtoupper('\\1')",
                                                             '/(_)(.)/e'  => "strtoupper('\\2')",
                                                             '/(^)(.)/e'  => "strtolower('\\2')"));
 
        $pattern    = '/(<table.*)(name="'.$table_name.'")(.*>)/i';
        $replace    = '$1$2 package="plugins.sfGuardPlugin.lib.model" phpName="'.$php_name.'" $3';
        $contents   = preg_replace($pattern, $replace, $contents, 1, $count);
 
        pake_echo($table_name);
      } 
    }
 
    // write the result
    $handle = fopen($schema_filename, "w+");
    fwrite($handle, $contents);
    fclose($handle);
  }
}
 

Installation

Put the myPakeTransformSchemaSfguard.php script into data/tasks directory

Use

mv ./plugins/sfGuardPlugin/config/schema.yml ./plugins/sfGuardPlugin/config/schema.yml.off
 
symfony propel-build-schema xml
 
symfony transform-schema-sfguard xml
 
symfony propel-build-model
 
by Olivier LOYNET on 2008-04-09, tagged cli  pake  propel  schema  sfguard 

clear the cache without symfony command line interface

Some PHP code to clean the cache very useful when you don't have access to symfony "command line interface" on production server.

file: symfony_cc.php

<?php
function deltree($f)
{
  $sf = realpath($sf);
 
  if (is_dir($f)) {
    foreach(glob($f.'/*') as $sf) {
      if (is_dir($sf) && !is_link($sf)) {
        deltree($sf);
        if (is_writable($sf)) {
          echo 'Delete dir.: '.$sf."\n";
          rmdir($sf);
        }
      } else {
        if (is_writable($sf)) {
          echo 'Delete file: '.$sf."\n";
          unlink($sf);
        }
      }
    }
  } else {
    die('Error: '.$f.' not a directory');
  }
}
 
echo '<pre>';
echo 'Clean symfony cache'."\n";
echo "\n";
deltree('../../cache');
echo '</pre>';
 
by Olivier LOYNET on 2008-03-11, tagged cache  cli 
(1 comment)