File: zf.php

package info (click to toggle)
zendframework 1.12.9+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 133,584 kB
  • sloc: xml: 1,311,829; php: 570,173; sh: 170; makefile: 125; sql: 121
file content (625 lines) | stat: -rwxr-xr-x 19,632 bytes parent folder | download | duplicates (2)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#!/usr/bin/php5
<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Tool
 * @subpackage Framework
 * @copyright  Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id$
 */

/**
 * ZF
 *
 * @category   Zend
 * @package    Zend_Tool
 * @subpackage Framework
 * @copyright  Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class ZF
{

    /**
     * @var bool
     */
    protected $_clientLoaded = false;

    /**
     * @var string
     */
    protected $_mode = 'runTool';

    /**
     * @var array of messages
     */
    protected $_messages = array();

    /**
     * @var string
     */
    protected $_homeDirectory = null;

    /**
     * @var string
     */
    protected $_storageDirectory = null;

    /**
     * @var string
     */
    protected $_configFile = null;

    /**
     * main()
     *
     * @return void
     */
    public static function main()
    {
        $zf = new self();
        $zf->bootstrap();
        $zf->run();
    }

    /**
     * bootstrap()
     *
     * @return ZF
     */
    public function bootstrap()
    {
        // detect settings
        $this->_mode             = $this->_detectMode();
        $this->_homeDirectory    = $this->_detectHomeDirectory();
        $this->_storageDirectory = $this->_detectStorageDirectory();
        $this->_configFile       = $this->_detectConfigFile();

        // setup
        $this->_setupPHPRuntime();
        $this->_setupToolRuntime();
    }

    /**
     * run()
     *
     * @return ZF
     */
    public function run()
    {
        switch ($this->_mode) {
            case 'runError':
                $this->_runError();
                $this->_runInfo();
                break;
            case 'runSetup':
                if ($this->_runSetup() === false) {
                    $this->_runInfo();
                }
                break;
            case 'runInfo':
                $this->_runInfo();
                break;
            case 'runTool':
            default:
                $this->_runTool();
                break;
        }

        return $this;
    }

    /**
     * _detectMode()
     *
     * @return ZF
     */
    protected function _detectMode()
    {
        $arguments = $_SERVER['argv'];

        $mode = 'runTool';

        if (!isset($arguments[0])) {
            return $mode;
        }

        if ($arguments[0] == $_SERVER['PHP_SELF']) {
            $this->_executable = array_shift($arguments);
        }

        if (!isset($arguments[0])) {
            return $mode;
        }

        if ($arguments[0] == '--setup') {
            $mode = 'runSetup';
        } elseif ($arguments[0] == '--info') {
            $mode = 'runInfo';
        }

        return $mode;
    }


    /**
     * _detectHomeDirectory() - detect the home directory in a variety of different places
     *
     * @param bool $mustExist Should the returned value already exist in the file system
     * @param bool $returnMessages Should it log messages for output later
     * @return string
     */
    protected function _detectHomeDirectory($mustExist = true, $returnMessages = true)
    {
        $homeDirectory = null;

        $homeDirectory = getenv('ZF_HOME'); // check env var ZF_HOME
        if ($homeDirectory) {
            $this->_logMessage('Home directory found in environment variable ZF_HOME with value ' . $homeDirectory, $returnMessages);
            if (!$mustExist || ($mustExist && file_exists($homeDirectory))) {
                return $homeDirectory;
            } else {
                $this->_logMessage('Home directory does not exist at ' . $homeDirectory, $returnMessages);
            }
        }

        $homeDirectory = getenv('HOME'); // HOME environment variable

        if ($homeDirectory) {
            $this->_logMessage('Home directory found in environment variable HOME with value ' . $homeDirectory, $returnMessages);
            if (!$mustExist || ($mustExist && file_exists($homeDirectory))) {
                return $homeDirectory;
            } else {
                $this->_logMessage('Home directory does not exist at ' . $homeDirectory, $returnMessages);
            }

        }

        $homeDirectory = getenv('HOMEPATH');

        if ($homeDirectory) {
            $this->_logMessage('Home directory found in environment variable HOMEPATH with value ' . $homeDirectory, $returnMessages);
            if (!$mustExist || ($mustExist && file_exists($homeDirectory))) {
                return $homeDirectory;
            } else {
                $this->_logMessage('Home directory does not exist at ' . $homeDirectory, $returnMessages);
            }
        }

        $homeDirectory = getenv('USERPROFILE');

        if ($homeDirectory) {
            $this->_logMessage('Home directory found in environment variable USERPROFILE with value ' . $homeDirectory, $returnMessages);
            if (!$mustExist || ($mustExist && file_exists($homeDirectory))) {
                return $homeDirectory;
            } else {
                $this->_logMessage('Home directory does not exist at ' . $homeDirectory, $returnMessages);
            }
        }

        return false;
    }

    /**
     * _detectStorageDirectory() - Detect where the storage directory is from a variaty of possiblities
     *
     * @param bool $mustExist Should the returned value already exist in the file system
     * @param bool $returnMessages Should it log messages for output later
     * @return string
     */
    protected function _detectStorageDirectory($mustExist = true, $returnMessages = true)
    {
        $storageDirectory = false;

        $storageDirectory = getenv('ZF_STORAGE_DIR');
        if ($storageDirectory) {
            $this->_logMessage('Storage directory path found in environment variable ZF_STORAGE_DIR with value ' . $storageDirectory, $returnMessages);
            if (!$mustExist || ($mustExist && file_exists($storageDirectory))) {
                return $storageDirectory;
            } else {
                $this->_logMessage('Storage directory does not exist at ' . $storageDirectory, $returnMessages);
            }
        }

        $homeDirectory = ($this->_homeDirectory) ? $this->_homeDirectory : $this->_detectHomeDirectory(true, false);

        if ($homeDirectory) {
            $storageDirectory = $homeDirectory . '/.zf/';
            $this->_logMessage('Storage directory assumed in home directory at location ' . $storageDirectory, $returnMessages);
            if (!$mustExist || ($mustExist && file_exists($storageDirectory))) {
                return $storageDirectory;
            } else {
                $this->_logMessage('Storage directory does not exist at ' . $storageDirectory, $returnMessages);
            }
        }

        return false;
    }

    /**
     * _detectConfigFile() - Detect config file location from a variety of possibilities
     *
     * @param bool $mustExist Should the returned value already exist in the file system
     * @param bool $returnMessages Should it log messages for output later
     * @return string
     */
    protected function _detectConfigFile($mustExist = true, $returnMessages = true)
    {
        $configFile = null;

        $configFile = getenv('ZF_CONFIG_FILE');
        if ($configFile) {
            $this->_logMessage('Config file found environment variable ZF_CONFIG_FILE at ' . $configFile, $returnMessages);
            if (!$mustExist || ($mustExist && file_exists($configFile))) {
                return $configFile;
            } else {
                $this->_logMessage('Config file does not exist at ' . $configFile, $returnMessages);
            }
        }

        $homeDirectory = ($this->_homeDirectory) ? $this->_homeDirectory : $this->_detectHomeDirectory(true, false);
        if ($homeDirectory) {
            $configFile = $homeDirectory . '/.zf.ini';
            $this->_logMessage('Config file assumed in home directory at location ' . $configFile, $returnMessages);
            if (!$mustExist || ($mustExist && file_exists($configFile))) {
                return $configFile;
            } else {
                $this->_logMessage('Config file does not exist at ' . $configFile, $returnMessages);
            }
        }

        $storageDirectory = ($this->_storageDirectory) ? $this->_storageDirectory : $this->_detectStorageDirectory(true, false);
        if ($storageDirectory) {
            $configFile = $storageDirectory . '/zf.ini';
            $this->_logMessage('Config file assumed in storage directory at location ' . $configFile, $returnMessages);
            if (!$mustExist || ($mustExist && file_exists($configFile))) {
                return $configFile;
            } else {
                $this->_logMessage('Config file does not exist at ' . $configFile, $returnMessages);
            }
        }

        return false;
    }


    /**
     * _setupPHPRuntime() - parse the config file if it exists for php ini values to set
     *
     * @return void
     */
    protected function _setupPHPRuntime()
    {
        // set php runtime settings
        ini_set('display_errors', true);

        // support the changing of the current working directory, necessary for some providers
        $cwd = getenv('ZEND_TOOL_CURRENT_WORKING_DIRECTORY');
        if ($cwd != '' && realpath($cwd)) {
            chdir($cwd);
        }

        if (!$this->_configFile) {
            return;
        }
        $zfINISettings = parse_ini_file($this->_configFile);
        $phpINISettings = ini_get_all();
        foreach ($zfINISettings as $zfINIKey => $zfINIValue) {
            if (substr($zfINIKey, 0, 4) === 'php.') {
                $phpINIKey = substr($zfINIKey, 4);
                if (array_key_exists($phpINIKey, $phpINISettings)) {
                    ini_set($phpINIKey, $zfINIValue);
                }
            }
        }
    }

    /**
     * _setupToolRuntime() - setup the tools include_path and load the proper framwork parts that
     * enable Zend_Tool to work.
     *
     * @return void
     */
    protected function _setupToolRuntime()
    {

        $includePathPrepend = getenv('ZEND_TOOL_INCLUDE_PATH_PREPEND');
        $includePathFull = getenv('ZEND_TOOL_INCLUDE_PATH');

        // check if the user has not provided anything
        if (!($includePathPrepend || $includePathFull)) {
            if ($this->_tryClientLoad()) {
                return;
            }
        }

        // if ZF is not in the include_path, but relative to this file, put it in the include_path
        if ($includePathPrepend || $includePathFull) {
            if (isset($includePathPrepend) && ($includePathPrepend !== false)) {
                set_include_path($includePathPrepend . PATH_SEPARATOR . get_include_path());
            } elseif (isset($includePathFull) && ($includePathFull !== false)) {
                set_include_path($includePathFull);
            }
        }

        if ($this->_tryClientLoad()) {
            return;
        }

        $zfIncludePath['relativePath'] = dirname(__FILE__) . '/../library/';
        if (file_exists($zfIncludePath['relativePath'] . 'Zend/Tool/Framework/Client/Console.php')) {
            set_include_path(realpath($zfIncludePath['relativePath']) . PATH_SEPARATOR . get_include_path());
        }

        if (!$this->_tryClientLoad()) {
            $this->_mode = 'runError';
            return;
        }
    }

    /**
     * _tryClientLoad() - Attempt to load the Zend_Tool_Framework_Client_Console to enable the tool to run.
     *
     * This method will return false if its not loaded to allow the consumer to alter the environment in such
     * a way that it can be called again to try loading the proper file/class.
     *
     * @return bool if the client is actuall loaded or not
     */
    protected function _tryClientLoad()
    {
        $this->_clientLoaded = false;
        $fh = @fopen('Zend/Tool/Framework/Client/Console.php', 'r', true);
        if (!$fh) {
            return $this->_clientLoaded; // false
        } else {
            fclose($fh);
            unset($fh);
            include 'Zend/Tool/Framework/Client/Console.php';
            $this->_clientLoaded = class_exists('Zend_Tool_Framework_Client_Console');
        }

        return $this->_clientLoaded;
    }

    /**
     * _runError() - Output the error screen that tells the user that the tool was not setup
     * in a sane way
     *
     * @return void
     */
    protected function _runError()
    {

        echo <<<EOS

***************************** ZF ERROR ********************************
In order to run the zf command, you need to ensure that Zend Framework
is inside your include_path.  There are a variety of ways that you can
ensure that this zf command line tool knows where the Zend Framework
library is on your system, but not all of them can be described here.

The easiest way to get the zf command running is to give it the include
path via an environment variable ZEND_TOOL_INCLUDE_PATH or
ZEND_TOOL_INCLUDE_PATH_PREPEND with the proper include path to use,
then run the command "zf --setup".  This command is designed to create
a storage location for your user, as well as create the zf.ini file
that the zf command will consult in order to run properly on your
system.

Example you would run:

$ ZEND_TOOL_INCLUDE_PATH=/path/to/library zf --setup

Your are encourged to read more in the link that follows.

EOS;

    }

    /**
     * _runInfo() - this command will produce information about the setup of this script and
     * Zend_Tool
     *
     * @return void
     */
    protected function _runInfo()
    {
        echo 'Zend_Tool & CLI Setup Information' . PHP_EOL
           . '(available via the command line "zf --info")'
           . PHP_EOL;

        echo '   * ' . implode(PHP_EOL . '   * ', $this->_messages) . PHP_EOL;

        echo PHP_EOL;

        echo 'To change the setup of this tool, run: "zf --setup"';

        echo PHP_EOL;

    }

    /**
     * _runSetup() - parse the request to see which setup command to run
     *
     * @return void
     */
    protected function _runSetup()
    {
        $setupCommand = (isset($_SERVER['argv'][2])) ? $_SERVER['argv'][2] : null;

        switch ($setupCommand) {
            case 'storage-directory':
                $this->_runSetupStorageDirectory();
                break;
            case 'config-file':
                $this->_runSetupConfigFile();
                break;
            default:
                $this->_runSetupMoreInfo();
                break;
        }
    }

    /**
     * _runSetupStorageDirectory() - if the storage directory does not exist, create it
     *
     * @return void
     */
    protected function _runSetupStorageDirectory()
    {
        $storageDirectory = $this->_detectStorageDirectory(false, false);

        if (file_exists($storageDirectory)) {
            echo 'Directory already exists at ' . $storageDirectory . PHP_EOL
               . 'Cannot create storage directory.';
            return;
        }

        mkdir($storageDirectory);

        echo 'Storage directory created at ' . $storageDirectory . PHP_EOL;
    }

    /**
     * _runSetupConfigFile()
     *
     * @return void
     */
    protected function _runSetupConfigFile()
    {
        $configFile = $this->_detectConfigFile(false, false);

        if (file_exists($configFile)) {
            echo 'File already exists at ' . $configFile . PHP_EOL
               . 'Cannot write new config file.';
            return;
        }

        $includePath = get_include_path();

        $contents = 'php.include_path = "' . $includePath . '"';

        file_put_contents($configFile, $contents);

        $iniValues = ini_get_all();
        if ($iniValues['include_path']['global_value'] != $iniValues['include_path']['local_value']) {
            echo 'NOTE: the php include_path to be used with the tool has been written' . PHP_EOL
               . 'to the config file, using ZEND_TOOL_INCLUDE_PATH (or other include_path setters)' . PHP_EOL
               . 'is no longer necessary.' . PHP_EOL . PHP_EOL;
        }

        echo 'Config file written to ' . $configFile . PHP_EOL;
    }

    /**
     * _runSetupMoreInfo() - return more information about what can be setup, and what is setup
     *
     * @return void
     */
    protected function _runSetupMoreInfo()
    {
        $homeDirectory    = $this->_detectHomeDirectory(false, false);
        $storageDirectory = $this->_detectStorageDirectory(false, false);
        $configFile       = $this->_detectConfigFile(false, false);

        echo <<<EOS

ZF Command Line Tool - Setup
----------------------------

Current Paths (Existing or not):
    Home Directory: {$homeDirectory}
    Storage Directory: {$storageDirectory}
    Config File: {$configFile}

Important Environment Variables:
    ZF_HOME
        - the directory this tool will look for a home directory
        - directory must exist
    ZF_STORAGE_DIR
        - where this tool will look for a storage directory
        - directory must exist
    ZF_CONFIG_FILE
        - where this tool will look for a configuration file
    ZF_TOOL_INCLUDE_PATH
        - set the include_path for this tool to use this value
    ZF_TOOL_INCLUDE_PATH_PREPEND
        - prepend the current php.ini include_path with this value

Search Order:
    Home Directory:
        - ZF_HOME, then HOME (*nix), then HOMEPATH (windows)
    Storage Directory:
        - ZF_STORAGE_DIR, then {home}/.zf/
    Config File:
        - ZF_CONFIG_FILE, then {home}/.zf.ini, then {home}/zf.ini,
          then {storage}/zf.ini

Commands:
    zf --setup storage-directory
        - setup the storage directory, directory will be created
    zf --setup config-file
        - create the config file with some default values


EOS;
    }

    /**
     * _runTool() - This is where the magic happens, dispatch Zend_Tool
     *
     * @return void
     */
    protected function _runTool()
    {

        $configOptions = array();
        if (isset($this->_configFile) && $this->_configFile) {
            $configOptions['configOptions']['configFilepath'] = $this->_configFile;
        }
        if (isset($this->_storageDirectory) && $this->_storageDirectory) {
            $configOptions['storageOptions']['directory'] = $this->_storageDirectory;
        }

        // ensure that zf.php loads the Zend_Tool_Project features
        $configOptions['classesToLoad'] = 'Zend_Tool_Project_Provider_Manifest';

        $console = new Zend_Tool_Framework_Client_Console($configOptions);
        $console->dispatch();
    }

    /**
     * _logMessage() - Internal method used to log setup and information messages.
     *
     * @param string $message
     * @param bool   $storeMessage
     * @return void
     */
    protected function _logMessage($message, $storeMessage = true)
    {
        if (!$storeMessage) {
            return;
        }

        $this->_messages[] = $message;
    }


}

if (!getenv('ZF_NO_MAIN')) {
    ZF::main();
}