Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ShinichiU/2849812 to your computer and use it in GitHub Desktop.
Save ShinichiU/2849812 to your computer and use it in GitHub Desktop.
symfony アプリケーションのアクションの一覧を出すやつ
<?php
/**
* This file is part of the OpenPNE package.
* (c) OpenPNE Project (http://www.openpne.jp/)
*
* For the full copyright and license information, please view the LICENSE
* file and the NOTICE file that were distributed with this source code.
*/
/**
* openpneActionListTask
*
* @package OpenPNE
* @subpackage task
* @author Kousuke Ebihara <[email protected]>
*/
class openpneActionListTask extends sfBaseTask
{
protected function configure()
{
$this->addArguments(array(
new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
));
$this->addOptions(array(
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
new sfCommandOption('component', null, sfCommandOption::PARAMETER_NONE, 'Is component?', null),
));
$this->namespace = 'openpne';
$this->name = 'action-list';
$this->briefDescription = 'action list wo haku';
$this->detailedDescription = <<<EOF
ganbare
component ga hoshii baai ha --component tukeroyo
EOF;
}
protected function execute($arguments = array(), $options = array())
{
$actionList = array();
$app = $arguments['application'];
$moduleBaseDirs = array();
foreach ($this->configuration->getPluginPaths() as $path)
{
$moduleBaseDirs[] = $path.'/modules';
$moduleBaseDirs[] = $path.'/apps/'.$app.'/modules';
}
$moduleBaseDirs[] = $this->configuration->getSymfonyLibDir().'/controller';
$moduleBaseDirs[] = sfConfig::get('sf_app_module_dir');
$moduleDirs = sfFinder::type('dir')->maxdepth(0)->in($moduleBaseDirs);
$actionDirs = array();
foreach ($moduleDirs as $moduleDir)
{
$actionDirs[] = $moduleDir.'/actions';
}
$actions = $this->getActionsDirectories($actionDirs, $options['component']);
foreach ($actions as $action)
{
$className = preg_replace('/[^a-zA-Z1-9]/', '_', $action);
$classString = file_get_contents($action);
eval('?>'.preg_replace('/class [^ ]+/', 'class '.$className, $classString, 1));
if (preg_match('#.*modules/([^/]+)/.*?$#', $action, $matches))
{
$module = $matches[1];
}
else
{
$module = 'default';
}
$r = new ReflectionClass($className);
if ($this->isSingleAction($r, $options['component'])) // single action
{
$actionList[] = array(
$action,
$module.'/'.$this->getActionNameFromFilePath($action, $options['component']),
);
}
else
{
$methods = $r->getMethods(ReflectionMethod::IS_PUBLIC);
foreach ($methods as $key => $method)
{
if ('execute' !== $method->name && 0 === strpos($method->name, 'execute'))
{
$prefix = ($options['component']) ? '_' : '';
$actionName = substr($method->name, strlen('execute'));
$actionName[0] = strtolower($actionName[0]);
$actionList[] = array(
$action,
$module.'/'.$prefix.$actionName,
);
}
}
}
}
foreach ($actionList as $item)
{
if (preg_match('#.*plugins/([^/]+Plugin)/.*?$#', $item[0], $matches))
{
$plugin = $matches[1];
}
else
{
$plugin = 'core';
}
echo $item[1]."\t".$plugin.PHP_EOL;
}
}
protected function getActionsDirectories($dirs, $isComponent = false)
{
if ($isComponent)
{
$targets = array('components.class.php', '*Component.class.php');
}
else
{
$targets = array('actions.class.php', '*Action.class.php');
}
return sfFinder::type('file')->name($targets)->maxdepth(0)->in($dirs);
}
protected function getActionNameFromFilePath($filepath, $isComponent = false)
{
$prefix = '';
if ($isComponent)
{
$prefix = '_';
$suffix = 'Component.class.php';
}
else
{
$suffix = 'Action.class.php';
}
return $prefix.substr(basename($filepath), 0, -strlen($suffix));
}
protected function isSingleAction(ReflectionClass $r, $isComponent = false)
{
return ($isComponent) ? ($r->isSubclassOf('sfComponent') && !$r->isSubclassOf('sfAction') && !$r->isSubclassOf('sfComponents')) : !$r->isSubclassOf('sfActions');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment