sfBugTrackerPublishAssetsTask.class.php 2.71 KB
Newer Older
Игорь's avatar
init    
Игорь committed
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
<?php

/**
 * Publishes Web Assets for sfBugTracker plugin
 *
 * @package    symfony
 * @subpackage task
 * @author
 * @version    SVN: $Id: sfBugTrackerPublishAssetsTask.class.php 23922 2018-08-02 20:23:38Z fabien $
 */
class sfBugTrackerPublishAssetsTask extends sfBaseTask
{
  /**
   * @see sfTask
   */
  protected function configure()
  {
    $this->namespace = 'bugtracker';
    $this->name = 'publish-assets';

    $this->briefDescription = 'Publishes web assets for sfBugTracker plugin';

    $this->addOptions(array(
      new sfCommandOption('name', null, sfCommandOption::PARAMETER_OPTIONAL, 'Publish the plugin as', null)
    ));
  }

  /**
   * @see sfTask
   */
  protected function execute($arguments = array(), $options = array())
  {
    $plugin = 'sfBugTrackerPlugin';
    $enabledPlugins = $this->configuration->getPlugins();

    if ($diff = array_diff(array($plugin), $enabledPlugins))
    {
      throw new InvalidArgumentException('Plugin(s) not found: '.join(', ', $diff));
    }

    $pluginConfiguration = $this->configuration->getPluginConfiguration($plugin);

    $this->logSection('plugin', 'Configuring plugin - '.$plugin);
    $this->installPluginAssets($plugin, $pluginConfiguration->getRootDir(), $options['name']);

  }

  /**
   * Installs web content for a plugin.
   *
   * @param string $plugin The plugin name
   * @param string $dir    The plugin directory
   */
  protected function installPluginAssets($plugin, $dir, $name)
  {
    $pluginWebDir = $dir.DIRECTORY_SEPARATOR.'web';
    $webDir = sfConfig::get('sf_web_dir');

    if (is_dir($pluginWebDir))
    {
      foreach( $this->scandir($pluginWebDir) as $path ){
        $lpath = str_replace($pluginWebDir, '', $path);
        $dpath = dirname($lpath);
        $fname = basename($lpath);
        $linkpath = $webDir.$dpath.($name?DIRECTORY_SEPARATOR.$name:'');
        $linkname = $linkpath.DIRECTORY_SEPARATOR.$fname;
        if( $name ){
          if( !file_exists($linkpath) )
            $this->getFilesystem()->relativeSymlink($pluginWebDir.$dpath, $linkpath, true);
        }else if( !file_exists($linkname) ){
          $linkdir = dirname($linkname);
          if( !file_exists($linkdir) )
            mkdir($linkdir, 0777, true);
          $this->getFilesystem()->relativeSymlink($path, $linkname, true);
        }
      }
    }
  }
  protected function scandir($dir)
  {
    $paths = array();
    foreach( scandir($dir) as $fname ){
      if( $fname[0] == '.' )
        continue;
      $next = $dir.DIRECTORY_SEPARATOR.$fname;
      if( is_link($next) )
        $next = readlink($next);
      if( is_dir($next) ){
        $paths = array_merge($paths, $this->scandir($next));
      }else if( is_file($next) ){
        $paths[] = $next;
      }
    }
    return $paths;
  }
}