functie-queue

Gesponsorde koppelingen

PHP script bestanden

  1. functie-queue

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php

// Load the queueCallback scheme from the repository.
require_once 'repository/queueCallback.php';

class queueFarm
{
    private static $queueFarm = null;
    
    private $queueIndex = array();
    private $queueCount = 0;
    private $waitTime   = 0.0;
    
    private function __construct()
    {
    }

    
    public function __destruct()
    {

        $this -> queueIndex = array();
        $this -> queueCount = 0;
    }

    
    private function __clone()
    {
    }

    
    public static function getInstance()
    {

        if( self :: $queueFarm == null )
        {

            self :: $queueFarm = new self();
        }

        
        return self :: $queueFarm;
    }

    
    public function queueCount()
    {

        return $this -> queueCount;
    }

    
    public function waitTime()
    {

        return $this -> waitTime;
    }

    
    public function attachCallback( queueCallback $queueCallback )
    {

        // Put it in our index.
        $this -> queueIndex [] = $queueCallback;
        
        // Increase the queue count.
        $this -> queueCount ++;
    }

    
    public function detachCallback( $callbackClass )
    {

        // We have queue classes in our index?
        if( $this -> queueCount < 1 )
        {

            return true;
        }

        
        // Fetch the class's name (if we have to) ..
        if( !is_string( $callbackClass ) && !get_class( $callbackClass ) )
        {

            throw new InvalidArgumentException( 'Callback class should be either a class or a string.' );
        }

        
        if( !is_string( $callbackClass ) )
        {

            $callbackClass = get_class( $callbackClass );
        }

        
        foreach( $this -> queueIndex as $queueId => $queueCallback )
        {

            // Fetch the parameters ..
            $paramList = $queueCallback -> getParams();
            
            // This is a class :: method() call?
            if( !isset( $paramList [ 'callback' ][ 1 ] ) )
            {

                continue;
            }

            
            // Is this a match?
            if( $paramList [ 'callback' ][ 0 ] == $callbackClass )
            {

                // Remove it from our list.
                unset( $this -> queueIndex [ $queueId ] );
                
                // Change the queue count.
                $this -> queueCount --;
            }
        }

        
        return true;
    }

    
    public function runCallbacks()
    {

        // We have things to run?
        if( $this -> queueCount < 1 )
        {

            return true;
        }

        
        // Current micro-time.
        $microTime = microtime( true );
        
        // Lowest known runtime.
        $lowestTime = 100.0;
        
        // Loop trought our element tree.
        foreach( $this -> queueIndex as $queueId => $queueCallback )
        {

            // Fetch callback's parameters ..
            $paramList = $queueCallback -> getParams();
            
            // Calculate the time left 'till execution ..
            $timeLeft = $paramList [ 'time' ] - $microTime;
            
            // Is that number negative?
            if( $timeLeft < 1 )
            {

                // Run the bitch ..
                self :: stackCall( $paramList );
                
                // Remove the item from our list ..
                unset( $this -> queueIndex [ $queueId ] );
                
                // Change the queue count.
                $this -> queueCount --;
                
                // Go to the next iteration.
                continue;
            }

            
            // We don't have to call it, but, is this timeLeft lower than the current known?
            if( $timeLeft < $lowestTime )
            {

                $lowestTime = $timeLeft;
            }
        }

        
        // Set the new wait time ..
        $this -> waitTime = $lowestTime;
        
        // Finished.
    }
    
    private static function stackCall( $paramList )
    {

        // Function call?
        if( !isset( $paramList [ 'callback' ][ 1 ] ) )
        {

            call_user_func_array( $paramList [ 'callback' ][ 0 ], $paramList [ 'params' ] );
        }
else
        {
            call_user_func_array( $paramList [ 'callback' ], $paramList [ 'params' ] );
        }
    }
}

[
/code]

[
code]
[
code]<?php
class queueCallback
{
    protected $paramList = array(
        'callback'  => null,
        'params'    => array(),
        'time'      => 0.0
    );
    
    protected function __construct( $callback )
    {

        $this -> paramList [  'callback' ] = $callback;
    }

    
    public function getParams()
    {

        return $this -> paramList;
    }

    
    public static function method()
    {

        $paramCount = func_num_args();
        $paramList  = func_get_args();
        
        switch( $paramCount )
        {
            case
1:
                if( !is_callable( $paramList [ 0 ] ) )
                {

                    throw new BadMethodCallException( 'Specified function is not callable.' );
                }

            break;
            
            case
2:
                if( !is_callable( array( $paramList [ 0 ], $paramList [ 1 ] ) ) )
                {

                    throw new BadMethodCallException( 'Please give a valid callback combination.' );
                }

            break;
            
            default:

                throw new UnexpectedValueException( 'Too many parameters given.' );
            break;
        }

        
        return new self( $paramList );
    }

    
    public function params()
    {

        $paramList = func_get_args();
        $this -> paramList [ 'params' ] = array_merge( $this -> paramList [ 'params' ], $paramList );
        
        return $this;
    }

    
    public function time( $microTime )
    {

        if( !is_float( $microTime ) )
        {

            throw new InvalidArgumentException( '1st parameter should be a float.' );
        }

        
        if( $microTime < 0.01 )
        {

            throw new InvalidArgumentException( '1st parameter should be > 0.01' );
        }

        
        $this -> paramList [ 'time' ]      = microtime( true ) + $microTime;
        
        return $this;
    }
}

[
/code]

En een voorbeeldscript:

[
code]
[
code]<?php

// Load the farm object.
require_once 'queueFarm.php';

$queueFarm = queueFarm :: getInstance();

// Create some tasks ..
$queueCallback1 = queueCallback :: method( 'strtolower' ) -> params( 'UPPERCASE' ) -> time( 1.0 );
$queueCallback2 = queueCallback :: method( 'strtoupper' ) -> params( 'lowercase' ) -> time( 2.0 );
$queueCallback3 = queueCallback :: method( 'substr' ) -> params( 'phphulp', 0, 3 ) -> time( 3.0 );

// Put them in our farm.
$queueFarm -> attachCallback( $queueCallback1 );
$queueFarm -> attachCallback( $queueCallback2 );
$queueFarm -> attachCallback( $queueCallback3 );

// Sleep 2 seconds ..
sleep( 2 );

// Process functions.
$queueFarm -> runCallbacks();

// Time 'till next function.
echo $queueFarm -> waitTime(); // weergeeft '1'

[/code]

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.