Skip to content

Instantly share code, notes, and snippets.

@jmhobbs
Created May 18, 2010 15:47
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jmhobbs/405143 to your computer and use it in GitHub Desktop.
A Simple ToDo App Using PHP and MongoDB
#!/usr/bin/env php
<?php
error_reporting( E_ERROR | E_PARSE );
class ToDo {
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", $this->argv[2] );
else if ( "low" == $argument )
$this->add( "low", $this->argv[2] );
else if ( "finish" == $argument )
$this->complete( true, $this->argv[2] );
else if ( "dont" == $argument )
$this->complete( false, $this->argv[2] );
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, $item ) {
if ( 2 >= count( $this->argv ) )
throw new Exception( "missing argument" );
$this->mongo_db->todos->insert( array(
"task" => $item,
"level" => $level,
"complete" => false,
"added" => time()
) );
}
protected function complete ( $finish, $item ) {
if ( 2 >= count( $this->argv ) )
throw new Exception( "missing argument" );
$document = $this->mongo_db->todos->findOne( array( "complete" => false, "task" => $item ) );
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 ToDo
if( 'cli' == PHP_SAPI ) {
try {
$app = new ToDo( $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