Skip to content

Commit db627d3

Browse files
committed
Added command line interface
1 parent 972471e commit db627d3

19 files changed

Lines changed: 1005 additions & 12 deletions

app/Controllers/Auth/AuthController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ public function getSignout($request, $response){
3030
*/
3131
public function signin($request, $response){
3232

33+
//if user is logged in redirect to home
34+
if($this->auth->user()){
35+
$this->flash->addMessage('info', 'You are already logged in!');
36+
return $response->withRedirect($this->router->pathFor('users.view',['id'=> $this->auth->user()->id]));
37+
}
38+
39+
3340
if($request->isPost()){
3441

3542
//Attempt to log user in

bin/console

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env php
2+
3+
<?php
4+
5+
require_once __DIR__ . '/../vendor/autoload.php';
6+
7+
use Symfony\Component\Console\Application;
8+
use Make\ModelCommand;
9+
use Make\ControllerCommand;
10+
use Make\ViewCommand;
11+
12+
$app = new Application();
13+
14+
$app->add(new ModelCommand());
15+
$app->add(new ControllerCommand());
16+
$app->add(new ViewCommand());
17+
18+
$app->run();

bin/src/ConfirmCommand.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Hash;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
use Symfony\Component\Console\Input\InputOption;
10+
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
11+
12+
use Hash\Hash;
13+
14+
class ConfirmCommand extends Command{
15+
16+
protected function configure(){
17+
$this->setName("Hash:Confirm")
18+
->setDescription("Confirms an Hash given the string.")
19+
->addArgument('Password', InputArgument::REQUIRED, 'What password do you wish to confirm?)')
20+
->addArgument('Hash', InputArgument::REQUIRED, 'What is the hashyou want to confirm?');
21+
}
22+
23+
protected function execute(InputInterface $input, OutputInterface $output){
24+
25+
$hash = new Hash();
26+
$inputPassword = $input->getArgument('Password');
27+
$inputHash = $input->getArgument('Hash');
28+
29+
$result = $hash->checkHash($inputPassword, $inputHash);
30+
31+
if($result){
32+
$output->writeln('The hash belongs to the password!');
33+
return true;
34+
}
35+
36+
$output->writeln('The hash does not belong to the password!');
37+
38+
}
39+
40+
}

bin/src/ControllerCommand.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Make;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
use Symfony\Component\Console\Input\InputOption;
10+
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
11+
12+
use Make\Make;
13+
14+
class ControllerCommand extends Command{
15+
16+
protected function configure(){
17+
$this->setName("make:controller")
18+
->setDescription("Hashes a given string using Bcrypt")
19+
->addArgument('controllerName', InputArgument::REQUIRED, 'What is the name of the controller you wish to create? Controller name must be capitalized and Plural with ending with the suffix Controller. eg. PostsController)');
20+
}
21+
22+
protected function execute(InputInterface $input, OutputInterface $output){
23+
24+
$make = new Make();
25+
$input = $input->getArgument('controllerName');
26+
27+
//confirm that the Controller name entered is formatted correctly
28+
29+
//confirm that it contains the word controller
30+
if( strpos( $input, "Controller" ) == false ) {
31+
$output->writeln('Error: Your controller name is not named correctly. It should be in plural and should end with the word Controller. Eg. PostsController');
32+
}else if($input{0} !== strtoupper($input{0})){
33+
//the first character does not start with an upper case
34+
$output->writeln('Error: Your controller name must start with a capital letter eg. PostsController');
35+
}else if( strpos( $input, "." ) !== false ) {
36+
$output->writeln('Error: Your controller name cannot contain a dot. Here is an example of a good controller name : PostsController');
37+
}else{
38+
$result = $make->makeController($input);
39+
40+
$output->writeln('Your controller has been created in app/Controllers/' . $input.'.php');
41+
42+
}
43+
44+
45+
}
46+
47+
}

bin/src/Make.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}

bin/src/ModelCommand.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Make;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
use Symfony\Component\Console\Input\InputOption;
10+
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
11+
12+
use Make\Make;
13+
14+
class ModelCommand extends Command{
15+
16+
protected function configure(){
17+
$this->setName("make:model")
18+
->setDescription("Creates a model file in app/Models")
19+
->addArgument('ModelName', InputArgument::REQUIRED, 'The name of the model - must be Camel Cased and Singular)');
20+
}
21+
22+
protected function execute(InputInterface $input, OutputInterface $output){
23+
24+
$make = new Make();
25+
$input = $input->getArgument('ModelName');
26+
27+
//Check if model name has a dot, throw error
28+
if( strpos( $input, "." ) !== false ) {
29+
$output->writeln('Error: Your model name cannot contain a dot. Example of a good model name: Post');
30+
} else if (strtolower($input[0]) == $input[0]){
31+
//the model name does not start with a capital letter
32+
33+
$output->writeln('Error: Your model name needs to start with a capital letter. Example of a good model name: Post ');
34+
} else{
35+
$result = $make->makeModel($input);
36+
$output->writeln('Your model has been created in app/Models/' . $input.'php - here is the result '.$result);
37+
}
38+
39+
40+
41+
}
42+
43+
}

bin/src/ViewCommand.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Make;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
use Symfony\Component\Console\Input\InputOption;
10+
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
11+
12+
use Make\Make;
13+
14+
class ViewCommand extends Command{
15+
16+
protected function configure(){
17+
$this->setName("make:view")
18+
->setDescription("Creates a vew folder in resources/views/ with 4 different views")
19+
->addArgument('viewName', InputArgument::REQUIRED, 'What is the name of the view folder you wish to create? View names must be plural and small caps. They should but not necessarily be the same with the name of the table in the database)');
20+
}
21+
22+
protected function execute(InputInterface $input, OutputInterface $output){
23+
24+
$make = new Make();
25+
$input = $input->getArgument('viewName');
26+
27+
28+
if(preg_match('/[A-Z]/', $input)){
29+
// There is atleast one uppercase letter
30+
$output->writeln('Error: your view name cannot contain an upper case letter. Try putting everything in small cap' . $result);
31+
}else if( strpos( $input, "." ) !== false ) {
32+
$output->writeln('Error: Your view name cannot contain a dot');
33+
}else{
34+
35+
$result = $make->makeView($input);
36+
$output->writeln('4 views have been created in the resources/views/' . $input .' folder');
37+
38+
}
39+
40+
41+
42+
}
43+
44+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
$container['SamplesController'] = function($container){
3+
return new \App\Controllers\SamplesController($container);
4+
};

0 commit comments

Comments
 (0)