DirectoryLocker.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2008 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 
00017     final class DirectoryLocker extends BaseLocker
00018     {
00019         private $directory = null;
00020         
00021         protected function __construct($directory = 'dir-locking/')
00022         {
00023             $this->directory = ONPHP_TEMP_PATH.$directory;
00024             
00025             if (!is_writable($this->directory)) {
00026                 if (!mkdir($this->directory, 0700, true)) {
00027                     throw new WrongArgumentException(
00028                         "can not write to '{$directory}'"
00029                     );
00030                 }
00031             }
00032         }
00033         
00034         public function get($key)
00035         {
00036             $mseconds = 0;
00037             
00038             while ($mseconds < 10000) {
00039                 try {
00040                     mkdir($this->directory.$key, 0700, false);
00041                     return $this->pool[$key] = true;
00042                 } catch (BaseException $e) {
00043                     // still exist
00044                     unset($e);
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 ?>