XaeroDegreaz Posted June 7, 2009 Share Posted June 7, 2009 Hi, thanks for taking the time to check out this post. To make a long story short, I'm taking input from the command line, and using that input to make a decision, and then load another class based on that decision. For some reason it keeps exiting, without loading the class and without error: This is the first part of the code: $xml = simplexml_load_file("Config.xml"); $em = new ExtensionManager(); $extension = $em->ScanExtensions((string)$xml->default_extension); echo("This text will not appear"); //# This seems to call, but it exits immediately afterwards and it SHOULDN'T because inside is an infinite loop. $e = new $extenstion(); Here is the ScanExtension method: public function ScanExtensions($defaultExtension) { Logger::log(__CLASS__, "Listing valid extensions..."); if ($handle = opendir('./Extensions')) { echo("*********************************************\n"); $i = 1; $list = array(); while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && is_dir("./Extensions/$file")) { if(file_exists("./Extensions/$file/$file.php")) { echo("[$i]\t$file\n"); $list[] = $file; $i++; } } } closedir($handle); echo("*********************************************\n"); } $out = fopen("php://stdout", "w"); $in = fopen("php://stdin", "r"); fwrite($out, "Which project should I launch? [blank = $defaultExtension]: "); $ProjectName = trim(fgets($in)); fclose($out); fclose($in); if($ProjectName > 0) { //# This text will not appear.. Logger::log is a simple echo class/method I have created Logger::log(__CLASS__, "Loading extension ".$list[$ProjectName-1]."."); //sleep(3); $ext = $list[$ProjectName-1]; }else { //# Neither will this logger output appear. Logger::log(__CLASS__, "Loading default extension $defaultExtension."); //sleep(3); $ext = $defaultExtension; } return $ext; } This method goes through everything fine. It accepts user input and seems to return the correct string stored in $ext (I know it is working because I purposely made some errors in the class it should call, and it appears in my log file) It just simply exits without any errors at all! I have error_reporting set to E_ALL. There is nothing wrong with the class file that gets called, because I can launch it just fine by commenting out the call to the ScanExtension method, and typing $e = new Example() in the first block of outlined code above. I'm not sure what's going on. I have tried taking the ScanExtension code from the method and just placing it beneath the first code block outlined above, and I get the same results. Any help at all would be greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/161240-solved-cli-closes-without-error-after-inputting-text-with-sdtin/ Share on other sites More sharing options...
XaeroDegreaz Posted June 7, 2009 Author Share Posted June 7, 2009 Also, I have noted the typo above ($extenstion) but it doesn't make a difference in the code, since it is never even executed. I have changed it in the code and still nothing. Link to comment https://forums.phpfreaks.com/topic/161240-solved-cli-closes-without-error-after-inputting-text-with-sdtin/#findComment-850802 Share on other sites More sharing options...
XaeroDegreaz Posted June 7, 2009 Author Share Posted June 7, 2009 Left the front page, bumpskies! Link to comment https://forums.phpfreaks.com/topic/161240-solved-cli-closes-without-error-after-inputting-text-with-sdtin/#findComment-850855 Share on other sites More sharing options...
Ken2k7 Posted June 7, 2009 Share Posted June 7, 2009 When you run it, what does it output? How far into ScanExtensions does it run up to? Link to comment https://forums.phpfreaks.com/topic/161240-solved-cli-closes-without-error-after-inputting-text-with-sdtin/#findComment-850861 Share on other sites More sharing options...
XaeroDegreaz Posted June 7, 2009 Author Share Posted June 7, 2009 It runs all the way throgh to the return statement, but it seems to dislike outputing code into the console in that method, for some reason. This project is actually pretty large and I have been coding it myself for several months now. I just decided for my next release that I would like to have the startup a little more interactive by allowing users to create a project through the console (which oddly enough I'm using stdin for user input control and it works for the project creator) and to also allow them to choose which extension they would like to run. It's open source and available at https://launchpad.net/smartsocket However, the current additions that I am doing are not yet pushed to the repository. Link to comment https://forums.phpfreaks.com/topic/161240-solved-cli-closes-without-error-after-inputting-text-with-sdtin/#findComment-850865 Share on other sites More sharing options...
XaeroDegreaz Posted June 7, 2009 Author Share Posted June 7, 2009 I trouble shooted all the way through the code and finally came to the conclusion that stdout was the culprit. After juggling around the code, I can get it to work if I don't fclose stdout. It seems as though you cannot close stdout if you are gonna echo more text to screen? Is that what it is?? $out = fopen("php://stdout", "w"); $in = fopen("php://stdin", "r"); fwrite($out, "Which project should I launch? [blank = $defaultExtension]: "); $ProjectName = trim(fgets($in)); //# This Logger will print to screen just fine Logger::log(__CLASS__, "Testing: $ProjectName / ".$list[(int)$ProjectName]); fclose($out); //fclose($in); //# If I put the logger here, however, the CLI will quit. It is a simple echoing class, outlined below. //Logger::log(__CLASS__, "Testing: $ProjectName / ".$list[(int)$ProjectName]); $tst = new $list[(int)$ProjectName](); Logger.php <?php Class Logger { static public function log($class, $message, $die=false) { if($die) { print("# $class (FATAL): $message\n"); while(true){}; exit(); } print("# $class\t: $message\n"); } } ?> See, nothing fancy. I'm happy to have found the cause of my original problem, but now I'm faced with another problem. It takes a few seconds to close the CLI on my own now. It used to be instant. I assume because the CLI is trying to close STDOUT? Is there a way past this? Link to comment https://forums.phpfreaks.com/topic/161240-solved-cli-closes-without-error-after-inputting-text-with-sdtin/#findComment-850916 Share on other sites More sharing options...
Daniel0 Posted June 7, 2009 Share Posted June 7, 2009 There is no need to open/close the standard input/output. They're available through the built-in constants STDIN and STDOUT (there is also STDERR if you need it). Link to comment https://forums.phpfreaks.com/topic/161240-solved-cli-closes-without-error-after-inputting-text-with-sdtin/#findComment-850920 Share on other sites More sharing options...
XaeroDegreaz Posted June 7, 2009 Author Share Posted June 7, 2009 I tried that, however, I get the NOTICE that those constants are undefined. [07-Jun-2009 04:24:57] PHP Notice: Use of undefined constant STDIN - assumed 'STDIN' in D:\...ExtensionManager.php on line 36 [07-Jun-2009 04:24:57] PHP Notice: Use of undefined constant STDOUT - assumed 'STDOUT' in D:\...ExtensionManager.php on line 36 [07-Jun-2009 04:24:57] PHP Notice: Use of undefined constant STDERR - assumed 'STDERR' in D:\...ExtensionManager.php on line 36 Link to comment https://forums.phpfreaks.com/topic/161240-solved-cli-closes-without-error-after-inputting-text-with-sdtin/#findComment-850926 Share on other sites More sharing options...
Daniel0 Posted June 7, 2009 Share Posted June 7, 2009 Hmm... how are you calling it? Those constants are only defined for the CLI SAPI. I assumed that's what you were using. Try to run php -v from the CLI and see what it outputs. Link to comment https://forums.phpfreaks.com/topic/161240-solved-cli-closes-without-error-after-inputting-text-with-sdtin/#findComment-850935 Share on other sites More sharing options...
XaeroDegreaz Posted June 7, 2009 Author Share Posted June 7, 2009 Nevermind guys, it's not that big of a deal. Since I seemed to fix my original problem, I'll go ahead and mark this as solved. Thanks for your guys' effort.. Link to comment https://forums.phpfreaks.com/topic/161240-solved-cli-closes-without-error-after-inputting-text-with-sdtin/#findComment-851042 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.