domDoc class uses flock()

From: Date: Mon, 05 May 2003 11:20:15 +0000
Subject: domDoc class uses flock()
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-1258@lists.php.net to get a copy of this message
I'v already started developing an object wrapper to DOM XML for my own usage (but freely available to anyone as with all my stuff) which provides a central point where I can control the way all this file accessing is done. Here is the constructor method alone plus the constant definitions preceeding the class. This code has not been tested yet.
    /**
    * Constructor method
    *
    * Create the objDoc instance property and associated ndRoot property based
    * on the user-selected mode of document creation. In the process, establish
    * the blnIsReadOnly property.
    *
    * @param    mixed   information required to create a DOM document
    * @param    int     constant specifying how the document is to be created
    * @return   void
    */
    function domDoc(&$Starter,$intUse = DOMDOC_AS_NEW) {
        $this->objErr = new xmlErrorLog($this);
        $this->intMode = $intUse;
        $this->blnIsReadOnly = true;
                                    // for more info on each case block, see
                                    // comments in the constant definitions
                                    // at the top of this file.
        switch ($intUse) {
            case DOMDOC_AS_NEW:
                $this->blnIsReadOnly = false;
                $this->objDoc = domxml_new_doc("1.0");
                $elRoot = $this->objDoc->create_element($Starter);
                $this->ndRoot = $this->objDoc->append_child($elRoot);
            break;
            case DOMDOC_AS_READFILE:
                $fp = fopen($Starter,"r")
                    OR $this->objErr->throw(
                        "could not open ".$Starter." for reading."
                    );
                                    // attempt thread safety by using flock
                flock($fp,LOCK_SH);
                $xmlData = fread($fp,filesize($Starter));
                flock($fp,LOCK_UN);
                fclose($fp);
                                    // finish working with the file as
                                    // quickly as possible and THEN worry
                                    // about making a DOM doc from it.
                $this->_domOpenFromData($xmlData);
            break;
            case DOMDOC_AS_WRITEFILE:
                $this->blnIsReadOnly = false;
                $this->fp = fopen($Starter,"bw+")
                    OR $this->objErr->throw(
                        "could not open ".$Starter." for writing."
                    );
                flock($this->fp,LOCK_EX);
                $xmlData = fread($this->fp,filesize($Starter));
                $this->_domOpenFromData($xmlData);
            break;
            case DOMDOC_AS_REMOTEFILE:
                $fp = fopen($Starter,"r")
                    OR $this->objErr->throw(
                        "could not open ".$Starter." for reading."
                    );
                $xmlData = fread($fp,filesize($Starter));
                fclose($fp);
                $this->_domOpenFromData($xmlData);
            break;
            case DOMDOC_AS_REFERENCE:
                $this->objDoc =& $Starter;
            break;
            case DOMDOC_AS_DATA:
                $this->_domOpenFromData($Starter);
            break;
            default:
                $this->objErr->throw(
                    "second argument to domDoc constructor is invalid"
                );
        }
    }


« previous php.internals (#1258) next »