PHP Symfony, Comment changer les messages par defaut des validateurs Symfony

Comment faire , quand on veut changer tous les messages par défaut des validateurs Symfony , sans les définir un par un par un bete ‘message’ => ‘mais blublu’ et sans allez les changer directement dans les entrailles de symfony ce qui est mal !!! C’est mal !!!

oh oui dit nous père castor
Et bien comme ça

1
2
3
    sfValidatorBase::setInvalidMessage('Champ invalide');
    sfValidatorBase::setRequiredMessage('Champ obligatoire');
    parent::setup();

Ce qui nous donne dans un vrai bout de php

VentesForm.class.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
class VentesForm extends BaseVentesForm
{
  public function configure()
  {
    sfValidatorBase::setInvalidMessage('Champ invalide');
    sfValidatorBase::setRequiredMessage('Champ obligatoire');
    parent::setup();
    unset($this['created_at'], $this['updated_at'],
          $this['expires_at'], $this['is_active']);
    $this->widgetSchema['uid'] = new sfWidgetFormInput(array('type' => 'hidden', 'is_hidden' => true));
    $this->widgetSchema['civ'] = new sfWidgetFormChoice(array('choices' => array('M.' => 'M.', 'Mme' => 'Mme', 'Mlle' => 'Mlle')));
    $this->widgetSchema['adresse1'] = new sfWidgetFormTextarea();
    $this->widgetSchema['adresse2'] = new sfWidgetFormTextarea();
    $this->widgetSchema['adresse3'] = new sfWidgetFormTextarea();
    $this->validatorSchema['email'] = new sfValidatorEmail(array(), array('invalid' => 'L\'adresse email saisie est incorrecte'));
    $this->widgetSchema->setLabel('numeroabo', 'N°Abonné');
  }
}

sfValidatorBase

NOTE:
Deprecated depuis la version 1.4 de symfony remplacer par
sfValidatorBase::setDefaultMessage() method

http://www.symfony-project.org/tutorial/1_4/en/deprecated

1
2
3
sfValidatorBase::setDefaultMessage('invalid', 'Champ invalide');
sfValidatorBase::setDefaultMessage('required', 'Champ obligatoire');
parent::setup();

PHP Symfony Text Helper

Text Helper
http://www.symfony-project.org/api/1_4/TextHelper

<?php
echo use_helper('Text')
echo auto_link_text() // Turns all urls and email addresses into clickable links. The +link+ parameter can limit what should be linked.
echo excerpt_text() // Extracts an excerpt from the +text+ surrounding the +phrase+ with a number of characters on each side determined
echo highlight_text($text, $phrase, $highlighter) // Highlights the +phrase+ where it is found in the +text+ by surrounding it like
echo simple_format_text()  // Returns +text+ transformed into html using very simple formatting rules
echo strip_links_text() // Turns all links into words, like "<a href="something">else</a>" to "else".
echo truncate_text() // Truncates +text+ to the length of +length+ and replaces the last three characters with the +truncate_string+
echo wrap_text() // Word wrap long lines to line_width.
echo _auto_link_email_addresses() // Turns all email addresses into clickable links.
echo _auto_link_urls() // Turns all urls into clickable links
?>

PHP Fork it

1
2
3
4
5
6
7
<?php
function test($max) {
  for($i = 0; $i < $max; $i++) {
 
  }
  }
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
class fork {
  private $children = array();
 
  public function __construct($threads) {
    $this->timestart = microtime(true);
    $this->threads = $threads;
  }
 
  public function forker($worker, $data) {
    foreach($data as $row) {
      $pid = pcntl_fork();
      if($pid == -1 ) {
        exit(1);
      } elseif($pid) {
        echo ' PARENT | launching thread (pid: '.$pid.')'."\n";
        $this->children[] = $pid;
        if(count($this->children) >= $this->threads) {
          $pid = array_shift($this->children);
          pcntl_waitpid($pid, $status);
        }
      } else {
        echo ' CHILD  | working on '.$worker.'('.$row.')'."\n";
        $worker($row);
        echo ' CHILD  | finish '.$worker.'('.$row.')'."\n";
        exit(0);
      }
    }
  }
 
  public function waitchildrens() {
    foreach($this->children as $child) {
      pcntl_waitpid($child, $status);
    }
  }
 
