|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Make; |
| 4 | + |
| 5 | +class Make{ |
| 6 | + |
| 7 | + /** |
| 8 | + * Receives a string name of a controller and creates the file |
| 9 | + * |
| 10 | + * @param string $controllerName |
| 11 | + * @return string string |
| 12 | + */ |
| 13 | + public static function makeController( $controllerName ){ |
| 14 | + //controller name must be camelcased and plural |
| 15 | + //model file name must be CamelCased and singular eg. Post |
| 16 | + |
| 17 | + //model file name must be CamelCased and singular eg. Post |
| 18 | + |
| 19 | + //get a sample copy of the text to be written to the controller config |
| 20 | + $controllerFile = file_get_contents (__DIR__ .'/templates/Controllers.php'); |
| 21 | + |
| 22 | + $controllerTemplate = __DIR__ . '/../../app/Controllers/'.$controllerName.'.php'; |
| 23 | + $handle = fopen($controllerTemplate,"x+") or die('Cannot open file: '.$controllerTemplate); |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + if($controllerFile){ |
| 28 | + $writeToFile = fwrite($handle, $controllerFile); |
| 29 | + |
| 30 | + |
| 31 | + if($writeToFile){ |
| 32 | + fclose($handle); |
| 33 | + |
| 34 | + //update Controller Config |
| 35 | + //get a sample copy of the text to be written to the controller config |
| 36 | + $controllerConfigTemplate = file_get_contents (__DIR__ .'/templates/ControllerConfig.php'); |
| 37 | + $controllerConfigTemplate = str_replace('SamplesController',$controllerName, $controllerConfigTemplate); |
| 38 | + $controllerConfigTemplate = str_replace('<?php','', $controllerConfigTemplate); |
| 39 | + |
| 40 | + $controllerConfigFile = __DIR__ . '/../../config/ControllerConfig.php'; |
| 41 | + |
| 42 | + $controllerConfigHandle = fopen($controllerConfigFile,"a") or die('Cannot open file: '.$controllerConfigFile); |
| 43 | + |
| 44 | + fwrite($controllerConfigHandle, "\n". $controllerConfigTemplate); |
| 45 | + fclose($controllerConfigHandle); |
| 46 | + |
| 47 | + |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Makes four views namely add,edit,view,index |
| 58 | + * @param undefined $viewName |
| 59 | + * |
| 60 | + * @return |
| 61 | + */ |
| 62 | + public static function makeView( $viewName ){ |
| 63 | + |
| 64 | + //we just need to copy the view template to the resources/views/ folder |
| 65 | + $src = __DIR__ .'/templates/views'; // source folder |
| 66 | + mkdir(__DIR__ . '/../../resources/views/'.$viewName); //create the folder first |
| 67 | + $dest = __DIR__ . '/../../resources/views/'.$viewName; // destination folder |
| 68 | + |
| 69 | + copy($src.'/add.twig', $dest.'/add.twig'); //copy all file |
| 70 | + copy($src.'/edit.twig', $dest.'/edit.twig'); //copy all file |
| 71 | + copy($src.'/view.twig', $dest.'/view.twig'); //copy all file |
| 72 | + $copyViews = copy($src.'/index.twig', $dest.'/index.twig'); //copy all file |
| 73 | + if($copyViews){ |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Makes a single model file |
| 82 | + * @param undefined $ModelName |
| 83 | + * |
| 84 | + * @return |
| 85 | + */ |
| 86 | + public static function makeModel( $modelName ){ |
| 87 | + //model file name must be CamelCased and singular eg. Post |
| 88 | + |
| 89 | + //get a sample copy of the model |
| 90 | + $modelFile = file_get_contents (__DIR__ .'/templates/Models.php'); |
| 91 | + |
| 92 | + $my_file = __DIR__ . '/../../app/Models/'.$modelName.'.php'; |
| 93 | + $handle = fopen($my_file,"x+") or die('Cannot open file: '.$my_file); |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + if($modelFile){ |
| 98 | + $writeToFile = fwrite($handle, $modelFile); |
| 99 | + |
| 100 | + if($writeToFile){ |
| 101 | + clearstatcache(); //flush the file cache |
| 102 | + fclose($handle); |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + return false; |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +} |
0 commit comments