00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 define( 'EZ_PHPCREATOR_VARIABLE', 1 );
00076 define( 'EZ_PHPCREATOR_SPACE', 2 );
00077 define( 'EZ_PHPCREATOR_TEXT', 3 );
00078 define( 'EZ_PHPCREATOR_METHOD_CALL', 4 );
00079 define( 'EZ_PHPCREATOR_CODE_PIECE', 5 );
00080 define( 'EZ_PHPCREATOR_EOL_COMMENT', 6 );
00081 define( 'EZ_PHPCREATOR_INCLUDE', 7 );
00082 define( 'EZ_PHPCREATOR_VARIABLE_UNSET', 8 );
00083 define( 'EZ_PHPCREATOR_DEFINE', 9 );
00084 define( 'EZ_PHPCREATOR_VARIABLE_UNSET_LIST', 10 );
00085 define( 'EZ_PHPCREATOR_RAW_VARIABLE', 11 );
00086
00087 define( 'EZ_PHPCREATOR_VARIABLE_ASSIGNMENT', 1 );
00088 define( 'EZ_PHPCREATOR_VARIABLE_APPEND_TEXT', 2 );
00089 define( 'EZ_PHPCREATOR_VARIABLE_APPEND_ELEMENT', 3 );
00090
00091 define( 'EZ_PHPCREATOR_INCLUDE_ONCE', 1 );
00092 define( 'EZ_PHPCREATOR_INCLUDE_ALWAYS', 2 );
00093
00094 define( 'EZ_PHPCREATOR_METHOD_CALL_PARAMETER_VALUE', 1 );
00095 define( 'EZ_PHPCREATOR_METHOD_CALL_PARAMETER_VARIABLE', 2 );
00096
00097 class eZPHPCreator
00098 {
00099
00100
00101
00102 function eZPHPCreator( $dir, $file, $prefix = '', $options = array() )
00103 {
00104 $this->PHPDir = $dir;
00105 $this->PHPFile = $file;
00106 $this->FilePrefix = $prefix;
00107 $this->FileResource = false;
00108 $this->Elements = array();
00109 $this->TextChunks = array();
00110 $this->TemporaryCounter = 0;
00111 if ( isset( $options['spacing'] ) and ( $options['spacing'] == 'disabled') )
00112 {
00113 $this->Spacing = false;
00114 }
00115 else
00116 {
00117 $this->Spacing = true;
00118 }
00119
00120 if ( isset( $options['clustering'] ) )
00121 {
00122 $this->ClusteringEnabled = true;
00123 $this->ClusterFileScope = $options['clustering'];
00124 }
00125 else
00126 {
00127 $this->ClusteringEnabled = false;
00128 }
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 function addDefine( $name, $value, $caseSensitive = true, $parameters = array() )
00156 {
00157 $element = array( EZ_PHPCREATOR_DEFINE,
00158 $name,
00159 $value,
00160 $caseSensitive,
00161 $parameters );
00162 $this->Elements[] = $element;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 function addRawVariable( $name, $value )
00177 {
00178 $element = array( EZ_PHPCREATOR_RAW_VARIABLE, $name, $value );
00179 $this->Elements[] = $element;
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 function addVariable( $name, $value, $assignmentType = EZ_PHPCREATOR_VARIABLE_ASSIGNMENT,
00211 $parameters = array() )
00212 {
00213 $element = array( EZ_PHPCREATOR_VARIABLE,
00214 $name,
00215 $value,
00216 $assignmentType,
00217 $parameters );
00218 $this->Elements[] = $element;
00219 }
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 function addVariableUnset( $name,
00241 $parameters = array() )
00242 {
00243 $element = array( EZ_PHPCREATOR_VARIABLE_UNSET,
00244 $name,
00245 $parameters );
00246 $this->Elements[] = $element;
00247 }
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 function addVariableUnsetList( $list,
00269 $parameters = array() )
00270 {
00271 $element = array( EZ_PHPCREATOR_VARIABLE_UNSET_LIST,
00272 $list,
00273 $parameters );
00274 $this->Elements[] = $element;
00275 }
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287 function addSpace( $lines = 1 )
00288 {
00289 $element = array( EZ_PHPCREATOR_SPACE,
00290 $lines );
00291 $this->Elements[] = $element;
00292 }
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 function addText( $text )
00305 {
00306 $element = array( EZ_PHPCREATOR_TEXT,
00307 $text );
00308 $this->Elements[] = $element;
00309 }
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342 function addMethodCall( $objectName, $methodName, $methodParameters, $returnValue = false, $parameters = array() )
00343 {
00344 $element = array( EZ_PHPCREATOR_METHOD_CALL,
00345 $objectName,
00346 $methodName,
00347 $methodParameters,
00348 $returnValue,
00349 $parameters );
00350 $this->Elements[] = $element;
00351 }
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377 function addCodePiece( $code, $parameters = array() )
00378 {
00379 $element = array( EZ_PHPCREATOR_CODE_PIECE,
00380 $code,
00381 $parameters );
00382 $this->Elements[] = $element;
00383 }
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 function addComment( $comment, $eol = true, $whitespaceHandling = true, $parameters = array() )
00408 {
00409 $element = array( EZ_PHPCREATOR_EOL_COMMENT,
00410 $comment,
00411 array_merge( $parameters,
00412 array( 'eol' => $eol,
00413 'whitespace-handling' => $whitespaceHandling ) ) );
00414 $this->Elements[] = $element;
00415 }
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438 function addInclude( $file, $type = EZ_PHPCREATOR_INCLUDE_ONCE, $parameters = array() )
00439 {
00440 $element = array( EZ_PHPCREATOR_INCLUDE,
00441 $file,
00442 $type,
00443 $parameters );
00444 $this->Elements[] = $element;
00445 }
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460 function variableNameText( $variableName, $assignmentType, $variableParameters = array() )
00461 {
00462 $variableParameters = array_merge( array( 'is-reference' => false ),
00463 $variableParameters );
00464 $isReference = $variableParameters['is-reference'];
00465 $text = '$' . $variableName;
00466 if ( $assignmentType == EZ_PHPCREATOR_VARIABLE_ASSIGNMENT )
00467 {
00468 if ( $isReference )
00469 $text .= ' =& ';
00470 else
00471 $text .= ' = ';
00472 }
00473 else if ( $assignmentType == EZ_PHPCREATOR_VARIABLE_APPEND_TEXT )
00474 {
00475 $text .= ' .= ';
00476 }
00477 else if ( $assignmentType == EZ_PHPCREATOR_VARIABLE_APPEND_ELEMENT )
00478 {
00479 if ( $isReference )
00480 $text .= '[] =& ';
00481 else
00482 $text .= '[] = ';
00483 }
00484 return $text;
00485 }
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508 function variableText( $value, $column = 0, $iteration = 0, $maxIterations = 2 )
00509 {
00510 if ( isset( $this->Spacing) and !$this->Spacing )
00511 {
00512 return var_export( $value, true );
00513 }
00514
00515 if ( is_bool( $value ) )
00516 $text = ( $value ? 'true' : 'false' );
00517 else if ( is_null( $value ) )
00518 $text = 'null';
00519 else if ( is_string( $value ) )
00520 {
00521 $valueText = str_replace( array( "\\",
00522 "\"",
00523 "\$",
00524 "\n" ),
00525 array( "\\\\",
00526 "\\\"",
00527 "\\$",
00528 "\\n" ),
00529 $value );
00530 $text = "\"$valueText\"";
00531 }
00532 else if ( is_numeric( $value ) )
00533 $text = $value;
00534 else if ( is_object( $value ) )
00535 {
00536 if ( $maxIterations !== false and
00537 $iteration > $maxIterations )
00538 {
00539 $temporaryVariableName = $this->temporaryVariableName( 'obj' );
00540 $this->writeVariable( $temporaryVariableName, $value );
00541 $text = '$' . $temporaryVariableName;
00542 }
00543 else
00544 {
00545 $text = '';
00546 if ( method_exists( $value, 'serializedata' ) )
00547 {
00548 $serializeData = $value->serializeData();
00549 $className = $serializeData['class_name'];
00550 $text = "new $className(";
00551
00552 $column += strlen( $text );
00553 $parameters = $serializeData['parameters'];
00554 $variables = $serializeData['variables'];
00555
00556 $i = 0;
00557 foreach ( $parameters as $parameter )
00558 {
00559 if ( $i > 0 )
00560 {
00561 $text .= ",\n" . str_repeat( ' ', $column );
00562 }
00563 $variableName = $variables[$parameter];
00564 $variableValue = $value->$variableName;
00565 $keyText = " ";
00566
00567
00568 if ( $this and ( get_class( $this ) == 'ezphpcreator' ) )
00569 {
00570 $text .= $keyText . $this->variableText( $variableValue, $column + strlen( $keyText ), $iteration + 1, $maxIterations );
00571 }
00572 else
00573 {
00574 $text .= $keyText . eZPHPCreator::variableText( $variableValue, $column + strlen( $keyText ), $iteration + 1, $maxIterations );
00575 }
00576 ++$i;
00577 }
00578 if ( $i > 0 )
00579 $text .= ' ';
00580
00581 $text .= ')';
00582 }
00583 }
00584 }
00585 else if ( is_array( $value ) )
00586 {
00587 if ( $maxIterations !== false and
00588 $iteration > $maxIterations )
00589 {
00590 $temporaryVariableName = $this->temporaryVariableName( 'arr' );
00591 $this->writeVariable( $temporaryVariableName, $value );
00592 $text = '$' . $temporaryVariableName;
00593 }
00594 else
00595 {
00596 $text = 'array(';
00597 $column += strlen( $text );
00598 $valueKeys = array_keys( $value );
00599 $isIndexed = true;
00600 for ( $i = 0; $i < count( $valueKeys ); ++$i )
00601 {
00602 if ( $i !== $valueKeys[$i] )
00603 {
00604 $isIndexed = false;
00605 break;
00606 }
00607 }
00608 $i = 0;
00609 foreach ( $valueKeys as $key )
00610 {
00611 if ( $i > 0 )
00612 {
00613 $text .= ",\n" . str_repeat( ' ', $column );
00614 }
00615 $element =& $value[$key];
00616 $keyText = ' ';
00617 if ( !$isIndexed )
00618 {
00619 if ( is_int( $key ) )
00620 $keyText = $key;
00621 else
00622 $keyText = "\"" . str_replace( array( "\\",
00623 "\"",
00624 "\n" ),
00625 array( "\\\\",
00626 "\\\"",
00627 "\\n" ),
00628 $key ) . "\"";
00629 $keyText = " $keyText => ";
00630 }
00631
00632
00633 if ( $this and ( get_class( $this ) == 'ezphpcreator' ) )
00634 {
00635 $text .= $keyText . $this->variableText( $element, $column + strlen( $keyText ), $iteration + 1, $maxIterations );
00636 }
00637 else
00638 {
00639 $text .= $keyText . eZPHPCreator::variableText( $element, $column + strlen( $keyText ), $iteration + 1, $maxIterations );
00640 }
00641 ++$i;
00642 }
00643 if ( $i > 0 )
00644 $text .= ' ';
00645 $text .= ')';
00646 }
00647 }
00648 else
00649 $text = 'null';
00650 return $text;
00651 }
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663 function prependSpacing( $text, $spacing, $skipEmptyLines = true, $spacingString = " ", $splitString = "\n" )
00664 {
00665 if ( $spacing == 0 or !$this->Spacing )
00666 return $text;
00667 $textArray = explode( $splitString, $text );
00668 $newTextArray = array();
00669 foreach ( $textArray as $text )
00670 {
00671 if ( trim( $text ) != '' and $this->Spacing )
00672 $textLine = str_repeat( $spacingString, $spacing ) . $text;
00673 else
00674 $textLine = $text;
00675 $newTextArray[] = $textLine;
00676 }
00677 return implode( $splitString, $newTextArray );
00678 }
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688 function open( $atomic = false )
00689 {
00690
00691
00692 if ( $this->ClusteringEnabled )
00693 return true;
00694
00695 if ( !$this->FileResource )
00696 {
00697 if ( !file_exists( $this->PHPDir ) )
00698 {
00699 include_once( 'lib/ezfile/classes/ezdir.php' );
00700 $ini =& eZINI::instance();
00701 $perm = octdec( $ini->variable( 'FileSettings', 'StorageDirPermissions' ) );
00702 eZDir::mkdir( $this->PHPDir, $perm, true );
00703 }
00704 $path = $this->PHPDir . '/' . $this->PHPFile;
00705 $oldumask = umask( 0 );
00706 $pathExisted = file_exists( $path );
00707 if ( $atomic )
00708 {
00709 $this->isAtomic = true;
00710 $this->requestedFilename = $path;
00711 $uniqid = md5( uniqid( "ezp". getmypid(), true ) );
00712 $path .= ".$uniqid";
00713 $this->tmpFilename = $path;
00714 }
00715 $ini =& eZINI::instance();
00716 $perm = octdec( $ini->variable( 'FileSettings', 'StorageFilePermissions' ) );
00717 $this->FileResource = @fopen( $this->FilePrefix . $path, "w" );
00718 if ( !$this->FileResource )
00719 eZDebug::writeError( "Could not open file '$path' for writing, perhaps wrong permissions" );
00720 if ( $this->FileResource and
00721 !$pathExisted )
00722 chmod( $path, $perm );
00723 umask( $oldumask );
00724 }
00725 return $this->FileResource;
00726 }
00727
00728
00729
00730
00731 function close()
00732 {
00733
00734
00735 if ( $this->ClusteringEnabled )
00736 return;
00737
00738 if ( $this->FileResource )
00739 {
00740 fclose( $this->FileResource );
00741
00742 if ( $this->isAtomic )
00743 {
00744 include_once( 'lib/ezfile/classes/ezfile.php' );
00745 eZFile::rename( $this->tmpFilename, $this->requestedFilename );
00746 }
00747 $this->FileResource = false;
00748 }
00749 }
00750
00751
00752
00753
00754
00755 function exists()
00756 {
00757 $path = $this->PHPDir . '/' . $this->PHPFile;
00758 if ( !$this->ClusteringEnabled )
00759 return file_exists( $path );
00760
00761
00762
00763 require_once( 'kernel/classes/ezclusterfilehandler.php' );
00764 $fileHandler = eZClusterFileHandler::instance();
00765 return $fileHandler->fileExists( $path );
00766 }
00767
00768
00769
00770
00771
00772
00773
00774
00775 function canRestore( $timestamp = false )
00776 {
00777
00778
00779 $path = $this->PHPDir . '/' . $this->PHPFile;
00780
00781 if ( $this->ClusteringEnabled )
00782 {
00783 require_once( 'kernel/classes/ezclusterfilehandler.php' );
00784 $file = eZClusterFileHandler::instance( $path );
00785 $canRestore= $file->exists();
00786
00787 if ( $timestamp !== false and $canRestore )
00788 {
00789 $cacheModifierTime = $file->mtime();
00790 $canRestore = ( $cacheModifierTime >= $timestamp );
00791 }
00792
00793 return $canRestore;
00794 }
00795
00796 $canRestore = file_exists( $path );
00797 if ( $timestamp !== false and
00798 $canRestore )
00799 {
00800 $cacheModifierTime = filemtime( $path );
00801 $canRestore = ( $cacheModifierTime >= $timestamp );
00802 }
00803
00804 return $canRestore;
00805 }
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824 function restore( $variableDefinitions )
00825 {
00826
00827
00828 $returnVariables = array();
00829 $path = $this->PHPDir . '/' . $this->PHPFile;
00830
00831 if ( !$this->ClusteringEnabled )
00832 include( $path );
00833 else
00834 {
00835 require_once( 'kernel/classes/ezclusterfilehandler.php' );
00836 $file = eZClusterFileHandler::instance( $path );
00837 if ( $file->exists() )
00838 {
00839 $fetchedFilePath = $file->fetchUnique();
00840 include( $fetchedFilePath );
00841 $file->fileDeleteLocal( $fetchedFilePath );
00842
00843
00844
00845 }
00846 }
00847
00848 foreach ( $variableDefinitions as $variableReturnName => $variableName )
00849 {
00850 $variableRequired = true;
00851 $variableDefault = false;
00852 if ( is_array( $variableName ) )
00853 {
00854 $variableDefinition = $variableName;
00855 $variableName = $variableDefinition['name'];
00856 $variableRequired = $variableDefinition['required'];
00857 if ( isset( $variableDefinition['default'] ) )
00858 $variableDefault = $variableDefinition['default'];
00859 }
00860 if ( isset( ${$variableName} ) )
00861 {
00862 $returnVariables[$variableReturnName] = ${$variableName};
00863 }
00864 else if ( $variableRequired )
00865 eZDebug::writeError( "Variable '$variableName' is not present in cache '$path'",
00866 'eZPHPCreator::restore' );
00867 else
00868 $returnVariables[$variableReturnName] = $variableDefault;
00869 }
00870 return $returnVariables;
00871 }
00872
00873
00874
00875
00876 function store( $atomic = false )
00877 {
00878 if ( $this->open( $atomic ) )
00879 {
00880 $this->write( "<?php\n" );
00881
00882 $this->writeElements();
00883
00884 $this->write( "?>\n" );
00885
00886 $this->writeChunks();
00887 $this->flushChunks();
00888 $this->close();
00889
00890
00891 include_once( 'lib/ezutils/classes/ezlog.php' );
00892 eZLog::writeStorageLog( $this->PHPFile, $this->PHPDir . '/' );
00893 return true;
00894 }
00895 else
00896 {
00897 eZDebug::writeError( "Failed to open file '" . $this->PHPDir . '/' . $this->PHPFile . "'",
00898 'eZPHPCreator::store' );
00899 return false;
00900 }
00901 }
00902
00903
00904
00905
00906
00907 function fetch( $addPHPMarkers = true )
00908 {
00909 if ( $addPHPMarkers )
00910 $this->write( "<?php\n" );
00911 $this->writeElements();
00912 if ( $addPHPMarkers )
00913 $this->write( "?>\n" );
00914
00915 $text = implode( '', $this->TextChunks );
00916
00917 $this->flushChunks();
00918
00919 return $text;
00920 }
00921
00922
00923
00924
00925
00926
00927 function writeChunks()
00928 {
00929
00930
00931 $count = count( $this->TextChunks );
00932
00933 if ( $this->ClusteringEnabled )
00934 {
00935 $text = '';
00936 for ( $i = 0; $i < $count; ++$i )
00937 $text .= $this->TextChunks[$i];
00938
00939 $filePath = $this->FilePrefix . $this->PHPDir . '/' . $this->PHPFile;
00940
00941 require_once( 'kernel/classes/ezclusterfilehandler.php' );
00942 $fileHandler = eZClusterFileHandler::instance();
00943 $fileHandler->fileStoreContents( $filePath, $text, $this->ClusterFileScope, 'php' );
00944
00945 return;
00946 }
00947
00948 for ( $i = 0; $i < $count; ++$i )
00949 {
00950 $text = $this->TextChunks[$i];
00951 fwrite( $this->FileResource, $text );
00952 }
00953 }
00954
00955
00956
00957
00958 function flushChunks()
00959 {
00960 $this->TextChunks = array();
00961 }
00962
00963
00964
00965
00966 function write( $text )
00967 {
00968 $this->TextChunks[] = $text;
00969 }
00970
00971
00972
00973
00974 function writeElements()
00975 {
00976 $count = count( $this->Elements );
00977 for ( $i = 0; $i < $count; ++$i )
00978 {
00979 $element =& $this->Elements[$i];
00980 if ( $element[0] == EZ_PHPCREATOR_DEFINE )
00981 {
00982 $this->writeDefine( $element );
00983 }
00984 else if ( $element[0] == EZ_PHPCREATOR_RAW_VARIABLE )
00985 {
00986 $this->writeRawVariable( $element[1], $element[2] );
00987 }
00988 else if ( $element[0] == EZ_PHPCREATOR_VARIABLE )
00989 {
00990 $this->writeVariable( $element[1], $element[2], $element[3], $element[4] );
00991 }
00992 else if ( $element[0] == EZ_PHPCREATOR_VARIABLE_UNSET )
00993 {
00994 $this->writeVariableUnset( $element );
00995 }
00996 else if ( $element[0] == EZ_PHPCREATOR_VARIABLE_UNSET_LIST )
00997 {
00998 $this->writeVariableUnsetList( $element );
00999 }
01000 else if ( $element[0] == EZ_PHPCREATOR_SPACE )
01001 {
01002 $this->writeSpace( $element );
01003 }
01004 else if ( $element[0] == EZ_PHPCREATOR_TEXT )
01005 {
01006 $this->writeText( $element );
01007 }
01008 else if ( $element[0] == EZ_PHPCREATOR_METHOD_CALL )
01009 {
01010 $this->writeMethodCall( $element );
01011 }
01012 else if ( $element[0] == EZ_PHPCREATOR_CODE_PIECE )
01013 {
01014 $this->writeCodePiece( $element );
01015 }
01016 else if ( $element[0] == EZ_PHPCREATOR_EOL_COMMENT )
01017 {
01018 $this->writeComment( $element );
01019 }
01020 else if ( $element[0] == EZ_PHPCREATOR_INCLUDE )
01021 {
01022 $this->writeInclude( $element );
01023 }
01024 }
01025 }
01026
01027
01028
01029
01030 function writeDefine( $element )
01031 {
01032 $name = $element[1];
01033 $value = $element[2];
01034 $caseSensitive = $element[3];
01035 $parameters = $element[4];
01036 $text = '';
01037 if ( $this->Spacing )
01038 {
01039 $spacing = 0;
01040 if ( isset( $parameters['spacing'] ) )
01041 $spacing = $parameters['spacing'];
01042 $text = str_repeat( ' ', $spacing );
01043 }
01044 $nameText = $this->variableText( $name, 0 );
01045 $valueText = $this->variableText( $value, 0 );
01046 $text .= "define( $nameText, $valueText";
01047 if ( !$caseSensitive )
01048 $text .= ", true";
01049 $text .= " );\n";
01050 $this->write( $text );
01051 }
01052
01053
01054
01055
01056 function writeInclude( $element )
01057 {
01058 $includeFile = $element[1];
01059 $includeType = $element[2];
01060 $parameters = $element[3];
01061 if ( $includeType == EZ_PHPCREATOR_INCLUDE_ONCE )
01062 $includeName = 'include_once';
01063 else if ( $includeType == EZ_PHPCREATOR_INCLUDE_ALWAYS )
01064 $includeName = 'include';
01065 $includeFileText = $this->variableText( $includeFile, 0 );
01066 $text = "$includeName( $includeFileText );\n";
01067 if ( $this->Spacing )
01068 {
01069 $spacing = 0;
01070 if ( isset( $parameters['spacing'] ) )
01071 $spacing = $parameters['spacing'];
01072 $text = str_repeat( ' ', $spacing ) . $text;
01073 }
01074 $this->write( $text );
01075 }
01076
01077
01078
01079
01080 function writeComment( $element )
01081 {
01082 $elementAttributes = $element[2];
01083 $spacing = 0;
01084 if ( isset( $elementAttributes['spacing'] ) and $this->Spacing )
01085 $spacing = $elementAttributes['spacing'];
01086 $whitespaceHandling = $elementAttributes['whitespace-handling'];
01087 $eol = $elementAttributes['eol'];
01088 $newCommentArray = array();
01089 $commentArray = explode( "\n", $element[1] );
01090 foreach ( $commentArray as $comment )
01091 {
01092 $textLine = '// ' . $comment;
01093 if ( $whitespaceHandling )
01094 {
01095 $textLine = rtrim( $textLine );
01096 $textLine = str_replace( "\t", ' ', $textLine );
01097 }
01098 $textLine = str_repeat( ' ', $spacing ) . $textLine;
01099 $newCommentArray[] = $textLine;
01100 }
01101 $text = implode( "\n", $newCommentArray );
01102 if ( $eol )
01103 $text .= "\n";
01104 $this->write( $text );
01105 }
01106
01107
01108
01109
01110 function writeSpace( $element )
01111 {
01112 $text = str_repeat( "\n", $element[1] );
01113 $this->write( $text );
01114 }
01115
01116
01117
01118
01119 function writeCodePiece( $element )
01120 {
01121 $code = $element[1];
01122 $parameters = $element[2];
01123 $spacing = 0;
01124 if ( isset( $parameters['spacing'] ) and $this->Spacing )
01125 $spacing = $parameters['spacing'];
01126 $text = eZPHPCreator::prependSpacing( $code, $spacing );
01127 $this->write( $text );
01128 }
01129
01130
01131
01132
01133 function writeText( $element )
01134 {
01135 $text = $element[1];
01136 $this->write( "\n?>" );
01137 $this->write( $text );
01138 $this->write( "<?php\n" );
01139 }
01140
01141
01142
01143
01144 function writeMethodCall( $element )
01145 {
01146 $objectName = $element[1];
01147 $methodName = $element[2];
01148 $methodParameters = $element[3];
01149 $returnValue = $element[4];
01150 $parameters = $element[5];
01151 $text = '';
01152 $spacing = 0;
01153 if ( isset( $parameters['spacing'] ) and $this->Spacing )
01154 $spacing = $parameters['spacing'];
01155 if ( is_array( $returnValue ) )
01156 {
01157 $variableName = $returnValue[0];
01158 $assignmentType = EZ_PHPCREATOR_VARIABLE_ASSIGNMENT;
01159 if ( isset( $variableValue[1] ) )
01160 $assignmentType = $variableValue[1];
01161 $text = $this->variableNameText( $variableName, $assignmentType );
01162 }
01163 $text .= '$' . $objectName . '->' . $methodName . '(';
01164 $column = strlen( $text );
01165 $i = 0;
01166 foreach ( $methodParameters as $parameterData )
01167 {
01168 if ( $i > 0 )
01169 $text .= ",\n" . str_repeat( ' ', $column );
01170 $parameterType = EZ_PHPCREATOR_METHOD_CALL_PARAMETER_VALUE;
01171 $parameterValue = $parameterData[0];
01172 if ( isset( $parameterData[1] ) )
01173 $parameterType = $parameterData[1];
01174 if ( $parameterType == EZ_PHPCREATOR_METHOD_CALL_PARAMETER_VALUE )
01175 $text .= ' ' . $this->variableText( $parameterValue, $column + 1 );
01176 else if ( $parameterType == EZ_PHPCREATOR_METHOD_CALL_PARAMETER_VARIABLE )
01177 $text .= ' $' . $parameterValue;
01178 ++$i;
01179 }
01180 if ( $i > 0 )
01181 $text .= ' ';
01182 $text .= ");\n";
01183 $text = eZPHPCreator::prependSpacing( $text, $spacing );
01184 $this->write( $text );
01185 }
01186
01187
01188
01189
01190 function writeVariableUnset( $element )
01191 {
01192 $variableName = $element[1];
01193 $parameters = $element[2];
01194 $spacing = 0;
01195 if ( isset( $parameters['spacing'] ) and $this->Spacing )
01196 $spacing = $parameters['spacing'];
01197 $text = "unset( \$$variableName );\n";
01198 $text = eZPHPCreator::prependSpacing( $text, $spacing );
01199 $this->write( $text );
01200 }
01201
01202
01203
01204
01205 function writeVariableUnsetList( $element )
01206 {
01207 $variableNames = $element[1];
01208
01209 if ( count( $variableNames ) )
01210 {
01211 $parameters = $element[2];
01212 $spacing = 0;
01213 if ( isset( $parameters['spacing'] ) and $this->Spacing )
01214 $spacing = $parameters['spacing'];
01215 $text = 'unset( ';
01216 array_walk( $variableNames, create_function( '&$variableName,$key', '$variableName = "\$" . $variableName;') );
01217 $text .= join( ', ', $variableNames );
01218 $text .= " );\n";
01219 $text = eZPHPCreator::prependSpacing( $text, $spacing );
01220 $this->write( $text );
01221 }
01222 }
01223
01224
01225
01226
01227 function writeRawVariable( $variableName, $variableValue )
01228 {
01229 $this->write( "\${$variableName} = ". var_export( $variableValue, true). ";\n" );
01230 }
01231
01232
01233
01234
01235 function writeVariable( $variableName, $variableValue, $assignmentType = EZ_PHPCREATOR_VARIABLE_ASSIGNMENT,
01236 $variableParameters = array() )
01237 {
01238 $variableParameters = array_merge( array( 'full-tree' => false,
01239 'spacing' => 0 ),
01240 $variableParameters );
01241 $fullTree = $variableParameters['full-tree'];
01242 $spacing = $this->Spacing ? $variableParameters['spacing'] : 0;
01243 $text = $this->variableNameText( $variableName, $assignmentType, $variableParameters );
01244 $maxIterations = 2;
01245 if ( $fullTree )
01246 $maxIterations = false;
01247 $text .= $this->variableText( $variableValue, strlen( $text ), 0, $maxIterations );
01248 $text .= ";\n";
01249 $text = eZPHPCreator::prependSpacing( $text, $spacing );
01250 $this->write( $text );
01251 }
01252
01253
01254
01255
01256 function temporaryVariableName( $prefix )
01257 {
01258 $temporaryCounter =& $this->TemporaryCounter;
01259 $variableName = $prefix . '_' . $temporaryCounter;
01260 ++$temporaryCounter;
01261 return $variableName;
01262 }
01263
01264
01265 var $PHPDir;
01266 var $PHPFile;
01267 var $FileResource;
01268 var $Elements;
01269 var $TextChunks;
01270 var $isAtomic;
01271 var $tmpFilename;
01272 var $requestedFilename;
01273 var $Spacing = true;
01274 var $ClusteringEnabled = false;
01275 var $ClusterFileScope = false;
01276 }
01277 ?>