Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A Simple ToDo App Using PHP and MongoDB
#!/usr/bin/env php
<?php
error_reporting( E_ERROR | E_PARSE );
class ToDoApplication {
protected $argv = array();
public function __construct ( $argv ) {
$this->argv = $argv;
try {
$this->mongo_connection = $mongo = new Mongo();
$this->mongo_db = $this->mongo_connection->todo;
}
catch ( Exception $e ) {
throw new Exception ( "Oops! Error connecting to MongoDB: " . $e->getMessage() );
}
}
protected function help ( $error = null ) {
if( ! is_null( $error ) ) {
print "$error\n";
print "\n";
}
$out = <<<EOF
usage: {$this->argv[0]} <method>
== Methods ==
<none> list all incomplete tasks sorted by priority then chronologically
help show this help
next list all incomplete tasks that are high priority
done list all complete tasks chronologically
high <argument> add high priority task called <argument>
low <argument> add low priority task called <argument>
finish <argument> complete task called <argument>
dont <argument> delete unfinished task called <argument>
EOF;
print $out;
}
public function run () {
try {
if ( 1 >= count( $this->argv ) ) {
$this->show(
array( "complete" => false ),
array( "level" => 1, "added" => 1 )
);
}
else {
$argument = strtolower( $this->argv[1] );
if ( "help" == $argument )
$this->help();
else if ( "next" == $argument )
$this->show(
array( "level" => "high", "complete" => false ),
array( "level" => 1, "added" => 1 )
);
else if ( "done" == $argument )
$this->show(
array( "complete" => array( '$ne' => false ) ),
array( 'completed' => 1 )
);
else if ( "high" == $argument )
$this->add( "high" );
else if ( "low" == $argument )
$this->add( "low" );
else if ( "finish" == $argument )
$this->complete( true );
else if ( "dont" == $argument )
$this->complete( false );
else
throw new Exception( "Bad Method" );
}
}
catch ( Exception $e ) {
$this->help( $e->getMessage() );
}
}
protected function show ( $params = null, $sort = null ) {
$cursor = $this->mongo_db->todos->find( $params );
if( ! is_null( $sort ) )
$cursor->sort( $sort );
foreach ( $cursor as $row )
print $row['task'] . "\n";
}
protected function add ( $level ) {
if ( 2 >= count( $this->argv ) )
throw new Exception( "missing argument" );
$this->mongo_db->todos->insert( array( "task" => $this->argv[2], "level" => $level, "complete" => false, "added" => time() ) );
}
protected function complete ( $finish ) {
if ( 2 >= count( $this->argv ) )
throw new Exception( "missing argument" );
$document = $this->mongo_db->todos->findOne( array( "complete" => false, "task" => $this->argv[2] ) );
if ( is_null( $document ) )
print "No Matching ToDo Found\n";
else {
if ( $finish ) {
$document['complete'] = time();
$this->mongo_db->todos->save( $document );
}
else {
$this->mongo_db->todos->remove( $document );
}
}
}
} // Class ToDoApplication
if( 'cli' == PHP_SAPI ) {
try {
$app = new ToDoApplication( $argv );
$app->run();
}
catch( Exception $e ) {
print $e->getMessage() . "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.