Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

package.xml file structure generator for plugins

It can be a hassle to write a package.yml for a plugin if it contains many files. Fortunately, with the help of the sfFinder class and a bit of programming, you can automate the generation of the <content> part in a batch.

Create a batch/package_generator.php with the following code:

<?php
 
define('SF_ROOT_DIR',    realpath(dirname(__FILE__).'/..'));
define('SF_APP',         'frontend');
define('SF_ENVIRONMENT', 'prod');
define('SF_DEBUG',       false);
 
require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
 
$line          = array();
$lines         = array();
$previous_dirs = array();  
$files         = sfFinder::type('file')->relative()->in('path/to/plugin/files');
 
// Prepare the list of files for nice hierarchy
foreach($files as $file)
{
  $file = str_replace('\\', '/', $file);
  $dirs = explode ('/', $file);
  $line['filename'] = array_pop($dirs);
  $temp_dirs = $dirs;
  foreach($dirs as $dir)
  {
    if($previous_dirs && $dir == $previous_dirs[0])
    {
      array_shift($previous_dirs); 
      array_shift($dirs);
    } 
  }
  foreach($dirs as $dir)
  {
    $line['open'][] = $dir;        
  }
  foreach($previous_dirs as $dir)
  {
    $line['close'][] = $dir;        
  }
  $lines[] = $line;
  $line = array();
  $previous_dirs = $temp_dirs;
}
$blanks = 0;
?>
 
<?php foreach($lines as $line): ?>
<?php if(isset($line['close'])): ?>
<?php foreach($line['close'] as $dir): ?>
<?php echo str_repeat('  ', --$blanks) ?></dir>
<?php endforeach; ?>
<?php endif; ?>
<?php if(isset($line['open'])): ?>
<?php foreach($line['open'] as $dir): $counter++ ?>
<?php echo str_repeat('  ', $blanks++) ?><dir name="<?php echo $dir ?>">
<?php endforeach; ?>
<?php endif; ?>
<?php echo str_repeat('  ', $blanks) ?><file name="<?php echo str_replace('\\', '/', $line['filename']) ?>" role="data"/>
<?php endforeach; ?>

Just run it with

> php batch/package_generator.php

and paste the output in your package.xml. That's all.

by Francois Zaninotto on 2007-01-04, tagged batch  package  plugin 

Comments on this snippet

gravatar icon
#1 Olivier Verdier on 2007-01-11 at 01:31

Ahem. http://www.symfony-project.com/trac/wiki/sfPluginPackageMaker

gravatar icon
#2 Francois Zaninotto on 2007-01-11 at 02:33

Indeed. I should have looked better.

However, the algorithm that takes the output of an sfFinder to make it a hierarchichal tree still has (some) interest, so I'll leave the snippet here.

You need to create an account or log in to post a comment or rate this snippet.