DirectoryLocker.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: DirectoryLocker.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00018     final class DirectoryLocker extends BaseLocker
00019     {
00020         private $directory = null;
00021         
00022         public function __construct($directory = 'dir-locking/')
00023         {
00024             $this->directory = ONPHP_TEMP_PATH.$directory;
00025             
00026             if (!is_writable($this->directory)) {
00027                 if (!mkdir($this->directory, 0700, true)) {
00028                     throw new WrongArgumentException(
00029                         "can not write to '{$directory}'"
00030                     );
00031                 }
00032             }
00033         }
00034         
00035         public function get($key)
00036         {
00037             $mseconds = 0;
00038             
00039             while ($mseconds < 10000) {
00040                 try {
00041                     mkdir($this->directory.$key, 0700, false);
00042                     return $this->pool[$key] = true;
00043                 } catch (BaseException $e) {
00044                     // still exist
00045                     $mseconds += 200;
00046                     usleep(200);
00047                 }
00048             }
00049             
00050             return false;
00051         }
00052         
00053         public function free($key)
00054         {
00055             try {
00056                 return rmdir($this->directory.$key);
00057             } catch (BaseException $e) {
00058                 return false;
00059             }
00060         }
00061         
00062         public function drop($key)
00063         {
00064             return $this->free($key);
00065         }
00066     }
00067 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4