# Source for file Compiler.php

Documentation is available at Compiler.php

1. <?php
2. /*
3. * $Id: Compiler.php 1768 2007-06-19 22:55:34Z zYne $
4. *
5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16. *
17. * This software consists of voluntary contributions made by many individuals
18. * and is licensed under the LGPL. For more information, see
19. * <http://www.phpdoctrine.com>.
20. */
21. /**
22. * Doctrine_Compiler
23. * This class can be used for compiling the entire Doctrine framework into a single file
24. *
25. * @package Doctrine
26. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
28. * @category Object Relational Mapping
29. * @link www.phpdoctrine.com
30. * @since 1.0
31. * @version $Revision: 1768 $
32. */
33. class Doctrine_Compiler
34. {
35. /**
36. * method for making a single file of most used doctrine runtime components
37. * including the compiled file instead of multiple files (in worst
38. * cases dozens of files) can improve performance by an order of magnitude
39. *
40. * @throws Doctrine_Compiler_Exception if something went wrong during the compile operation
41. * @return void
42. */
43. public static function compile($target = null)
44. {
45. $path = Doctrine::getPath();
46. $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::LEAVES_ONLY);
48. foreach ($it as $file) {
49. $e = explode('.', $file->getFileName());
51. // we don't want to require versioning files
52. if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) {
53. require_once $file->getPathName();
54. }
55. }
57. $classes = array_merge(get_declared_classes(), get_declared_interfaces());
59. $ret = array();
61. foreach ($classes as $class) {
62. $e = explode('_', $class);
64. if ($e[0] !== 'Doctrine') {
65. continue;
66. }
67. $refl = new ReflectionClass($class);
68. $file = $refl->getFileName();
70. print 'Adding ' . $file . PHP_EOL;
72. $lines = file($file);
74. $start = $refl->getStartLine() - 1;
75. $end = $refl->getEndLine();
77. $ret = array_merge($ret, array_slice($lines, $start, ($end - $start)));
78. }
80. if ($target == null) {
81. $target = $path . DIRECTORY_SEPARATOR . 'Doctrine.compiled.php';
82. }
84. // first write the 'compiled' data to a text file, so
85. // that we can use php_strip_whitespace (which only works on files)
86. $fp = @fopen($target, 'w');
88. if ($fp === false) {
89. throw new Doctrine_Compiler_Exception("Couldn't write compiled data. Failed to open $target");
90. }
91. fwrite($fp, "<?php ". implode('', $ret));
92. fclose($fp);
94. $stripped = php_strip_whitespace($target);
95. $fp = @fopen($target, 'w');
96. if ($fp === false) {
97. throw new Doctrine_Compiler_Exception("Couldn't write compiled data. Failed to open $file");
98. }
99. fwrite($fp, $stripped);
100. fclose($fp);
101. }
102. }