2015-02-10 22 views
-2

我指的是将die()函数用于除调试以外的其他功能。 这是一个“很好的工作”的情况,但这是不好的做法?在函数内部杀死一个脚本是不好的做法吗?

use Symfony\Component\Filesystem\Filesystem; 
use Symfony\Component\Filesystem\Exception\IOExceptionInterface; 

/** 
* This Command will help Cjw create a new demo site to start off 
* with the Multisite bundle. 
* 
* Class CreateSiteCommand 
* @package Cjw\GeneratorBundle\Command 
*/ 
class RemoveSiteCommand extends ContainerAwareCommand 
{ 
    private $vendor; //eg "Cjw" 
    private $fullBundleName; //eg "SiteCjwNetworkBundle" 
    private $fullBundleNameWithVendor; //eg "CjwSiteCjwNetworkBundle" 
    (more vars) 

    /** 
    * this function is used to configure the Symfony2 console commands 
    */ 
    protected function configure() 
    { 
     $this 
      ->setName('cjw:delete-site') 
      ->setDescription('Delete Cjw Site') 
      ->addOption('db_user', null, InputOption::VALUE_REQUIRED, 'If set, the database user will be shown on the instructions'); 
    } 

    /** 
    * This function executes the code after the command input has been validated. 
    * 
    * @param InputInterface $input gets the user input 
    * @param OutputInterface $output shows a message on the command line 
    * @return int|null|void 
    */ 
    protected function execute(InputInterface $input, OutputInterface $output) 
    { 
     // set optional db_username 

     $dialog = $this->getHelper('dialog'); 
     $reusable = new \Reusable(); 
     $fs = new Filesystem(); 
     $cmd = new \ColorOutput(); 

     //push only vendors into the vendors array 
     $vendors = $reusable->getFileList($reusable->getMainDirectory('src'),true); 


     //select a vendor from the array 
     $vendor = $dialog->select(
      $output, 
      'Select your vendor [1]: ', 
      $vendors, 
      1 
     ); 

     $bundles_in_vendor = $reusable->getFileList($reusable->getMainDirectory('src/'.$vendors[$vendor]),true); 

     //push bundles that start with 'Site' into array 
     $sites = array(); 

     foreach($bundles_in_vendor as $bundle) 
     { 
      if($reusable->startsWith($bundle,'Site')) 
      { 
       array_push($sites,$bundle); 
      } 
     } 

     $site_to_delete = $dialog->select(
      $output, 
      'Select site to remove: ', 
      $sites, 
      1 
     ); 

     $bundle_deletion_path = $reusable->getMainDirectory('src/'.$vendors[$vendor]).'/'.$sites[$site_to_delete]; 

     $are_you_sure = array('yes','no'); 
     $confirmation = $dialog->select(
      $output, 
      'Are you sure you want to delete: '.$sites[$site_to_delete], 
      $are_you_sure, 
      1 
     ); 

     if($are_you_sure[$confirmation] == 'yes') 
     { 
      echo $cmd->color('yellow','attempting to remove bundle in: '.$bundle_deletion_path); 
      $fs->remove($bundle_deletion_path); 

      //returns demo 
      $sitename = strtolower($sites[$site_to_delete]); 
      $sitename = substr($sitename,0,-6); 
      $sitename = substr($sitename,4); 
      $this->setRawSiteNameInput($sitename); 

      // returns acmedemo 
      $symlinkname = strtolower($vendors[$vendor].substr($sites[$site_to_delete],0,-6)); 
      $this->removeSymlinks($symlinkname,$this->getRawSiteNameInput()); 

      $this->createSetters($vendor,substr($sites[$site_to_delete],0,-6)); 

      $this->deleteEzPublishExtension(); 
      echo $this->getFullLegacyPath(); 

      echo $cmd->color('green','deletion process completed.'); 
     } 
     else 
     { 
      echo "deletion canceled"; 
      die(); 
     } 

     function_that_further_deletion_process(); 
    } 

这是一个Symfony2的控制台脚本,从一定的结构

+2

您在代码中没有功能。我认为它不起作用,或者当变量未定义时,您想将任何文本写入空白页? – panther 2015-02-10 10:36:02

+0

@panther我现在发布了整个代码,为了简单起见,我使用了另一个代码。 – user3531149 2015-02-10 10:40:52

+0

专门针对Symfony而言,这不是最佳实践,因为这可能会阻止某些事件侦听器被触发。 – Gerry 2015-02-10 11:58:43

回答

2

这是完全安全的,如果这是你的问题,因为PHP作为准解释语言没有留下任何痕迹或假象删除网站何时终止执行。

如果是好的做法是另一回事。我会说测试的目的很好,但你应该在最终代码中避免它。原因是它使代码很难维护。考虑别人潜入你的代码并试图理解逻辑。其中一个实际上必须通过所有的代码来绊倒这个细节。机会是一个不会,所以你的代码的行为似乎中断。

而是尝试做下列操作之一:

  • 抛出一个异常离开当前范围。这样的例外很可能会被调用范围所捕获并吞噬,但它是一种清晰可预测的返回方式。显然你应该记录这样的行为。

  • 返回一个明显超出范围的值,返回“正常”的值。因此,例如nullfalse而不是典型的值。这迫使调用范围检查返回值,但无论如何这是一个好习惯。

  • 重构您的代码,使其没有理由突然终止执行。

+0

我想你应该提到一种方法来优雅地杀死应用程序运行。通过设置自定义错误/异常处理程序或其他技术? – 2015-02-10 10:39:41

+0

事情是,当发现错误时,通常会使用异常,而不是在用户取消按需操作时使用异常。 – user3531149 2015-02-10 10:42:41

+0

@ user3531149我不同意,这是一个观点问题。异常不等于错误。它只意味着:发生了意想不到的事情。不多,不少。我经常使用异常来发送信息。它使代码易读和脆脆。显然你想为此使用自定义的异常类。 – arkascha 2015-02-10 10:44:20

0

你还没有告诉我们你的意图是与die ..因此,我们不能告诉你是否正在使用正确的工具...

die和它的代名词,exit都退出脚本。如果这就是你想要的,那很好。

对于正常操作,我倾向于使用exit,对于错误情况(无法正常恢复,例如:无法连接到数据库),我倾向于使用。我也使用die的包装,所以如果需要的话,我可以记录这样的事件。虽然两个命令都做同样的事情,但意图有所不同,我想在代码中表达这种意图。

在你的例子中,我会使用exit

+0

现在贴出代码 – user3531149 2015-02-10 10:40:22

+0

这是代码,而不是intenion。代码完成它所做的事情,不一定是你想象中的事情......但从上下文中,我认为你做的是正确的事情......打印错误消息,退出。 – 2015-02-10 10:42:57

相关问题