diff options
Diffstat (limited to 'zend/file.cpp')
-rw-r--r-- | zend/file.cpp | 59 |
1 files changed, 23 insertions, 36 deletions
diff --git a/zend/file.cpp b/zend/file.cpp index 2b7e77f..77b96f5 100644 --- a/zend/file.cpp +++ b/zend/file.cpp @@ -19,10 +19,10 @@ namespace Php { /** * Constructor - * + * * The constructor receives a filename as parameter. It uses the normal - * PHP include path resolve algorithms to find the location of the file. - * + * PHP include path resolve algorithms to find the location of the file. + * * @param name the filename * @param size length of the filename */ @@ -30,16 +30,9 @@ File::File(const char *name, size_t size) { // we need the tsrm_ls variable TSRMLS_FETCH(); - + // resolve the path _path = zend_resolve_path(name, size TSRMLS_CC); - - // the resolve-path function sometimes returns the original pointer, we - // do not want that because we may have to store the pathname in this object - if (_path != name) return; - - // make a full copy of the pathname - _path = estrndup(name, size); } /** @@ -48,10 +41,7 @@ File::File(const char *name, size_t size) File::~File() { // clean up path name - if (_path) efree(_path); - - // clean up opcodes - if (_opcodes) delete _opcodes; + if (_path) zend_string_release(_path); } /** @@ -62,10 +52,10 @@ bool File::compile() { // never works if the path is invalid if (!_path) return false; - + // is the file already compiled? if (_opcodes) return _opcodes->valid(); - + // we are going to open the file zend_file_handle fileHandle; @@ -73,20 +63,20 @@ bool File::compile() TSRMLS_FETCH(); // open the file - if (zend_stream_open(_path, &fileHandle TSRMLS_CC) == FAILURE) return false; + if (zend_stream_open(ZSTR_VAL(_path), &fileHandle TSRMLS_CC) == FAILURE) return false; + + // make sure the path name is stored in the handle (@todo: is this necessary? do we need the copy?) + if (!fileHandle.opened_path) fileHandle.opened_path = zend_string_copy(_path); - // make sure the path name is stored in the handle - if (!fileHandle.opened_path) fileHandle.opened_path = estrdup(_path); - // we need temporary compiler options CompilerOptions options(ZEND_COMPILE_DEFAULT TSRMLS_CC); - + // create the opcodes - _opcodes = new Opcodes(zend_compile_file(&fileHandle, ZEND_INCLUDE TSRMLS_CC) TSRMLS_CC); + _opcodes.reset(new Opcodes(zend_compile_file(&fileHandle, ZEND_INCLUDE TSRMLS_CC) TSRMLS_CC)); // close the file handle zend_destroy_file_handle(&fileHandle TSRMLS_CC); - + // done return _opcodes->valid(); } @@ -99,13 +89,13 @@ bool File::exists() { // it is of course not valid if the path could not be resolved if (!_path) return false; - + // if we have valid opcodes, we're sure that it exists if (_opcodes && _opcodes->valid()) return true; - + // retrieve stats struct stat buf; - return stat(_path, &buf) == 0; + return stat(ZSTR_VAL(_path), &buf) == 0; } /** @@ -122,7 +112,7 @@ bool File::valid() * Execute the file * @return Value */ -Value File::execute() +Value File::execute() { // do we already have the opcodes? if (_opcodes) return _opcodes->execute(); @@ -130,12 +120,9 @@ Value File::execute() // try compiling the file if (!compile()) return nullptr; - // we need the tsrm_ls variable (@todo would it be better if this was a member?) - TSRMLS_FETCH(); - // add the entry to the list of included files - zend_hash_add_empty_element(&EG(included_files), _path, ::strlen(_path) + 1); - + zend_hash_add_empty_element(&EG(included_files), _path); + // execute the opcodes return _opcodes->execute(); } @@ -144,16 +131,16 @@ Value File::execute() * Execute a file only once * @return Value */ -Value File::once() +Value File::once() { // skip if the path is invalid if (!_path) return nullptr; // we need the tsrm_ls variable (@todo would it be better if this was a member?) TSRMLS_FETCH(); - + // check if this file was already included - if (zend_hash_exists(&EG(included_files), _path, ::strlen(_path) + 1)) return nullptr; + if (zend_hash_exists(&EG(included_files), _path)) return nullptr; // execute the file return execute(); |