  public function time() {
    $timeend = microtime(true);
    $time = $timeend - $this->timestart;
    echo ' TIME   | '.$time.' seconde(s)'."\n";
  }
}
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$fork = new fork(4);
$fork->forker('test', array('11111111',
                            '22222222',
                            '33333333',
                            '44444444',
                            '55555555',
                            '66666666',
                            '77777777',
                            '88888888'));
$fork->waitchildrens();
$fork->time();
?>

PHP Symfony, fichier d’exemple routing.yml

routing.yml
ou ? apps/%app%/config/routing.yml

# default rules
homepage:
  url:   /
  param: { module: post, action: index }
 
post_show:
  url:   /blog/:slug.:sf_format
  class: sfDoctrineRoute
  param: { module: post, action: show, sf_format: html }
  options:
    generate_shortest_url: false
    type: object
    model: BlogPost
    column: slug
  requirements:
    sf_method: get
 
post_addComment:
  url: /comment/add
  class: sfRequestRoute
  param: { module: post, action: addComment }
  requirements:
    sf_method: post

PHP comment « streamer » du mp3 en php

J’ai du récemment envoyer des mp3 a un player flash sans que le chemin des mp3 ne soit accessible a l’utilisateur et qu’ils soient stocké hors de la racine du serveur web.

Le bout de code qui sert a servir les mp3,
attention a vous de vérifier que ce que vous envoyez est bien du mp3 ^^

1
2
3
4
5
6
$file_path = '/home/hio/monfichier.mp3';
header('Content-type: audio/mpeg');
header('Content-Disposition: inline; filename="'.$file_path.'"');
header('Content-transfer-encoding: binary');
header('Content-length: '.filesize($file_path));
readfile($file_path);

Voila rien de bien compliquer ^^

PHP comment recuperer l’extension d’un fichier

Une fonction php pratique qui permet de récupérer l’extension d’un fichier,
ce n’est pas la seul façon de faire mais c’est la façon que je trouve être la plus propre.

1
2
3
4
5
6
7
8
function getFileExtension($filename) {
   $filename = explode('.', $filename);
   if(count($filename) >= 2) {
       return $filename[count($filename)-1];
   } else {
       return false;
   }
}

Utilisation

$extension = getFileExtension('the_memory_remains.mp3');
echo $extension;

Ce qui donnera

mp3

PHP Tutorial d’une transformations XSLT en PHP5

Voici un petit tutorial interressant sur php/xsl/xslt ^^ pour voir le tutorial en entier on click sur la source tout en bas.

Nous allons voir dans cet article comment effectuer des transformationsXSLT en PHP 5. Alors que PHP 4 fournissait deux méthodes pour effectuer cette tache , l’extension xslt et l’extension domxml-xslt, PHP 5 ne fournit plus qu’une et une seule manière uniforme, basée sur la libxslt, la classeXSLTProcessor.
La compréhension générale de cet article suppose que vous disposiez de quelques bases en XSLT. Nous partirons néanmoins d’un exemple assez simple de transformation, auquel nous ajouterons progressivement des fonctionnalités, tout en découvrant les méthodes de la classeXSLTProcessor.

Source

PHP Sorti de php 5.3 RC1

The PHP development team is proud to announce the availability of the first release candidate of PHP 5.3.0 (PHP 5.3.0RC1). This release marks the final phase in a major improvement in the 5.X series, which includes a large number of new features, bug fixes and security enhancements.

Bon c’est en Anglais , mais c’est pas non plus du Shakespeare ^^.

Liste des evolutions majeurs:

This release also drops several extensions and unifies usage of internal APIs. Users should be aware of the following known backwards compatibility breaks:

Removed zend.ze1_compatibility_mode


Plus d’information sur php.net

et sur

phpdeveloper.org

et pour telecharger cette RC1 c’est là qa.php.net

PHP Lire un fichier texte

Comment lire un fichier texte en php ?

et bien comme ça ^^

$file = 'file.txt';
if(file_exists($file)) {
    $handle = fopen($file, 'r');
    while (!feof($handle)) {
        $line = fgets($handle, 4096);
        echo $line;
    }
    fclose($handle);
}

fopen, fgets

Bon voila rien de très compliqué et ca peu en aider certain.