Skip to content

Instantly share code, notes, and snippets.

@nojimage
Created February 3, 2011 13:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nojimage/809469 to your computer and use it in GitHub Desktop.
Save nojimage/809469 to your computer and use it in GitHub Desktop.
CakePHP LnShell - create plugin webroot symlink
<?php
/**
* Create symlink plugins and themes webroot to APP/webroot/
*
* CakePHP 1.3
* PHP versions 5
*
* Copyright 2011, nojimage (http://php-tips.com/)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @version 1.0
* @author nojimage <nojimage at gmail.com>
* @copyright 2011 nojimage (http://php-tips.com/)
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @link  http://php-tips.com/
*
* =====
* Usage:
*
* in console
*
* cake ln all
*
* or
*
* cake ln search
*
* or
*
* cake ln plugin {plugin_name}
*
* or
*
* cake ln theme {theme_name}
*
*/
class LnShell extends Shell {
public $uses = array();
public function main() {
$this->help();
}
public function help() {
$head = "-----------------------------------------------\n";
$head .= "Usage: cake ln <command> <params1> <params2>...\n";
$head .= "-----------------------------------------------\n";
$head .= "Commands:\n";
$commands = array(
'all' => "all\n" .
"\t" . "create symlink app plugins and themes webroot to APP/webroot/.\n",
'plugin' => "plugin <plugin_name>\n" .
"\t" . "create symlink plugin webroot to APP/webroot/{plugin_name}.\n",
'theme' => "theme <theme_name>\n" .
"\t" . "create symlink theme webroot to APP/webroot/theme/{theme_name}.\n",
'search' => "search\n" .
"\t" . "list up plugins and themes webroot.\n",
'help' => "help [<command>]\n" .
"\t" . "Displays this help message, or a message on a specific command.",
);
$this->out($head);
if (!isset($this->args[0])) {
foreach ($commands as $cmd) {
$this->out("{$cmd}\n\n");
}
} elseif (isset($commands[strtolower($this->args[0])])) {
$this->out($commands[strtolower($this->args[0])] . "\n\n");
} else {
$this->out(sprintf(__("Command '%s' not found", true), $this->args[0]));
}
}
/**
*
*/
public function all() {
foreach ($this->_searchPlugin() as $pluginName) {
$this->out('plugin: ' . $pluginName . ' has webroot directory.');
$this->_pluginSymlink($pluginName);
}
foreach ($this->_searchTheme() as $themeName) {
$this->out('theme: ' . $themeName . ' has webroot directory.');
$this->_themeSymlink($themeName);
}
}
/**
*
*/
public function search() {
foreach ($this->_searchPlugin() as $pluginName) {
$this->out('plugin: ' . $pluginName . ' has webroot directory.');
}
foreach ($this->_searchTheme() as $themeName) {
$this->out('theme: ' . $themeName . ' has webroot directory.');
}
}
/**
*
*/
public function plugin() {
if (empty($this->args)) {
$this->out('please input plugin name!');
return;
}
$pluginName = Inflector::underscore($this->args[0]);
$this->_pluginSymlink($pluginName);
}
/**
*
*/
public function theme() {
if (empty($this->args)) {
$this->out('please input theme name!');
return;
}
$themeName = Inflector::underscore($this->args[0]);
$this->_themeSymlink($themeName);
}
/**
* create pluin symlink
*
* @param string $pluginName
*/
protected function _pluginSymlink($pluginName) {
// dist path check
$link = WWW_ROOT . $pluginName;
if (file_exists($link)) {
$this->out('distination already exists: ' . $link);
return;
}
$target = App::pluginPath($pluginName) . WEBROOT_DIR;
$this->_symlink($target, $link);
}
/**
* create theme symlink
*
* @param string $themeName
*/
protected function _themeSymlink($themeName) {
// dist path check
$link = WWW_ROOT . 'theme' . DS . $themeName;
if (file_exists($link)) {
$this->out('distination already exists: ' . $link);
return;
}
$target = App::themePath($themeName) . WEBROOT_DIR;
$this->_symlink($target, $link);
}
/**
* create symlink
*
* @param string $target
* @param string $link
*/
protected function _symlink($target, $link) {
if (!file_exists($target) || file_exists($link)) {
return;
}
$input = $this->in(sprintf('ln -s %s %s ?', $target, $link), array('y', 'n'), 'n');
if (strtolower($input) !== 'y') {
return;
}
// create symlink
if (symlink($target, $link)) {
$this->out('SUCCESS!');
} else {
$this->out('FAILURE!');
}
}
/**
* search all plugins
*
* @return array
*/
protected function _searchPlugin() {
$pluginPaths = App::path('plugins');
$Folder = new Folder();
$plugins = array();
foreach ($pluginPaths as $path) {
$Folder->path = $path;
list($pluginNames, $files) = $Folder->read();
foreach ($pluginNames as $pluginName) {
if (is_dir($path . $pluginName . DS . WEBROOT_DIR)) {
$plugins[] = $pluginName;
}
}
}
return $plugins;
}
/**
* search all themes
*
* @return array
*/
protected function _searchTheme() {
$viewPaths = App::path('views');
$Folder = new Folder();
$themes = array();
foreach ($viewPaths as $path) {
$Folder->path = $path . 'themed' . DS;
list($themeNames, $files) = $Folder->read();
foreach ($themeNames as $themeName) {
if (is_dir($Folder->path . $themeName . DS . WEBROOT_DIR)) {
$themes[] = $themeName;
}
}
}
return $themes;
}
}
@nojimage
Copy link
Author

support theme!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment