PHP Velho Oeste 2024

Exemple qui implémente inclued dans une application

Cet exemple montre comment implémenter inclued dans une application existante, et afficher les résultats.

Exemple #1 Traitement des informations dans une application PHP

<?php
// Fichier de stockage des informations d'inclusion
$fp fopen('/tmp/wp.ser''w');
if (
$fp) {
    
$clue inclued_get_data();
    if (
$clue) {
        
fwrite($fpserialize($clue));
    }
    
fclose($fp);
}
?>

Maintenant que nous avons des données, il est temps de prendre du recul et d'avoir un graphique. L'extension inclued inclut un fichier PHP appelé gengraph.php qui crée un fichier DOT, qui peut être passé à la bibliothèque » graphviz. Cependant, cette approche n'est pas obligatoire.

Exemple #2 Exemple d'utilisation de gengraph.php

Cet exemple crée une image appelée inclued.png qui montre les fichiers inclus.

# First, create the dot file
$ php gengraph.php -i /tmp/wp.ser -o wp.dot

# Next, create the image
$ dot -Tpng -o inclued.png wp.dot

Exemple #3 Lister les données depuis une capture inclued

En utilisant la directive inclued.dumpdir , des fichiers (les traces include) sont écrits à chaque requête. Voici un moyen de lister ces fichiers et utiliser unserialize() avec.

<?php
$path 
ini_get('inclued.dumpdir');
if (
$path && is_dir($path)) {

    echo 
"Path: $path"PHP_EOL;

    
$inclues = new GlobIterator($path DIRECTORY_SEPARATOR 'inclued*');

    if (
$inclues->count() === 0) {
        echo 
'Pas de traces'PHP_EOL;
        exit;
    }

    foreach (
$inclues as $inclue) {

        echo 
'Fichier Inclus: '$inclue->getFilename(), PHP_EOL;

        
$data file_get_contents($inclue->getPathname());
        if (
$data) {
            
$inc unserialize($data);
            echo 
' -- fichier: '$inc['request']['SCRIPT_FILENAME'], PHP_EOL;
            echo 
' -- nombre d\'inclusions: 'count($inc['includes']), PHP_EOL;
        }
        echo 
PHP_EOL;
    }
} else {
    echo 
'Pas de traces.'PHP_EOL;
}
?>

L'exemple ci-dessus va afficher quelque chose de similaire à :

PATH: /tmp/inclued
Fichier Inclus: inclued.56521.1
 -- fichier: /Users/philip/test.php
 -- nombre d'inclusions: 1

Fichier Inclus: inclued.56563.1
 -- fichier: /tmp/none.php
 -- nombre d'inclusions: 0

Fichier Inclus: inclued.56636.1
 -- fichier: /tmp/three.php
 -- nombre d'inclusions: 3

add a note add a note

User Contributed Notes 3 notes

up
1
g dot kuizinas at anuary dot com
11 years ago
If you install the module and execute it via CLI it will not produce any errors when not enabled. It took me quite a bit of time until I realised that by default the module is disabled, http://www.php.net/manual/en/inclued.configuration.php.
up
1
gtisza at gmail dot com
11 years ago
Note that inclued does not work correctly when xdebug is enabled (the includes array remains empty; consequently, graphing the classes works but graphing the files does not). Bug report: https://bugs.php.net/bug.php?id=59419
up
0
admin at eexit dot net
13 years ago
The new gengraph.php has « -t » parameter set to « includes » by default. Don't forget to specify it to « classes » for most of all projects.

Since PHP 5.3, namespace operators are stripped by Graphiz when the graph is generated. Add the following tips to escape namespace operators:

<?php
// Around line 254
else /* classes */
{
   
$filemap = array();

    foreach(
$data["classes"] as $k => $v) {
       
$class = $v;

   
// Here is the tip to remove namespaces
       
$class['name'] = str_replace('\\', '\\\\', $class['name']);
       
$class['mangled_name'] = str_replace('\\', '\\\\', $class['mangled_name']);

// ...
?>

By the way, if you are looking for an easy way to use inclued, take a look at my library: http://www.eexit.net/projects/inclued.html
To Top