geoffrey (owner)

Revisions

gist: 221786 Download_button fork
public
Public Clone URL: git://gist.github.com/221786.git
Embed All Files: show embed
PHP #
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
<?php
 
class apify
{
  public
    $api_url_template = 'http://www.symfony-project.org/api/1_3/%class_name%#method_%method_name%',
    $class_name_callback = null,
    $method_name_callback = 'strtolower',
    $mask = '!`(sf[a-zA-Z]+)::([a-zA-Z]+)\(\)`!U';
 
  public function callback($matches)
  {
    $class_name = is_callable($this->class_name_callback) ? call_user_func($this->class_name_callback, $matches[1]) : $matches[1];
    $method_name = is_callable($this->method_name_callback) ? call_user_func($this->method_name_callback, $matches[2]) : $matches[2];
              
    $api_url = strtr($this->api_url_template, array(
      '%class_name%' => $class_name,
      '%method_name%' => $method_name,
    ));
 
    return sprintf('[%s](%s)', $matches[0], $api_url);
  }
 
  public function process($file)
  {
    $content = file_get_contents($file);
    $content = preg_replace_callback($this->mask, array($this, 'callback'), $content);
    file_put_contents($file, $content);
  }
}
 
$file = $argv[1];
 
if (!file_exists($file) || !is_readable($file))
{
  throw InvalidArgumentException(sprintf('File "%s" does not exist or is unreadable', $file));
}
 
$apify = new apify();
$apify->process($file);