Example #1
0
  $t->is(sfToolkit::literalize($param), false, sprintf('::literalize() returns false with "%s"', $param));
  if (strtoupper($param) != $param)
  {
    $t->is(sfToolkit::literalize(strtoupper($param)), false, sprintf('::literalize() returns false with "%s"', strtoupper($param)));
  }
  $t->is(sfToolkit::literalize(' '.$param.' '), false, sprintf('::literalize() returns false with "%s"', ' '.$param.' '));
}

foreach (array('null', '~', '') as $param)
{
  $t->is(sfToolkit::literalize($param), null, sprintf('::literalize() returns null with "%s"', $param));
  if (strtoupper($param) != $param)
  {
    $t->is(sfToolkit::literalize(strtoupper($param)), null, sprintf('::literalize() returns null with "%s"', strtoupper($param)));
  }
  $t->is(sfToolkit::literalize(' '.$param.' '), null, sprintf('::literalize() returns null with "%s"', ' '.$param.' '));
}

// ::replaceConstants()
$t->diag('::replaceConstants()');
sfConfig::set('foo', 'bar');
$t->is(sfToolkit::replaceConstants('my value with a %foo% constant'), 'my value with a bar constant', '::replaceConstantsCallback() replaces constants enclosed in %');
$t->is(sfToolkit::replaceConstants('%Y/%m/%d %H:%M'), '%Y/%m/%d %H:%M', '::replaceConstantsCallback() does not replace unknown constants');
sfConfig::set('bar', null);
$t->is(sfToolkit::replaceConstants('my value with a %bar% constant'), 'my value with a  constant', '::replaceConstantsCallback() replaces constants enclosed in % even if value is null');
$t->is(sfToolkit::replaceConstants('my value with a %foobar% constant'), 'my value with a %foobar% constant', '::replaceConstantsCallback() returns the original string if the constant is not defined');
$t->is(sfToolkit::replaceConstants('my value with a %foo\'bar% constant'), 'my value with a %foo\'bar% constant', '::replaceConstantsCallback() returns the original string if the constant is not defined');
$t->is(sfToolkit::replaceConstants('my value with a %foo"bar% constant'), 'my value with a %foo"bar% constant', '::replaceConstantsCallback() returns the original string if the constant is not defined');

// ::isPathAbsolute()
$t->diag('::isPathAbsolute()');
    /**
     * @see sfTask
     */
    protected function execute($arguments = array(), $options = array())
    {
        $taskName = $arguments['task_name'];
        $taskNameComponents = explode(':', $taskName);
        $namespace = isset($taskNameComponents[1]) ? $taskNameComponents[0] : '';
        $name = isset($taskNameComponents[1]) ? $taskNameComponents[1] : $taskNameComponents[0];
        $taskClassName = str_replace('-', '', $namespace ? $namespace . ucfirst($name) : $name) . 'Task';
        // Validate the class name
        if (!preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $taskClassName)) {
            throw new sfCommandException(sprintf('The task class name "%s" is invalid.', $taskClassName));
        }
        $briefDescription = $options['brief-description'];
        $detailedDescription = <<<HED
The [{$taskName}|INFO] task does things.
Call it with:

  [php symfony {$taskName}|INFO]
HED;
        $useDatabase = sfToolkit::literalize($options['use-database']);
        $defaultConnection = is_string($useDatabase) ? $useDatabase : sfConfig::get('sf_orm');
        if ($useDatabase) {
            $content = <<<HED
<?php

class {$taskClassName} extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    // \$this->addArguments(array(
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
    // ));

    \$this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', '{$defaultConnection}'),
      // add your own options here
    ));

    \$this->namespace        = '{$namespace}';
    \$this->name             = '{$name}';
    \$this->briefDescription = '{$briefDescription}';
    \$this->detailedDescription = <<<EOF
{$detailedDescription}
EOF;
  }

  protected function execute(\$arguments = array(), \$options = array())
  {
    // initialize the database connection
    \$databaseManager = new sfDatabaseManager(\$this->configuration);
    \$connection = \$databaseManager->getDatabase(\$options['connection'])->getConnection();

    // add your code here
  }
}

HED;
        } else {
            $content = <<<HED
<?php

class {$taskClassName} extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    // \$this->addArguments(array(
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
    // ));

    // // add your own options here
    // \$this->addOptions(array(
    //   new sfCommandOption('my_option', null, sfCommandOption::PARAMETER_REQUIRED, 'My option'),
    // ));

    \$this->namespace        = '{$namespace}';
    \$this->name             = '{$name}';
    \$this->briefDescription = '{$briefDescription}';
    \$this->detailedDescription = <<<EOF
{$detailedDescription}
EOF;
  }

  protected function execute(\$arguments = array(), \$options = array())
  {
    // add your code here
  }
}

