Pluf Framework

Pluf Framework Git Source Tree


Root/src/Pluf/Translation/Generator.php

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * Extract all the strings from the template folders and create
 * 'template'.php files with the strings to be translated.
 */
class Pluf_Translation_Generator
{
    public $outputdir = '';
 
 
    /**
     * Recursive listing of the content of a folder.
     *
     * @credits http://php.net/dir
     *
     * @param string Path to browse
     * @param int How deep to browse (-1=unlimited)
     * @param string Mode "FULL"|"DIRS"|"FILES" ('FULL')
     * @param sring Pattern to exclude some files ('')
     * @param int Must not be defined (0)
     * @return array List of files
     */
    public static function list_dir($path, $maxdepth=-1, $mode='FULL', $exclude='', $d=0)
    {
        if (substr($path, strlen($path)-1) != '/') {
            $path .= '/';
        }    
        $dirlist = array();
        if ($mode != 'FILES') {
            $dirlist[] = $path;
        }
        if ($handle = opendir($path)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != '.' && $file != '..'
                    && ($exclude == '' or !preg_match($exclude, $file))) {
                    $file = $path.$file;
                    if (!is_dir($file)) {
                        if ($mode != 'DIRS') {
                            $dirlist[] = $file;
                        }
                    } elseif ($d >=0 && ($d < $maxdepth || $maxdepth < 0)) {
                        $result = self::list_dir($file.'/', $maxdepth, $mode,
                                           $exclude, $d+1);
                        $dirlist = array_merge($dirlist, $result);
                    }
                }
            }
            closedir($handle);
        }
        if ($d == 0) {
            natcasesort($dirlist);
        }
        return $dirlist;
    }
 
 
    /**
     * Recursive make of a directory.
     *
     * @credits http://php.net/mkdir
     *
     * @param string Directory to make
     * @param octal Chmod of the directory (0777)
     * @return bool Success
     */
    public static function rmkdir($dir, $mode=0777)
    {
        if (is_dir($dir) || @mkdir($dir, $mode)) return true;
        if (!self::rmkdir(dirname($dir), $mode)) return false;
        return @mkdir($dir, $mode);
    }
 
 
    public static function is_pathrelative($dir)
    {
        if (strtoupper(substr(PHP_OS,0,3) == 'WIN')) {
            return (preg_match('/^\w+:/', $dir) <= 0);
        } else {
            return (preg_match('/^\//', $dir) <= 0);
        }
    }
 
    public static function unifypath($path)
    {
        if (strtoupper(substr(PHP_OS,0,3) == 'WIN')) {
            return str_replace('\\', DIRECTORY_SEPARATOR, $path);
        }
        return $path;
    }
 
 
    public static function real_path($path)
    {
        $_path = self::unifypath($path);
        if (self::is_pathrelative($path)) {
            $_curdir = self::unifypath(realpath('.'));
            $_path = $_curdir.$_path;
        }
        $_startPoint = '';
        if (strtoupper(substr(PHP_OS,0,3) == 'WIN')) {
            list($_startPoint, $_path) = explode(':', $_path, 2);
            $_startPoint .= ':';
        }
        // From now processing is the same for WIndows and Unix,
        // and hopefully for others.
        $_realparts = array();
        $_parts = explode(DIRECTORY_SEPARATOR, $_path);
        for ($i=0; $i<count($_parts); $i++) {
            if (strlen($_parts[$i]) == 0 || $_parts[$i] == '.') {
                continue;
            }
            if ($_parts[$i] == '..') {
                if (count($_realparts) > 0) {
                    array_pop($_realparts);
                }
            } else {
                array_push($_realparts, $_parts[$i]);
            }
        }
        return $_startPoint.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $_realparts);
    }
 
 
    /**
     * Generate the files.
     */
    public function generate($outputdir)
    {
        $dest_files = array();
        foreach (Pluf::f('template_folders') as $folder) {
            $src_files = self::list_dir($folder, -1, 'FULL');
            foreach ($src_files as $file) {
                // Build an associative array where the key is the
                // source file and the value is the destination file.
                $dest_files[$file] = str_replace($folder, $outputdir, $file);
            }
        }
        foreach ($dest_files as $src=>$dest) {
            if (is_dir($src)) {
                self::rmkdir($dest);
                @chmod($dest, 0775);
                print 'Create folder: '.$dest."\n";
                continue;
            }
            self::rmkdir(dirname($dest));
            print 'Parse to: '.$dest.'.php';
            $compiler = new Pluf_Translation_TemplateExtractor(self::real_path($src), array(''), true);
            $content = $compiler->compile();
            $file = fopen($dest.'.php', 'wb');
            fwrite($file, $content);
            fclose($file);
            @chmod($dest.'.php', 0664);
            print ' - Ok'."\n";
            continue;
        }
    }
}

Archive Download this file

Branches

Tags

Number of commits:
Page rendered in 0.09211s using 11 queries.