HED;
        }
        // check that the task directory exists and that the task file doesn't exist
        if (!is_readable(sfConfig::get('sf_root_dir') . '/' . $options['dir'])) {
            $this->getFilesystem()->mkdirs($options['dir']);
        }
        $taskFile = sfConfig::get('sf_root_dir') . '/' . $options['dir'] . '/' . $taskClassName . '.class.php';
        if (is_readable($taskFile)) {
            throw new sfCommandException(sprintf('A "%s" task already exists in "%s".', $taskName, $taskFile));
        }
        $this->logSection('task', sprintf('Creating "%s" task file', $taskFile));
        file_put_contents($taskFile, $content);
    }
 /**
  * Loads the linear list of attributes from the [names] category.
  *
  * @param string The configuration file name (for exception usage)
  * @param array  An associative array of request method data
  * @param array  An associative array of file/parameter names in which to store loaded information
  * @param array  An associative array of validator data
  * @param array  The loaded ini configuration that we'll use for verification purposes
  * @param string A comma delimited list of file/parameter names
  */
 protected function loadAttributes(&$configFiles, &$methods, &$names, &$validators, &$config, &$list)
 {
     foreach ($config['names'] as $name => $attributes) {
         // get a reference to the name entry
         if (preg_match('/^([a-z0-9_-]+)\\{([a-z0-9\\s_-]+)\\}$/i', $name, $match)) {
             // this name entry has a parent
             $subname = $match[2];
             $parent = $match[1];
             if (!isset($names[$parent][$subname])) {
                 // unknown parent or subname
                 $error = sprintf('Configuration file "%s" specifies unregistered parent "%s" or subname "%s"', $configFiles[0], $parent, $subname);
                 throw new sfParseException($error);
             }
             $entry =& $names[$parent][$subname];
         } else {
             // no parent
             if (!isset($names[$name])) {
                 // unknown name
                 $error = sprintf('Configuration file "%s" specifies unregistered name "%s"', $configFiles[0], $name);
                 throw new sfParseException($error);
             }
             $entry =& $names[$name];
         }
         foreach ($attributes as $attribute => $value) {
             if ($attribute == 'validators') {
                 // load validators for this file/parameter name
                 $this->loadValidators($configFiles, $validators, $config, $value, $entry);
             } else {
                 if ($attribute == 'type') {
                     // name type
                     $lvalue = strtolower($value);
                     $entry['file'] = $lvalue == 'file' ? 'true' : 'false';
                 } else {
                     // just a normal attribute
                     $entry[$attribute] = sfToolkit::literalize($value, true);
                 }
             }
         }
     }
 }
/**
 * Changes some style attributes of an existing DOM element unobstrusively
 *
 * <b>Example:</b>
 * <code>
 *  <?php UJS_attr('#foo', array('display' => 'none', 'text-decoration' => 'underline')) ?>
 *  // You can also use the string syntax
 *  <?php UJS_attr('#foo', 'display:none text-decoration:underline') ?>
 * </code>
 *
 * @param string the CSS3 selector to the DOM element(s) to replace
 * @param array the style attributes to update
 */
function UJS_css($selector, $css_options = array())
{
  if(is_string($css_options))
  {
    // Fixme: allow for semicolon separator
    preg_match_all('/
      \s*([\w-]+)             # key (may contain dash)            \\1
      \s*:\s*                 # :
      (\'|")?                 # values may be included in \' or " \\2
      (.*?)                   # value                             \\3
      (?(2) \\2)              # matching \' or " if needed        \\4
      \s*(?:
        (?:[\w-]+\s*:) | \s*$  # followed by another key: or the end of the string
      )
    /x', $css_options, $matches, PREG_SET_ORDER);
    $css_options = array();
    foreach ($matches as $val)
    {
      $css_options[$val[1]] = sfToolkit::literalize($val[3]);
    }
  }

  $response = sfContext::getInstance()->getResponse();
  $script = $response->getParameter('script', '', 'symfony/view/UJS');
  $attributes = '';
  foreach($css_options as $key => $value)
  {
    $attributes .= sprintf(".css('%s', '%s')", $key, $value);
  }
  $script .= sprintf("\$('%s')%s;\n", $selector, $attributes);
  $response->setParameter('script', $script, 'symfony/view/UJS');
}